Python basics 1: python Basics
About Memory Allocation
1 ## create a new memory space for the new string Definition 2 >>> str1 = 'hoho' 3 >>> str2 = str1 4 >>> id (str1 ), id (str2) # view the address of the Memory Object and observe the memory address. That is, str2 has opened up memory space 5 (140297199501752,140 297199501752) # Here we can see that the same is caused by an internal mechanism of python. If the string is large enough, it will be different, do not tangle 6 >>> str2 = 'heihei' 7 >>> str1 8'hoho' 9 >>> str210 'heihei' 11 >>> id (str1 ), id (str2) # Check whether the memory address has changed 12 (140297199501752,140 297214622552) 13 14 # non-character server, such as list, tuples, after a dictionary value is defined, it only points the new variable name (which can be understood as a tag) to the same memory address. For example, the dictionary is shown as follows: 15 >>> dic1 = {'name ': 'hohoho'} 16 >>>> dic2 = dic117 >>> id (dic1), id (dic2) 18 (140297199190088,140 297199190088) 19 >>> dic1 = {'name ': 'hoho'} 20 >>>> dic2 = dic121 >>>> id (dic1), id (dic2) # view the address of the memory object, therefore, dic2 was modified. In fact, dic1 also followed by 22 (140297199191752,140 297199191752) 23 >>> dic2 ['name'] = 'heihei' 24 >>> dic225 {'name ': 'heihei'} 26 >>> dic127 {'name': 'heihei '}
Copying list, tuples, and dictionaries (use of the copy module for shortest and deep copy)
1. You can use slices for the list and tuples for shortest copy, or use the copy module for shortest copy (including dictionaries)
2. Use copy. deepcopy () instance for deep Replication
1 >>> import copy 2 >>> list1 = [1, 2] 3 >>> list2 = list1 4 >>> list2 [0] = 2 # changed list2, list1 followed by 5 >>> list1 6 [2, 2] 7 >>> list3 = list1 [:] # very complex, use array slices for shallow copy 8 >>> list3 = copy. copy (list1) 9 >>> id (list1), id (list2), id (list3) # Here we can see that the address space is different 10 (140297199250696,140 297199250696, 140297199247560) 11 >>> 12 >>> list4 = [1, [2] ## complex structures must be copied in depth. 13 >>> list5 = list4 [:] 14 >>> list515 [1, [2] 16 >>> list5 [1] [0] = 617 >>> list418 [1, [6] # Here we can see that the list in the inner layer is not copied, and list4 has changed 19 >>> list6 = copy. deepcopy (list4) # Use deep copy here 20 >>> list6 [1] [0] = 821 >>> list622 [1, [8] 23 >>> list424 [1, [6] # The copied
Common built-in functions
There are a lot of built-in functions in python, just remember the common ones, but you will know how to view the built-in functions and help functions.
Under normal circumstances, take three steps
Integer
1 >>> s, y = divmod (7,3) ## divmod returns data with the value (quotient, remainder). It can be used for paging 2 >>> s, y3 (2, 1) 4 >>> a =-25 >>> abs (-2) # abs obtains the absolute value of 6 27 >>> len (str (-2 )) # Get SPEED length 8 2View Code
Floating Point
1 >>> a = 7.02 >>> divmod (a, 3) 3 (2.0, 1.0) 4 >>> a = 7.2355 >>>. _ round _ (2) # rounding to 6 7.247>. _ trunc _ () # Round 8 7View Code
String
1 >>> str1 = 'this is a string '2 >>>> 'is in str1 # member judgment 3 True 4 >>> str1 [] # Slice operation and index 5 'Hi' 6> len (str1) # length 7 16 8 >>> str1.find ('is ') # search for a string and return the index value 9 210 >>>> str1.find ('is) 11 512 >>> str1.find ('iss') # Return-1 is not found. If it is an index, the system reports an error 13-114 >>> str1.index ('is', 3) 15 516 >>> str1.index ('iss') 17 Traceback (most recent call last): 18 File "<stdin>", line 1, in <module> 19 ValueError: substring not found20 >>> str1 = 'aaa' 21 >>> str1.strip () to blank, line feed, press ENTER 22 'aaa' 23 >>> str1.lstrip () 24 'aaa' 25 >>> str1.rstrip () 26 'aaa' 27 >>> str1 = 'duiqi '# alignment operation 28 >>> str1.ljust (20) 29 'duiqi' 30 >>> str1.ljust (20 ,'*') 31 'duiqi **************** '32 >>> str1.20.ust (20 ,'*') 33 '**************** duiqi' 34 >>> str1.center (20 ,'*') 35 '******** duiqi *********' 36 >>> str1 = 'this is a string '37 >>> str1.split () # split operation 38 ['this', 'is ', 'A', 'string'] 39 >>> str1.splitlines () 40 ['this is a string'] 41 >>> list1 = [1, 2, 3] 42 >>> '-> '. join ([str (I) for I in list1]) # connection operation 43 '1-> 2-> 3' 44> str145' this is a string '46 >>> str1.count ('is ') # Count 47 248 >>> str1.replace ('is, OS ') # Replace 49 'thos OS a string' 50 >>>> str1.replace ('is, OS ', 1) # Replace, replace only one 51 'thos' is a string '52 53 str1.startswith ('sub') # Start with 54 str1.endswith ('sub ') # End with 55 str1.lower () # convert to lowercase 56 str1.upper () # convert to uppercaseView Code
List and tuples (tuples cannot be modified)
1 >>> lst1 = ['a'] 2 >>> lst1.append ('B') # Add 3 >>> lst1 4 ['A ', 'B'] 5 >>> lst2 = ['C', 'D'] 6 >>> lst1.extend (lst2) # New extension 7 >>> lst1 8 ['A', 'B', 'C', 'D'] 9 >>> lst1.insert (0, 'z ') # Insert 10 >>> lst111 ['Z', 'A', 'B', 'C', 'D'] 12 >>> lst1.pop () # Remove end 13 'D' 14 >>> lst115 ['Z', 'A', 'B', 'C'] 16 >>> lst1.remove ('Z ') # Delete the specified element 17 >>> lst118 ['A', 'B', 'C'] 19 >>> lst1 = ['A', 'B ', 'C', 'D'] 20 >>> lst2 = lst1.copy () #21 for the shortest python3 >>>> lst2 = lst1.copy () 22 >>> lst223 ['A', 'B', 'C', 'D'] 24 >>> lst2.clear () # Clear List 25 >>> lst226 [] 27 >>> del lst2 # Delete list 28 >>> lst129 ['D', 'C', 'B ', 'A'] 30 >>> lst1.sort () # Sort 31 >>> lst132 ['A', 'B', 'C ', 'D '] 33 >>> lst1.append ('A') 34 >>> lst1.count ('A') # Count 35 236 >>> lst137 ['A ', 'B', 'C', 'D', 'a'] 38 >>> len (lst1) # length 39 540 >>> lst1.index ('A ') # index 41 042 >>> lst1.index ('A', 1) # index 43 4View Code
Dictionary
1 >>> dic1 = {'key1': 'A', 'key2': 'B'} 2 >>> dic1.get ('key1') # obtain the dictionary value, if None is returned by default, you can specify 3 'A' 4 >>> dic1.get ('key3') 5 >>> dic1.items () 6 dict_items ([('key2 ', 'B'), ('key1', 'A')]) # returns the list of tuples 7 >>> list (dic1.items () 8 [('key2 ', 'B'), ('key1', 'A')] 9> dic1.keys () # Return to the keys list 10 dict_keys (['key2', 'key1']) 11 >>> dic1.values () # Return Value List 12 dict_values (['B', 'a']) 13 >>> dic2 = dic1.copy () # Light copy 14 >>> dic215 {'key2': 'B', 'key1 ': 'A'} 16 >>> dic1 ['key3'] = 'C' # assign a value (modify) 17 >>> dic118 {'key2': 'B ', 'key1': 'A', 'key3': 'C'} 19 >>> dic1.pop ('key1 ') # Delete the specified key20'a' 21 >>> dic122 {'key2': 'B', 'key3': 'C'} 23 >>> dic1.get ('key1 ', 'A ') # value. No value is returned for 'a '24' a' 25 >>> dic126 {'key2':' B ', 'key3 ': 'C'} 27 >>> dic1.setdefault ('key1', 'A') # Set the default value (it seems useless) 28 'A' 29 >>> dic130 {'key2': 'B', 'key1': 'A', 'key3 ': 'C'} 31 >>>> dic3 = {'name': 'update'} 32 >>> dic1.update (dic3) # update 33 >>> dic1 34 {'key2 ': 'B', 'name': 'update', 'key1': 'A', 'key3 ': 'C'} 35 >>> del dic3 # Delete 36 >>> dic137 {'key2': 'B', 'name': 'update', 'key1 ': 'A', 'key3': 'C'} 38 >>> len (dic1) # length 39 4View Code