1. Python Environment construction
1. Download Python and install it: HTTP://PAN.BAIDU.COM/S/1JHPWBLK
2. Start idle to edit Python code
2. Basic grammar
1. Note: A single line comment begins with #, and a multiline comment encloses the comment with three single quotation marks ("') or three double quotation marks (" ").
2. In interactive mode, the result of the last expression output is assigned to the variable _.
3. Data type (6 kinds): numbers,string,list,tuple,sets,dictionaries
1.Number (digital)
A. The numeric division (/) always returns a floating-point number to get the integer using the//operator. Example: 1/2=0.5 1//2=0
B. In mixed calculations, Pyhton converts an integer to a floating-point number.
C. Use the * * operation to perform a power operation. Example: 5**2//25 represents 5 of the square 2**7//128 represents 2 of the 7-time Square
2.String (String)
A. Enclose the single quotation mark (') or double quotation mark ("") and use a backslash (\) to escape the special character. Example: Print ("ddd\tddd")//ddd DDD
B. If you do not want the backslash to escape, you can add an R to the string before it to indicate the original string. Example: Print (r "ddd\tddd")//ddd\tddd
C. A backslash can be used as a continuation character, indicating that the next line is a continuance of the previous row.
Example >>> str= "ddd" \
"DDD"
>>> Print (str)
Dddddd
D. You can use "" "," "" "" "" "or" "..." to cross multiple lines.
>>> str= "" "ddd
DDD "" "
>>> Print (str)
Ddd
Ddd
E. Use + to indicate stitching, or repeat with *. Example: Print ("str" + "ing", "a")//string AAA
F. The string is indexed in two ways, starting from left to right and starting at 0 from right to left with-1; string interception: Variable [head subscript: Tail subscript].
Example: str= "Aabbccddeeff"
>>> str[0]//' a '
>>> Str[0:5]//' AABBC '
>>> str[3:-1]//' Bccddeef '
>>> str[3:-9]//'
>>> str[3:]//' Bccddeeff '
>>> Str[:5]//' AABBC '
G. The string is immutable. Example: str[2]= "D"//will report an error
3.List (list): between square brackets, the elements are separated by commas. ["AA", 23,true]
A. As with strings, lists can be indexed and sliced.
The elements in the b.list can be changed.
4.Tuple (Ganso): Written in parentheses, the elements are separated by commas. A= ("AA", 23,true,["a", "B"])
A. The elements of a tuple cannot be modified. Example: A[2]=false//Error
The b.tuple element cannot be changed, but it can contain mutable objects. Example: a[3][1]= "C"//("AA", 23,true,["a", "C"])
5.Sets (Collection): Create set set {"1", "2", 3} or set ("123") using curly braces or set () functions
A. Creating an empty collection must be set () instead of {}, because {} is used to create an empty dictionary.
B. The basic function is to conduct membership tests and eliminate duplicate elements.
Example:>>> str= "AABBCCDDFF"
>>> s=set (str)//{' F ', ' C ', ' d ', ' A ', ' B '}
>>> ' A ' in S//true member test
>>> "M" in s//false
>>> str2= "DDFFMM"
>>> S2=set (str2)//{' f ', ' d ', ' m '}
>>> s-s2//{' C ', ' A ', ' B '} computes the difference set of the added set
>>> s|s2//{' f ', ' C ', ' a ', ' d ', ' B ', ' m '} computes the set of two sets
>>> s&s2//{' f ', ' d '} computes the intersection of two sets
>>> s^s2//{' C ', ' A ', ' B ', ' m '} computes a non-simultaneous element of two sets
6.Dictionaries (dictionary): use braces or dict () function to create {"A": 1, "B": 2, "C": 3} or Dict ([("A", 1), ("B", 2), ("C", 3)])
Python Learning Summary 1