1. Number
Numbers can be used directly in Python operations, using parentheses to group
In [1]: 2+2out[1]: 4In [2]: 50-5*6out[2]: 20In [3]: (50-5.0*6)/4out[3]: 5.0In [4]: 8/5.0out[4]: 1.6
In the case of division/, if the 2 divisor is of type int, the returned value is also integral int:
If there are 1 divisor of float float, then the result value is float type float;
With the operator//division, the returned value is the rounding
Use% to do division to take remainder;
In [5]: 17/3out[5]: 5In [6]: 17/3.0out[6]: 5.666666666666667In [7]: 17//3.0out[7]: 5.0In [8]: 17%3out[8]: 2
Use * * To calculate the exponentiation:
In [9]: 5**2out[9]: 25In [ten]: 2**7out[10]: 128
To assign a value using the = sign:
In [all]: width=20in []: height=5*9in []: Width * heightout[13]: 900
If a variable is not defined, then using it directly will make an error:
in [+]: N---------------------------------------------------------------------------nameerror Traceback (most recent) <ipython-input-14-fe13119fb084> in <module> ()----> 1 nnameerror:name ' n ' is not defined
Conversion of integral and floating-point types:
In []: 3*3.75/1.5out[15]: 7.5In [+]: 7.0/2out[16]: 3.5
The result of the last printed calculation can be given directly to the "_" symbol
in [+]: tax=12.5/100in []: price=100.50in [+]: Tax * priceout[19]: 12.5625In []: price+_out[20]: 113.0625In [+]: RO und (_,2) out[21]: 113.06
2, String
You can use single quotation marks and double quotation marks to refer to a string directly, and a backslash to escape quotation marks:
in [+]: ' Spam eggs ' out[22]: ' Spam eggs ' in [all]: ' doesn\ ' t ' out[23]: ' doesn ' t ' in []: ' doesn ' t ' out[24]: ' doesn ' t ' in [25]: ' Yes, ' he said. ' OUT[25]: ' Yes, ' he said. ' in []: "\" yes,\ "he said." OUT[26]: ' Yes, ' he said. ' in [+]: ' "isn\ ' t," she said. OUT[27]: ' isn\ ' t, ' she said. '
Use the Print command to ignore character quotes and to print special meaning symbols:
in [+]: ' isn\ ' t, ' she said. ' OUT[28]: ' isn\ ' t, ' she said. ' In []: print ' "isn\ ' t," she said. " Isn ' t, "she said." in [+]: s = ' first line.\nsecond line. ' in [+]: sout[31]: ' First line.\nsecond line. ' in [+]: print Sfirst line. Second line.
If you don't want to print a special character, you can precede the first quotation mark with R:
in [+]: print ' C:\some\name ' C:\someameIn [+]: Print R ' C\some\name ' C\some\name
You can put more than one line in a row by using the quotation marks, but the plus \ symbol after the first quotation mark will still use multiline mode:
in [38]: print "" " ....: Usage: thingy [OPTIONS] ....: -h Display this usage message ....: -h hostname hostname to connect to ....: "" "Usage: thingy [options] -h display this usage message -h hostname Hostname to connect to
String can use the + sign join or the * number operation
in [+]: 3 * ' un ' + ' ium ' out[40]: ' Unununium '
Connecting multiple strings can be used in the following ways:
in []: ' Py ' ' thon ' out[42]: ' Python '
Use the + symbol to concatenate a variable with a string:
In ["]: prefix = ' Py ' in []: prefix + ' thon ' out[44]: ' Python '
Using quotation marks is useful when you need to concatenate long strings together:
In []: Text = (' Put several strings within parentheses ' ....: ' To has them joined together. ') in [+]: textout[46]: ' Put several strings within parentheses to has them joined together. '
A string can have an index:
in [+]: Word = ' Python ' in [word[0]out[48]: ' P ' in [ps]: word[5]out[49]: ' n ' in [+]: word[-1]out[50]: ' n ' in [Wuyi]: Wor D[-2]OUT[51]: ' O ' in [Page]: word[-6]out[52]: ' P '
Slice of string:
in [+]: word[0:2]out[53]: ' Py ' in [si]: word[2:5]out[54]: ' tho ' in []: Word[:2] + word[2:]out[55]: ' Python ' in []: word[ : 4] + word[4:]out[56]: ' Python '
The string slice index shows:
+---+---+---+---+---+---+ | P | y | T | H | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6-6-5-4-3-2-1
Slices that exceed the index order can be handled more amicably:
In [the]: word[4:42]out[68]: ' On ' in []: word[42:]
String cannot be changed:
In []: word[0] = ' J '---------------------------------------------------------------------------TypeError Traceback (most recent) <ipython-input-69-197b67ffdd83> in <module> ()----> 1 word [0] = ' J ' TypeError: ' str ' object does not a support item assignment
If you need a different string, you can generate a new string:
In [all]: ' J ' + word[1:]out[70]: ' Jython ' in [[]: Word[:2] + ' py ' out[71]: ' PyPy '
Use the built-in Len function to calculate the length of a string:
In [out[72]: Len (word): 6
This article is from the "Ordinary Road" blog, please be sure to keep this source http://linjohn.blog.51cto.com/1026193/1608530
Python2.7 Self-paced note using Python as a calculator