Assign values to multiple variables at the same time
# Assign 1, 2, 3 to a, b, and c respectively
a, b, c = 1, 2, 3
print('a={}, b={}, c={}'.format(a, b, c))
# a=1, b=2, c=3
No intermediate variables are used to exchange variable values
# Exchange the values of a, b
a, b = 1, 2
a, b = b, a
print('a={}, b={}'.format(a, b))
# a=2, b=1
Use in
instead of or
to judge
# Determine whether a is equal to one of 1, 2, 3
a = 2
if a in (1, 2, 3):
print(True)
else:
print(False)
# True
Use zip to manipulate the corresponding elements of multiple lists at the same time
# The elements of the same index in the two lists are subtracted into a new list
x, y, z = [10, 20], [1, 2], []
for i, j in zip(x, y):
z.append(i-j)
print(z)
# [9, 18]
Use set to remove duplicate elements in the list
# Remove repeated characters in the list a
x, y = ['a','b','c','a','d'], []
y = list(set(x))
print(x, y, sep='\n')
# ['a','b','c','a','d']
# ['b','a','c','d']
For operations that need to manually release resources at the end, you can put them into the context manager to automatically release resources
# Put the write operation into the context manager
with open('hello.txt','w') as f:
f.write('Hello World!')
# Hello World!
Use enumerate function enumerate to enumerate iterable objects and corresponding indexes
x = ['a', 'b', 'c', 'd', 'e']
for i, j in enumerate(x):
print(i, j)
# 0 a
# 1 b
# 2 c
# 3 d
# 4 e
Take out the elements in the list at once
# Corresponding to take out all elements in the list
x = [1, 2, 3, 4]
x0, x1, x2, x3 = x
print(x0, x1, x2, x3)
# 1 2 3 4
# Take out only the first and last elements of the list
x = [1, 2, 3, 4]
head, *_, tail = x
print(head, tail)
# 1 4
Use the join method to specify the interval to merge the string list into a long string
# Spaces connect the list of strings to grow strings
x = ['Hello','World','!']
y = ''.join(x)
print(y)
# Hello World!
Use if···else···
to achieve trinocular operation
# If a is greater than 10, assign b to true, otherwise false
a = 10
b = True if a> 5 else False
print(b)
# True
Print output with f string to improve code readability
# Print name and age
name ='Xiao Ming'
age = 18
print('name: %s, age: %d'% (name, age))
print('name: {}, age: {}'.format(name, age))
print(f'name: {name}, age: {age}')
# name: Xiao Ming, age: 18
# name: Xiao Ming, age: 18
# name: Xiao Ming, age: 18
Simplify AND Logic in Conditional Judgment
# a Both conditions are met at the same time
a, b = 10, 0
if 5 <a <15:
b =-a
print(b)
# -10
# Flip the string x
x ='olleH'
y = x[::-1]
print(y)
# Hello
Use the dict and zip functions to convert the list into a dictionary format
# The corresponding elements in x and y become dictionary key-value pairs
x = ['name','age']
y = ['Xiao Ming', 18]
z = dict(zip(x, y))
print(z)
# {'name':'Xiao Ming','age': 18}
Generate arrays with iterators
# Generate an even-numbered array within 20
x = [i*2 for i in range(10)]
print(x)
# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Use the get method to implement the switch function
# Find the name according to the index and print it
index = 2
name = {
0: "Xiao Ming",
1: "Er Ming",
2: "Da Ming"
}.get(index, "Not Found")
print(f'index: {index}, name: {name}')
# ndex: 2, name: Da Ming
# Loop through the two-dimensional array and print by line
x = [
['Xiao Ming', 18],
['Er Ming', 19],
['Da Ming', 20]
]
for name, age in x:
print(f'name: {name}, age: {age}')
# name: Xiao Ming, age: 18
# name: Er Ming, age: 19
# name: Da Ming, age: 20
# Cut list elements, the first two, the middle two, and the last two
x = [1, 2, 3, 4, 5, 6]
x1 = x[:2]
x2 = x[2:4]
x3 = x[-2:]
print(x1, x2, x3, sep='\n')
# [1, 2]
# [3, 4]
# [5, 6]
# Directly use the list name as the judgment condition
x = []
if x:
print('x is empty.')
else:
print('x is not empty.')
# x is not empty.