1、 Join operators
print([1,2]+[2,3])
print([1,2]*3)
# result
[1, 2, 2, 3]
[1, 2, 1, 2, 1, 2]
2、 Member operators
print(1 in [1,2,3])
print(0 in ['a',False,[1,2]])
# result
True
True $ Because here bool It's worth it False=0,Ture=1
3 、 Indexes
li = [1,2,3,[1,'b',3]]
print(li[0])
print(li[-1])
print(li[-1][0])
# result
1
[1, 'b', 3]
1
4、 section
li = ['172','25','254','100']
print(li[:2])
print(li[1:])
print(li[::-1])
# result
['172', '25']
['25', '254', '100']
['100', '254', '25', '172']
5、 Exercises
Will list li = [‘172’,‘25’,‘254’,‘100’] Output is 100-254-25
# Method 1
li = ['172','25','254','100']
print('-'.join(li[1:][::-1]))
# Method 2
li = ['172','25','254','100']
print('-'.join(li[3:0:-1])) $ Here is the reverse order , It can be seen as (0:3], Then reverse the order
6、 for loop
names = [" fans "," Vermicelli "," Pink Ribbon "]
for name in names:
print(f'{
name}')
# result
fans
Vermicelli
Pink Ribbon
1、 Additional
li = [1,2,3]
li.append(4)
print(li)
# result
[1, 2, 3, 4]
2、 Append at the beginning of list
li= [1,2,3]
li.insert(0."cat")
print(li)
# result
['cat', 1, 2, 3]
3、 Add... In the middle of the list
li= [1,2,3]
li.insert(2,“cat”)
print(li)
# result
[1, 2, ‘cat’, 3]
4、 Append multiple at a time
li= [1,2,3]
li.extend([4,5,6])
print(li)
# result
[1, 2, 'cat', 3]
1、 modify , By indexing or slicing
Method 1 through the index
li= [1,2,3]
li[0]='hello'
li[1]='westos'
print(li)
# result
['hello', 'westos', 3]
Method 2 by slicing
li= [1,2,3]
li[:2]=['cat','westos']
print(li)
# result
['cat', 'westos', 3]
3、 see : View elements by indexing and slicing . View index values and occurrences
li=[1,2,2,2,3,4]
print(li.count(1)) $ Count the times
print(li.index(3)) $ Address of statistical index
# result
1
4
1、 Delete... According to index
pop It stands for exploring the meaning of pop-up
li = [1,2,3]
print(li.pop(1))
print(li)
# result
2
[1, 3]
2、 according to value Value delete
li = [1,2,3]
li.remove(3)
print(li)
# result
[1, 2]
3、 Empty it all
li = [1,2,3]
li.clear()
print(li)
# result
[]
4、 reverse
li = [1,2,3,4]
li.reverse()
print(li)
# result
[4, 3, 2, 1]
5, Sort
li = [1,3,7,4]
li.sort()
print(li)
# result
[1, 3, 4, 7]
1、 Creation of tuples
An empty tuple
t1=()
print(t1,type(t1))
# result
() <class 'tuple'>
Be careful : A single element is not a tuple
t2=(1)
print(t2,type(t2))
# result
1 <class 'int'>
Tuples can have many types
t3=(1,2,True,[1,2,3,4])
print(t3,type(t3))
# result
(1, 2, True, [1, 2, 3, 4]) <class 'tuple'>
print((1,2,3)+(3,)) Splicing
print((1,2,3)*3) repeat
print(3 in (1,2,3)) Judge
# result
(1, 2, 3, 3)
(1, 2, 3, 1, 2, 3, 1, 2, 3)
True
t=(1,2,3)
print(t[0]) Indexes
print(t[-1])
print(t[::-1]) reverse
print(t[1:3]) Fragmentation
# result
1
3
(3, 2, 1)
(2, 3)
Elements are immutable data
t = (1,23,3,424,2,2,2,3)
print(t.count(2))
print(t.index(2))
# result
3
4
from collections import namedtuple from collections Import from the toolbox namedtuple Tools
User = namedtuple('User',('name','age','city')) Use namedtuple Create a named tuple object User
User1 = User('westos','18','xian')
print(User1)
print(User1.name)
print(User1.age)
print(User1.city)
# result
User(name='westos', age='18', city='xian')
westos
18
xian
5、 ... and 、 Judge
python Language :==:
Is to judge whether the type and value are equal is:
Check if the addresses are the same
print(1 == "1")
# result
False Because of the different types
li = [1,2,3]
li1= li.copy()
print(li==li1)
# result
Ture
li = [1,2,3]
li1= li.copy()
print(li==li1)
print(li is li1)
print(id(li),id(li1))
# result
False Because of the different addresses
1956102874048 1956102814272
1. The value of the reference
Because it points to the same storage address , All the same
nums1 = [1,2,3]
nums2 = nums1
nums1.append(4)
print(nums2)
# result
[1, 2, 3, 4]
2、 Variable data type and immutable data type
Variable data type :list
Immutable data types ;str,tuple,namedtuple
3、 Shallow copy
The copies are stored in different addresses
.copy and [:] It's all shallow copies
n1 = [1,2,3]
n2 = n1.copy()
print(id(n1),id(n2))
n1.append(4)
print(n2)
# result
1800894875712 1800894754048
[1, 2, 3]
3、 Shallow copy supplement
Be careful : If there are nested lists , If you want to copy , Be sure to use a deep copy
n1 = [1,2,[1,2]]
n2 = n1.copy()
n1[-1].append(4)
print(n2)
print(id(n1),id(n2))
print(id(n1[-1]),id(n2[-1]))
# result
[1, 2, [1,2,4]]
1800894881024 1800894875712
1800894746432 1800894746432