# 19 pythonic tips

created at 08-11-2021 views: 3

## 1. Multi-variable assignment¶

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


## 2. Variable value exchange¶

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


## 3. Multiple judgments¶

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


## 4. Multi-list synchronization processing¶

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]


## 5. Remove duplicate elements in list¶

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']


## 6. Context Management¶

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!


## 7. Sequence iteration¶

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


## 8. Get list content¶

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
# 1 4


## 9. Convert String List to String¶

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!


## 10. Trinocular Operation¶

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


## 11. Printout format¶

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


## 12. Chain comparison¶

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


## 13. String flip¶

# Flip the string x
x ='olleH'
y = x[::-1]
print(y)
# Hello


## 14. Double list to dictionary format¶

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}


## 15. Iterator generates an array¶

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]


## 16. Switch judgment form¶

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"
print(f'index: {index}, name: {name}')
# ndex: 2, name: Da Ming


## 17. Two-dimensional array traversal¶

# 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


## 18. List cutting¶

# 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]


## 19. Determine whether list is empty¶

# 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.

created at:08-11-2021