Some of the frequently used concepts in Python:
1. There is no constant concept in python, in other words all the quantities in Python can be changed, specifying that the amount defined as a constant is capitalized, Eg:constof = "This is a const".
2. In Python we import a three-party library to enter the >>\user\administrator\pip install library name under DOS
3. Statements of several commonly used libraries
Import Osos.system ("ipconfig") # View IP address under Windows res = Os.system ("ipconfig") # Returns the result of the program res = Os.open (" Ipconfig "). Read () # Returns the result of the execution of the program, can print out the import Syssys.path # Print out the environment variable, usually we will need to follow the library placed in the site Under-packages or Dist-packages
4.PYC file, that is, we import a file in another file compiled in the file for the interpreter to use, my understanding is that a library file for other files, for the convenience of the interpreter call generated. Python3 is placed in a cache folder.
5. Use less "+" hyphens when working with character output, as it reduces program efficiency
6. Operation of character type:
7, for the operation of the list: (the list is actually the array we often say)
list = ["Beijing", "Shanghai", "Nanchang", "Wuhan", "Nanchang", "Changsha", "Shenzhen"]list.insert (2, "6") # Changes the second element of the list to 6, inserting only one character at a time List.insert (5, "tianjing") # the 5th element in the list becomes tianjinglist.append ("Taiwan") # at the end of the list, add the Taiwan element List.remove ("Shanghai") # Remove an element from the list, and Del in Python, to delete list.index ("Nanchang") for each # returns the index position where Nanchang is located in the list # the program that deletes the specified character from the entire list can be written in this way. CNT = list.count ("Nanchang")) # returns the number of "Nanchang" List.count ("Nanchang") in the list # Indicates that there are multiple Nanchang characters in the list list1 =["Hangzhou", "234", "All", "Xinjiang"] # Add a new list after the list list1list.extend (List1) list.reverse () # Invert the original list List.pop (2) # Specify subscript To delete the list, delete the third element For i in range (CNT): list[list.index (" Nanchang ")] = " Wuhan "List.clear () # Delete entire list
Add a print to the back of each sentence to see how the list changes throughout the process, with the following results
[' Beijing ', ' Shanghai ', ' 6 ', ' Nanchang ', ' Wuhan ', ' Nanchang ',, ', Changsha ', ' Shenzhen ' [' Beijing ', ' Shanghai ', ' 6 ', ' Nanchang ', ' Wuhan ', ' ' tianjing ', ' Nanchang ', ' Changsha ', ' Shenzhen ' [' Beijing ', ' Shanghai ', ' 6 ', ' Nanchang ', ' Wuhan ', ' tianjing ', ' Nanchang ', ' Changsha ', ', ' Shenzhen ', ' Taiwan ' [' Beijing ', ' 6 ' , ' Nanchang ', ' Wuhan ', ' tianjing ', ', Nanchang ', ' Changsha ', ' Shenzhen ', ' Taiwan ' [' Beijing ', ' 6 ', ' Nanchang ', ' Wuhan ', ' tianjing ', ' Nanchang ',, ' Changsha ' , ' Shenzhen ', ' Taiwan ']2[' Beijing ', ' 6 ', ' Nanchang ', ' Wuhan ', ' "tianjing ', ' Nanchang ', ' Changsha ', ' Shenzhen ', ', Taiwan ', ' Hangzhou ', ' 234 ',, ' [], ' Xinjiang '] [' Xinjiang ', ', ' 234 ', ' Hangzhou ', ' Taiwan ', ' Shenzhen ', ' Changsha ', ' Nanchang ', ' tianjing ', ‘ Wuhan ', ' Nanchang ', ' 6 ', ' Beijing ' [' Xinjiang ', ' all ', ' Hangzhou ', ' Taiwan ', ' Shenzhen ', ' Changsha ', ' Nanchang ', ' ' tianjing ', ' Wuhan ', ' Nanchang ', ' 6 ', ' Beijing ' [' Xinjiang ', ', ' Hangzhou ', ' Taiwan ', ' Shenzhen ', ' Changsha ', ' tianjing ', ' Wuhan ', ' Wuhan ', ' 6 ', ' Beijing ']# ----------------------- I am the split line-------------------"" " there are some other operations, such as List.copy (), but it is worth noting that the list can be nested in multilevel lists, but call List2 = list.copy ( ), only the first list of lists is copied, and the depth of the table will only copy the address. If you want to complete a copy, you need to import the official library import copy, and call Copy.deepcopy, where the code is as follows "" "" "" "import copylist = [" Beijing ", "Shanghai", [1, 2, 3, 4, 5], "Nanchang", "Changsha", "Shenzhen"] List1 = list.copy () # Use the copy function of the list to assign a value, only completely copy the first layer, for the second layer and above (not verified) only copied the soft link(address) list2 = list # The first layer is a soft link, The second floor and above are also soft links (address) list3 = copy.deepcopy (List) # completely copied a list, divorced from the previous relationship print (ID (List), id (LIST1), id (LIST2), ID (LIST3)) # print out three different ways of variable address print ("--------------------I am Split Line--------------------------") list[0] = "Nanchang" Print (list) print (List1) print (List2) print (LIST3) print (ID (list), ID (LIST1), id (LIST2), ID (LIST3)) Print ("--------------------I am Split Line--------------------------") List[2][0:2] = [7, 8]print (List) Print (List1) print (List2) print (LIST3) print (ID (List), id (LIST1), id (LIST2), ID (LIST3))
The results of the
above run are as follows, and we can see that there are three different amplitude modes in the Python3, and then the LIST1,LIST2,LIST3 changes when we change the elements in the list of original lists. where List.copy () just completely replicates the first-tier list, and its deep list is not duplicated (only assigned addresses), the benefit is that it can greatly reduce the complexity of the deep list when there is a lot of data. And for the direct use of the operator "=" of the assignment is completely copied the address of the original list, similar to the C + + alias (reference), from the address of the variable can be seen, and the previous address is exactly the same. Copy.deepcopy () is a complete copy of the new data. But I think the deepcopy (deep copy) here is the opposite of the concept in C + +, (in C + + it is only a shallow copy of this assignment value).
14456264 14456776 14456264 14457928--------------------I'm the split line--------------------------[' Nanchang ', ' Shanghai ', [1, 2, 3, 4, 5], ' Nanchang ', ' Changsha ', ' Shenzhen ' [' Beijing ', ' Shanghai ', [1, 2, 3, 4, 5], ' Nanchang ', ' Changsha ', ' Shenzhen ' [' Nanchang ', ' Shanghai ', [1, 2, 3, 4, 5], ' Nanchang ', ' Changsha ', ' Shenzhen ' [' Beijing ', ' Shanghai ', [1, 2, 3, 4, 5], ' Nanchang ', ' Changsha ', ' Shenzhen ']14456264 14456776 14456264 14457928 --------------------I was dividing line--------------------------[' Nanchang ', ' Shanghai ', [7, 8, 3, 4, 5], ' Nanchang ', ' Changsha ', ' Shenzhen ' [' Beijing ', ' Shanghai ', [7, 8, 3, 4, 5], ' Nanchang ', ' Changsha ', ' Shenzhen ' [' Nanchang ', ' Shanghai ', [7, 8, 3, 4, 5], ' Nanchang ', ' Changsha ', ' Shenzhen ' [' Beijing ', ' Shanghai ', [1, 2, 3, 4, 5], ' Nanchang ', ' Changsha ', ' Shenzhen ']14456264 14456776 14456264 14457928
8, the introduction of the index
The index in Python is similar to the explanatory language of MATLAB. Take the LIS list above, the content is as follows
list = ["Beijing", "Shanghai", "Nanchang", "Wuhan", "Nanchang", "Changsha", "Shenzhen"]list[0:2] # Take the List of the front two elements, from left to right take the head not to take the tail, in MATLAB will be obtained three number, the result is [' Beijing ', ' Shanghai ']list[-3:-1] # Take the last two elements of the List, the answer is [' Nanchang ', ' Changsha ']list[-3:] # Take the last three elements of the List, the answer is ["Nanchang", "Changsha", "Shenzhen"]list[::2] # for the entire list, take one element in the Matlab language, the last element represents the step, and here is the last element that represents the step, not the default step of 1list[1:5][3][4] &nIn Bsp; # python, you can always slice the list, which means that the 2nd element in the list is sliced out before the 6th element, and then 4 (list[1], LIST[2], list[3], list[4], can be understood as a new temporary list) elements of the 4th element (that is, the original list[4]) extracted, and finally to the element (string "Nanchang") of the 5th character, so the answer is ' H '.
This article is from the "Exclusive Rainy Day" blog, please be sure to keep this source http://520sky.blog.51cto.com/9188727/1828229
Python Learning Path-Basic Concept (III)