gogoWebsite

python returns list data type_python-basic data type

Updated to 4 hours ago

Data Type

1. Numbers

Int, Python3 does not have long integers, they are unified into integers, and there is no limit on size.

Float, python floating point type is the same as the C language double, and can represent 15-16-bit valid numbers.

2. Boolean type

True and False

3. String

1 myStr="CKZeng"# or:myStr='CKZeng' python single quotes and double quotes are no different

Various operations of strings: All the following operations will not change the string itself, and will return the value after calling the method myStr="CKZeng"

() # Change the initial letter to uppercase and other letters to lowercase

("CK",bengin,end) #Query how many "CK" are in the specified position of the query string, and you can also check a single character. Begin and end can be omitted

(20,"=") #The total length of the string is 20, if not enough, it will be supplemented on both sides.

(20,"=") #Fix on the right

(20,"=") #Follow on the left

() #Encoding, string to binary

("g") # Whether the string ends with a substring or character, returns a boolean value

("C",bengin,end) #From the position of bengin, find the position of the first substring or character

("C",bengin,end)#From the position of (end-1) to find the position of the first substring or character.

() #String format myStr.format_map() #String format, parameters are dictionary, key corresponds to the parameter name in the string {}, value is the replaced value

1 myStr="Test:{}"

2 print(("456"))3 #Output Test:456

4 myStr="Test1:{param1},Test2 {param2}"

5 print((param2="456",param1=123))6 #Output Test1:123,Test2 456

7 myStr="Test1:{1},Test2 {0}"

8 print(("456",123))9 #Output Test1:123,Test2 456

10 myStr="Test1:{param1},Test2 {param2}"

11 print(myStr.format_map({"param1":123,"param2":"456"}))12 #Output Test1:123,Test2 (list) #The parameter can only be a list of strings, concatenate this list into a string, and add myStr between each element

1 myStr="/"

2 print((['C:','dir1','dir2']))

Output result:

C:/dir1/() #Uppercase letters to lowercase

() #Lowercase letters to uppercase

() #Lowercase to uppercase, uppercase to lowercase

(oldstr,newstr) #Replace

(str) # Remove substrings or characters at both ends of strings, and do not pass parameters to remove spaces by default

() #Only remove the left one

() #Only remove the right one

() #Stands are cut into dictionary based on a character or substring

1 myStr="1+2+3+4"

2 print(("+"))

Output result:

['1', '2', '3', '4']

String slice: Cut a part of the string, just like the list slice below

4. List

Python's list is a bit similar to C++ vector, but it is much more convenient than vector and has a more powerful function.

A list of python can store multiple types of data, and you can put integer, floating point, string and other types of data in the same list, and you can even nest lists and dictionaries, such as:

1  myList=[1,2.12,'3','4',5,[456,789],{"a":123}]

Index: The sequence number of the data in the list starts from 0, that is, the index where 0 is 1 and 1 is 2.12 in myList

Element: The data in the list is an element, each index corresponds to one element, and the corresponding element of index 5 in myList is [456,789]

Add, delete, modify and check:

1 #Add

2 (value) #Insert element value at the end

3 (index,value) #Insert value at index position, move the following element backward

4 #Delete

5 (value) #Delete the first element in the list with value

6 (1) #Delete the first element in the list with value, and if the parameter is not passed, the last element will be deleted by default.

7 del myList[1] #Delete the element with index 1, move the following element forward

8 () #Clear all elements in the list

9 #Modification

10 myList[1]=2 # Change the value of the element indexed to 2

11 #Check

12 myList[1] #get the value of the element with index 1

Slice: Snippet some elements of the list

myList[begin:end] #Intercept the element whose index is begin-end (excluding end), the default value of begin is 0, and the default value of end is the last index of the list +1 (that is, the return value includes the last element)

1 myList=[1,4,3,5,2]2 myList[1:3] #Return [4,3]

3 myList[1:-1] #Returns [4,3,5], index-1 represents the index of the last element of the list

4 myList[2:] #Return [3,5,2]

5 myList[:] or myList[0:] #get all elements

myList[begin::interval] #From the index begin (including begin) every (interval-1), the default value of begin is 0

myList=[1,2,3,4,5,6,7,8,9]

myList[1::1] #Return [1,2,3,4,5,6,7,8,9]

myList[1::2] #Return [1,3,5,7,9]

Other operations:

() #List Flip

()   #The list is sorted from small to large, the elements in the list must be of the same type, the string is sorted by the first letter Ascii code, and the first character is the same, so the second character is sorted according to the second character, and the nested list sorting rules in the list are consistent with the string sorting

(myList2) #Incorporate myList2 in myList

1 myList=[1,2,3]2 myList2=[4,5,6]3 print(myList)4 (myList2)5 print(myList)

Program execution results:

[1, 2, 3]

[1, 2, 3, 4, 5, 6]

Copy: Use "=" directly

In fact, using "=" directly is not copying, it just assigns the address of the list to another variable. Like a pointer in C language, it does not copy more data.

1 myList1=[1,2,3,4,5]2 myList2=myList13 print(myList1)4 print(myList2)5 #The results of the above two prints are [1,2,3,4,5]

6 #Modify the data and print it

7 myList1[1]=08 print(myList1)9 print(myList2)10 #myList1 and myList2 have become [1,0,2,3,4,5] using the copy() method that comes with the list

1 list1=[12,456,789,3658,45]2 list2=() #Copy list1 to list2

3 print(list1)4 print(list2) #You can see that the print result is the same

5 list1[2]=0 #Modify the element whose index list1 is 2, and change 789 to 0

6 list2[2]=1 #Modify the element whose index list1 is 2, and change 789 to 1

7 print(list1)8 print(list2) #Print list1 and list2 respectively

Program execution results:

[12, 456, 789, 3658, 45]

[12, 456, 789, 3658, 45]

[12, 456, 0, 3658, 45]

[12, 456, 1, 3658, 45]

Judging from the execution results, list1 and list2 are independent data. Modifying the data of one list will not affect the data of the other list. But this is not a complete copy, and you can only copy the first layer of the list. If the list is nested in the list, it will be different.

For example:

list1=[12,456,789,[123,456,789],3658,45] #Nest list in list1[123,456,789]

list2=()print(list1)print(list2)

list1[3][1]=0 #Change list[3][1] that is, 456 to 0

print(list1)print(list2)

Program execution results:

[12, 456, 789, [123, 456, 789], 3658, 45]

[12, 456, 789, [123, 456, 789], 3658, 45]

[12, 456, 789, [123, 0, 789], 3658, 45]

[12, 456, 789, [123, 0, 789], 3658, 45]

You can see that the data in list1 has been changed, but the data in list2 has changed, because only the address of the list inside is copied during copying, so when modifying the value of the list inside, both lists will change. copy module

Module: Module is a function written by others. After importing the module, you can use the functions written by others. In fact, it is like the C++ class library. It is to import functions and classes written by others, and then you can use it.

() #This is the same as the copy() method that comes with the list, and can only copy one layer of data. The usage is as follows:

1 import copy #Import module: import module name

2 list1=[12,456,789,3658,45]3 list2=(list1) #Copy the data of list1

() #This is completely copied. No matter how many lists are nested in the list, the data will be copied one more copy. The usage is the same as () list2=list1[:]  #Copy using the list slicing method, shallow copy

list2=list(list1)  #list method, if no parameters are passed, an empty list will be returned, shallow copy

5. Tuples

myTuple=(1,2,3,4,5)   #Tuple is a list that cannot be modified

6. Dictionary

Similar to C language map, each key corresponds to a value

myDict={"name":"Blog Garden","url":"/blog/"}

"name" and "url" are keys, "blog park" and "/blog/" are value, and the corresponding value is obtained through key

Add, delete, modify and check:

1 #Check:

2 myDict["name"] #get it directly, if it does not exist, it will report an error

3 ("name", value)  #If this key does not exist, the value will return, the default value is None

4 ("name") #If this key does not exist, the value is None

5 # Added:

6 myDict["ID"]=5# Assign directly through the new key

7 #Change:

8 myDict["name"]="bokeyyuan" #Assign value through key

9 (key=value) #If key exists, modify it; if it does not exist, create it

10 #Delete:

11 (key)12 delmyDict[key]13 () #Delete one randomly

7. Collection

Create a collection

mySet1={1,2,3,4,5}

mySet2=set([3,4,5,6])

operate:

1 (mySet2) #See Intersection

2 (mySet2) #Search for union

3 (mySet2) #Find the difference set

4 (mySet2) #Judge whether mySet1 is a subset of mySet2

5 (mySet2) #Judge whether mySet1 is a supplementary set of mySet2

6 (mySet2) #Judge whether there is no intersection, return True

7 mySet1.symmetric_difference(mySet2) #Remove the intersection and make up a set

8 (element) #Add elements

9 (set) #Increase elements in batches, parameters can be lists, tuples, and collections

10 (element) #Delete an element