Common data types in Python are:

  • Int plastic
  • Bool Indicates the Boolean type
  • The STR string
  • Float float
  • List the list
  • Dict dictionary
  • The set collection
  • The tuple tuples

This article explains lists, tuples, and their common methods.


List 1.

You’re familiar with lists. Lists in Python are the C equivalent of arrays. Is an ordered, mutable container that can hold multiple elements of different types.

1.1 define

number_list = [98.88.Awesome!.12, -1]
data_list = [1.True."abc"."Strong"] A list can hold multiple different types of elements
Copy the code

1.2 Unique Functions

Lists are used so often in Python that there are many list-specific methods in Python. For reasons of space, only common methods are covered here, and you can refer to the Official Python documentation for more

1. Append, append the value to the end of the original list. (append method)

data_list = []
data_list.append("abc")
print(data_list) # ["abc"]
Copy the code

2. Add data in batches. (extend method)

tools = ["Move brick"."Kitchen"."Hammer"]
tools.extend( [11.22.33])The values in # weapon are appended to tools one by one
print(tools) # # # # # # # # #

Note: The extend method argument here is not necessarily a list, but an iterable will do the trick.
tools = ["Move brick"."Kitchen"."Hammer"]
tools.extend( (11.22.33))# (11,22,33) is a tuple
print(tools) # # # # # # # # #
Copy the code

3. Insert: Inserts a value in the specified index position of the original list. (Insert method)

user_list = ["a"."b"]
user_list.insert(0."c")
print(user_list) # ["c","a","b"]
Copy the code

4. Delete the value from the original list. (remove method)

user_list = ["a"."b"."c"."d"]
user_list.remove("a")
print(user_list) # ["b","c","d"]
# note: Delete from left to right to find the first delete, and there is no error, so use caution!
Copy the code

5. Kick an element from the original list based on the index (pop method)

user_list = ["a"."b"."c"."d"."e"]
0, 1, 2, 3, 4
user_list.pop(1)
print(user_list) # ["a","c","d","e"]

Delete the last one by default
user_list.pop()
print(user_list) # ["a","c","d"]
Copy the code

6. Clear the original list (clear method)

user_list = ["a"."b"."c"]
user_list.clear()
print(user_list) # []
Copy the code

7. Get index by value (index)

user_list = ["a"."b"."c"."d"."e"]
0, 1, 2, 3, 4
index = user_list.index("a")
print(index) # 0
# note: the index method is used to find the first element from left to right, and there is no error in it, so use it with caution!
Copy the code

8. Sort list elements (sort method)

# number sort
num_list = [11.22.4.5.11.99.88]
num_list.sort()  Sort num_list from smallest to largest
num_list.sort(reverse=True)  # # Sort num_list from largest to smallest

# For strings, the program disassembles the strings and converts them to numbers according to some code table before comparing them

# Note: An error is reported when internal elements cannot be compared when sorting.
Copy the code

9. Reverse the original list (reverse method)

user_list = [11.22.33]
user_list.reverse()

print(user_list) # 33,22,11] [
Copy the code

1.3 Public Functions

1. Add the two lists to generate a new list.

data = ["Zhao four"."Liu"] + ["Song Xiaofeng"."Van der Biao"]
print(data) # # # # # # # # # # #

v1 = ["Zhao four"."Liu"]
v2 = ["Song Xiaofeng"."Van der Biao"]
v3 = v1 + v2
print(v3) # # # # # # # # # # #
Copy the code

2. Multiply the list * integer to create N copies of the elements in the list and generate a new list.

data = ["Zhao four"."Liu"] * 2
print(data) # # # # # # # # # #

v1 = ["Zhao four"."Liu"]
v2 = v1 * 2
print(v1) # # # # # # # # #
print(v2) # # # # # # # # # #
Copy the code

3. Since the list is composed of multiple elements, you can use IN to determine whether an element is in the list.

user_list = ["Dog"."The egg"."Sand"."You"] 
result = "You" in user_list
# result = "you" not in user_list
print(result) # True

When checking the existence of the elements in the list, we compare them one by one, which is inefficient.
Copy the code
# Case study: Sensitive word substitution
text = input("Please enter text content:") 
forbidden_list = ["Grass"]
for item in forbidden_list:
    text = text.replace(item,"* *")
print(text)
Copy the code

4. Get the length

user_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4']
print( len(user_list) ) # 3
Copy the code

5. Index, an operation on an element

# read
user_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4']
print( user_list[0])print( user_list[2])print( user_list[3])# error
Copy the code
# change
user_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4']
user_list[0] = "Wu Peiqi"
print(user_list) # [" Wu Peiqi "," Liu Huaqiang ",' Nicholas Zhao Si ']
Copy the code
# delete
user_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4']
del user_list[1]

user_list.remove("Liu Huaqiang")
ele = user_list.pop(1)
# Note: An error will be reported if the index is out of range.
Copy the code

6. Slicing, multi-element operation (rarely used)

# read
user_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4']

print( user_list[0:2])# # # # # # # # # # #
print( user_list[1:)print( user_list[:-1])Copy the code
# change
user_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4']
user_list[0:2] = [11.22.33.44]
print(user_list) # output [11, 22, 33, 44, 'Nicholas zhao si ']

user_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4']
user_list[2:] = [11.22.33.44]
print(user_list) # output [' Fan Debiao ', 'Liu Huaqiang ', 11, 22, 33, 44]

user_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4']
user_list[3:] = [11.22.33.44]
print(user_list) # output [' Fan Debiao ', 'Liu Huaqiang ',' Nicholas Zhao Si ', 11, 22, 33, 44]


user_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4']
user_list[10000:] = [11.22.33.44]
print(user_list) # output [' Fan Debiao ', 'Liu Huaqiang ',' Nicholas Zhao Si ', 11, 22, 33, 44]


user_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4']
user_list[-10000:1] = [11.22.33.44]
print(user_list) [11, 22, 33, 44, 'Liu Huaqiang ',' Nicholas Zhao si ']
Copy the code
# delete
user_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4']
del user_list[1:]
print(user_list) # output [' van der Biao ']
Copy the code

Step 7. Long

user_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4'.Song Xiaobao."Liu"]
0, 1, 2, 3, 4
print( user_list[1:4:2])print( user_list[0: :2])print( user_list[1: :2])print( user_list[4:1: -1])Copy the code
# Example: Implement list flipping
user_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4'.Song Xiaobao."Liu"]
new_data = user_list[::-1]
print(new_data) # [" liu Neng "," Song Xiaobao "," Nicholas Zhao Si "," Liu Huaqiang "," Fan Debiao "]


data_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4'.Song Xiaobao."Liu"]
data_list.reverse()
print(data_list) # [" liu Neng "," Song Xiaobao "," Nicholas Zhao Si "," Liu Huaqiang "," Fan Debiao "]
Copy the code

8. The for loop

user_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4'.Song Xiaobao."Liu"]
for item in user_list:
print(item)
Copy the code
user_list = ["Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4'.Song Xiaobao."Liu"]

for index in range( len(user_list) ):
    item = user_index[index]
    print(item)
Copy the code

1.4 conversion

  • Int, bool cannot be converted to a list

  • str

    name = "Wu Peiqi"
    
    data = list(name)  # [" wU "," Pei "," Qi "]
    print(data)
    Copy the code
  • Tuplp, set

    v1 = (11.22.33.44) # tuples
    vv1 = list(v1)     # list,22,33,44 [11]
    
    v2 = {"abc"."eric"."dsb"} # set
    vv2 = list(v2) # list [" acb ", "Eric," "DSB"]
    Copy the code

1.5. Other

1. The nested

Lists are containers that can store all kinds of data, so they also support nesting of lists, such as:

data = [ "Xie Guangkun"["Haiyan"."Zhao Benshan"].True[11.22[999.123].33.44].Song Xiaobao ]
Copy the code

For nested values, you can learn from the index points you learned earlier, for example:

data = [ "Xie Guangkun"["Haiyan"."Zhao Benshan"].True[11.22.33.44].Song Xiaobao ]

print( data[0])# "Xie Guangkun"
print( data[1])# [" Haiyan "," Zhao Benshan "]
print( data[0] [2])# "kun"
print( data[1] [-1])# zhao Benshan
Copy the code

2. A tuple

A tuple is an ordered, immutable container that can hold multiple elements of different types.

How do you make immutable? Remember one sentence: “My son can never be someone else, but my son can grow up.”

2.1 define

d1 = (1)  # 1
d2 = (1.)# (1,)
d3 = (1.2)
# Note: It is recommended to add a comma at the end of a tuple to indicate that it is a tuple.
Copy the code

2.2 Unique Functions

There is no

2.3 Public Functions

1. Add the two lists to generate a new tuple.

data = ("Zhao four"."Liu") + ("Song Xiaofeng"."Van der Biao")
print(data) # (" Zhao Si "," Liu Neng "," Song Xiaofeng "," Fan Debiao ")

v1 = ("Zhao four"."Liu")
v2 = ("Song Xiaofeng"."Van der Biao")
v3 = v1 + v2
print(v3) # (" Zhao Si "," Liu Neng "," Song Xiaofeng "," Fan Debiao ")
Copy the code

2. Multiply, list * integer creates N copies of the elements in the list and generates a new tuple.

data = ("Zhao four"."Liu") * 2
print(data) # (" zhao Si "," Liu Neng "," Zhao Si "," Liu Neng ")

v1 = ("Zhao four"."Liu")
v2 = v1 * 2
print(v1) # (" Zhao Si "," Liu Neng ")
print(v2) # (" zhao Si "," Liu Neng "," Zhao Si "," Liu Neng ")
Copy the code

3. Get the length

user_list = ("Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4'.)print( len(user_list) ) # 3
Copy the code

Index of 4.

user_list = ("Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4'.)print( user_list[0])# "Vanderbiao"
print( user_list[2])# "Liu Huaqiang"
print( user_list[3])# "Nicholas Zhao 4"
Copy the code

5. Slice

user_list = ("Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4'.)print( user_list[0:2])# (" Fan Debiao "," Liu Huaqiang ")
print( user_list[1:)# (" Liu Huaqiang ",' Nicholas Zhao Si ')
print( user_list[:-1])# (" Fan Debiao "," Liu Huaqiang ")
Copy the code

Step 6. Long

user_list = ("Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4'.Song Xiaobao."Liu")
print( user_list[1:4:2])# (" Liu Huaqiang "," Song Xiaobao ")
print( user_list[0: :2])# (" Fan Debiao ", "Nicholas Zhao 4 "," Liu Neng ")
print( user_list[1: :2])# (" Liu Huaqiang "," Song Xiaobao ")
print( user_list[4:1: -1]) # (" Liu Neng "," Song Xiaobao ",' Nicholas Zhao Si ')
Copy the code

7. The for loop

user_list = ("Van der Biao"."Liu Huaqiang".'Nicholas Zhao 4'.Song Xiaobao."Liu")
for item in user_list:
	print(item)
Copy the code

2.4 conversion

Other types are converted to tuples. With tuples (other types), only strings and lists can be converted to tuples.

data = tuple(other)# str / list 
Copy the code
name = "Hello?"
data = tuple(name)
print(data) # output (" you "," ok "," yay ")
Copy the code
name = ["Wu".18."python"]
data = tuple(name)
print(data) # output (" wu ",18,"python")
Copy the code

2.5 other

1. The nested

Because both tuples and lists can act as containers, they can hold many elements inside them and support various nested elements within them.

tu = ( 'a'.'b', ('c'.'d') )
tu1 = tu[0]
tu2 = tu[1]
tu3 = tu[2] [0]
tu4 = tu[2] [1]
tu5 = tu[2] [1] [3]

print(tu1) # a
print(tu2) # b
print(tu3) # c
print(tu4) # d
print(tu5) # Raise (IndexError) error
Copy the code

The basic data types of Python are as follows: