文章目錄
- 1. 多變數賦值
- 2. 變數值交換
- 3. 多項判斷
- 4. 多串列同步處理
- 5. 去除串列重復元素
- 6. 背景關系管理
- 7. 序列迭代
- 8. 獲取串列內容
- 9. 字串串列轉字串
- 10. 三目運算
- 11. 列印輸出的格式
- 12. 鏈式比較
- 13. 字串翻轉
- 14. 雙串列轉字典格式
- 15. 迭代器生成陣列
- 16. switch 判斷形式
- 17. 二維陣列遍歷
- 18. 串列切割
- 19. 判斷串列是否為空
1. 多變數賦值
同時為多個變數進行賦值
# 分別為 a, b, c 賦值 1, 2, 3
a, b, c = 1, 2, 3
print('a={}, b={}, c={}'.format(a, b, c))
# a=1, b=2, c=3
2. 變數值交換
不用中間變數交換變數的值
# 交換 a, b 的值
a, b = 1, 2
a, b = b, a
print('a={}, b={}'.format(a, b))
# a=2, b=1
3. 多項判斷
用 in 代替 or 進行判斷
# 判斷 a 是否等于 1, 2, 3 中的某一個
a = 2
if a in (1, 2, 3):
print(True)
else:
print(False)
# True
4. 多串列同步處理
用 zip 同時操作多個串列的對應元素
# 兩個串列中同索引元素相減成新串列
x, y, z = [10, 20], [1, 2], []
for i, j in zip(x, y):
z.append(i - j)
print(z)
# [9, 18]
5. 去除串列重復元素
用 set 去除串列中重復的元素
# 去除串列中重復出現的字符 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. 背景關系管理
對于結束需要手動釋放資源的操作可以放入背景關系管理器自動釋放資源
# 將寫操作放入背景關系管理器中
with open('hello.txt', 'w') as f:
f.write('Hello World!')
# Hello World!
7. 序列迭代
用列舉函式 enumerate 列舉可迭代物件和對應索引
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. 獲取串列內容
將串列中的元素一次取出
# 對應取出串列中所有元素
x = [1, 2, 3, 4]
x0, x1, x2, x3 = x
print(x0, x1, x2, x3)
# 1 2 3 4
# 只取出串列首尾元素
x = [1, 2, 3, 4]
head, *_, tail = x
print(head, tail)
# 1 4
9. 字串串列轉字串
用 join 方法指定間隔合并字串串列為長字串
# 間隔空格將字串串列連接成長字串
x = ['Hello', 'World', '!']
y = ' '.join(x)
print(y)
# Hello World !
10. 三目運算
用 if···else··· 實作三目運算
# 如果 a 大于 10 賦值給 b 真,否則假
a = 10
b = True if a > 5 else False
print(b)
# True
11. 列印輸出的格式
用 f 字串列印輸出,提高代碼可讀性
# 列印名字和年齡
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. 鏈式比較
簡化條件判斷中的與邏輯
# a 同時滿足兩個條件
a, b = 10, 0
if 5 < a < 15:
b = - a
print(b)
# -10
13. 字串翻轉
# 將字串 x 進行翻轉
x = 'olleH'
y = x[::-1]
print(y)
# Hello
14. 雙串列轉字典格式
用 dict 和 zip 函式將串列對應轉為字典格式
# x 和 y 中對應元素變為字典鍵值對
x = ['name', 'age']
y = ['Xiao Ming', 18]
z = dict(zip(x, y))
print(z)
# {'name': 'Xiao Ming', 'age': 18}
15. 迭代器生成陣列
用迭代器生成陣列
# 生成 20 以內偶數陣列
x = [i*2 for i in range(10)]
print(x)
# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
16. switch 判斷形式
用 get 方法實作 switch 功能
# 根據索引找到姓名并列印
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
17. 二維陣列遍歷
# 回圈遍歷二維陣列并按行列印
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. 串列切割
# 切割串列元素,前兩個,中間兩個,后兩個
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. 判斷串列是否為空
# 直接將串列名作為判斷條件
x = []
if x:
print('x is empty.')
else:
print('x is not empty.')
# x is not empty.

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/292962.html
標籤:python
