Variables: Variables define the Rules: 1. Variable names can only be any combination of letters, numbers, or underscores
2. The first character of a variable name cannot be a number
3. The following keywords cannot be declared as variable names
[' and ', ' as ', ' assert ',
' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ',
' Finally ', ' for ', ' from ', ' global ', ' if ', ' Import ', ' on ', ' is ', ' lambda ',
' Not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' with ', ' yield '] data type: Two, numeric type: Int (integer) on a 32-bit machine, the number of integers is 32 bits , the value range is -2**31~2**31-1, that is, -2147483648~2147483647 on a 64-bit system, the number of digits of the integer is 64 bits, the value range is -2**63~2** 63-1, that is, -9223372036854775808~9223372036854775807 long (Long integer) is different from the C language, Python's long integer does not refer to the positioning width, that is: Python does not limit the size of long integer values, But in fact, because of the limited machine memory, we use a long integer value can not be infinitely large. Note that, since Python2.2, Python automatically converts integer data to long integers if an integer overflows, so it does not cause any serious consequences if you do not add the letter L after long integer data. Float (float) floats are used to process real numbers, that is, numbers with decimals. Similar to the double type in C, accounting for 8 bytes (64 bits), where 52 bits represent the bottom, 11 bits represent the exponent, and the remaining one represents the symbol. Complex (plural) complex numbers are composed of real and imaginary parts, the general form is X+yj, where x is the real part of the complex number, and Y is the imaginary part of the plural, where x and y are real numbers. Note: Small number pools exist in Python:-5 ~
2,573, Boolean true or False1 or 0 (values other than 0 are true) how do I see the bool type of a variable? >>>bool (0) Four, string concatenation of the evil string: The string in Python is embodied in the C language as a character array, each time you create a string, you need to open a contiguous space in memory, and once you need to modify the string, you need to open up again, Every occurrence of the evil + sign will re-open a space within. Simple to understand is to use the plus sign to stitch strings wasted resource string formatting name = ' Ian ' age = 12print ('%s is%d years
Old. '% (name,age)) #字符串是%s, integer%d, floating-point%f string common functions: str = ' Ian is 12!! ' # Remove white space # This method removes the leading and trailing spaces and the end of the \ n newline character Str.strip () # split #split () places the segmented fields into a list, separated by a space by default, Str.split (', ') separated by commas s = str.split () Print (type (s)) print (s[1]) # length print (len (str)) # index # gives a string that can output any one character, or, if the index is negative, is equivalent to the forward number from the back. Print (str[10]) print (str[-4]) #切片 # Slice is a partial separation of content from a given string print (Str[0:3]) print (Str[:3]) v. List creation list: List1 =
[' Apple ', ' pear ', ' peach '] or List1 =
List ([' Apple ', ' pear ', ' peach ') lists of commonly used functions: listing =
[' Apple ', ' pear ', ' peach ', 66] # Index print (list[0]) # Slice, same as String print (List[0:2]) # append list.append (' banana ') print (list) # delete # Remove method, delete element, no return value #pop method, delete element, return element value, default from backward to List.remove (' banana ') print (' * * * *) A = List.pop () print (a) Print (List.pop (2)) #删掉peach, or use the pop (-2) # length to display the number of list elements print (len (list)) # Loop, how to loop a list? x = 0 #为列表元素添加序号for i in list:x + = 1 print (x,i) # contains if ' Apple ' in List:print (' ") else:print (' out ') VI, GANSO create a meta-ancestor: Tuple1 =
(' Apple ', ' pear ', ' peach ') or tuple1 =
Tuple (' apple ', ' pear ', ' peach ') Ganso common operations: Tuple1 =
(' Apple ', ' pear ', ' peach ') #元祖和列表基本差不多, but Ganso cannot be modified after creation, list can be modified # index print (tuple1[1]) # Slice print (Tuple1[1:3]) # loop x = 0for I in TUPL E1:x + = 1 print (x,i) # length print (len (tuple1)) # contains if ' Apple ' in Tuple1:print (' ") else:print (' Out ') Seven, Dictionary dictionary is unordered!!!! Create Dictionary: dic =
{' K1 ': ' v1 ', ' K2 ': ' v2 '} or dic =
Dict ({' K1 ': ' v1 ', ' K2 ': ' v2 '}) Common operations for dictionaries: DIC =
{' K1 ': ' v1 ', ' K2 ': ' v2 '} # index # dictionary is indexed by using key as key sub-print (dic[' K1 ']) # added dic[' K3 '] = ' v3 ' Print (DIC) # Delete #del in the same list Remove,pop () method is still the same, delete the value and return del dic[' K3 ']print (dic) Del_key = Dic.pop (' K2 ') print (Del_key) print (DIC) # Key, value, key value pair print (' * ' *20) dic =
{' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 '}print (Dic.keys ()) #只显示keyprint (Dic.values ())) #只显示valueprint (Dic.items ()) # Display key and value # loop for I in DIC: #默认为. Keys (), can be dic.values () or Dic.items () print (i) # length print (len (DIC)) eight, loop/range/break/ continue# Loop # A simple for loop as follows for I in [All-in-one]: print (i) #range函数 >>> range (1,5)
#代表从1到5 (not including 5) [1, 2, 3, 4]>>>
Range (1,5,2) #代表从1到5, Interval 2 (not including 5) [1, 3]>>> range (5)
#代表从0到5 (not including 5) [0, 1, 2, 3, 4]ps: The above is the display method of 2.0, not applicable in 3.0, in 3.0 you can use the loop to remove the #breakbreak语句可以用在for循环和while循环语句中. Simply put, the break statement exits the loop immediately, and the loop body on the subsequent side is not executed. #continuecontinue语句也是用在for循环和while循环语句中, using continue, you can skip this cycle, the incomplete loop body does not loop, but the next loop directly