python代码书写_Python代码的优雅写法,让代码更简洁
我們都知道,Python 的設計哲學是「優雅」、「明確」、「簡單」。這也許很多人選擇 Python 的原因。但是我收到有些伙伴反饋,他寫的 Python 并不優雅,甚至很臃腫,那可能是你的姿勢不對哦!今天就給大家帶來 Python 語句的十大優雅之法。
為多個變量賦值
有時,有多個變量需要賦值,這時你會怎么賦值呢?
常規方法:
常規方法是給變量逐個賦值。
Python
a = 0
b = 1
c = 2
1
2
3
a=0
b=1
c=2
優雅方法:
直接按順序對應一一賦值。
Python
a,?b,?c?=?0,?1,?2
1
a,?b,?c?=?0,?1,?2
序列解包
需要取出列表中的元素。
常規方法:
一般我們知道可以通過下標獲取具體元素。
Python
info = ['brucepk', 'man', 'python']
name = info[0]
sex = info[1]
tech = info[2]
print(name,sex,tech)
# 結果
brucepk man python
1
2
3
4
5
6
7
8
info=['brucepk','man','python']
name=info[0]
sex=info[1]
tech=info[2]
print(name,sex,tech)
# 結果
brucepkmanpython
優雅方法:
給出對應變量接收所有元素。
Python
info = ['brucepk', 'man', 'python']
name,sex,tech = info
print(name,sex,tech)
# 結果
brucepk man python
1
2
3
4
5
6
info=['brucepk','man','python']
name,sex,tech=info
print(name,sex,tech)
# 結果
brucepkmanpython
優雅你的判斷語句
我們用判斷語句來定義一個絕對值函數。
常規方法:
Python
x = -6
if x < 0:
y = -x
else:
y = x
print(y)
# 結果
6
1
2
3
4
5
6
7
8
9
x=-6
ifx<0:
y=-x
else:
y=x
print(y)
# 結果
6
優雅方法:
Python
x = -6
y = -x if x<0 else x
print(y)
# 結果
6
1
2
3
4
5
6
x=-6
y=-xifx<0elsex
print(y)
# 結果
6
區間判斷
使用 and 連續兩次判斷的語句,條件都符合時才執行語句。
常規方法:
Python
score = 82
if score >=80 and score < 90:
level = 'B'
print(level)
# 結果
B
1
2
3
4
5
6
7
score=82
ifscore>=80andscore<90:
level='B'
print(level)
# 結果
B
優雅方法:
使用鏈式判斷。
Python
score = 82
if 80 <= score < 90:
level = 'B'
print(level)
# 結果
B
1
2
3
4
5
6
7
score=82
if80<=score<90:
level='B'
print(level)
# 結果
B
多個值符合條件判斷
多個值任意一個值符合條件即為 True 的情況。
常規方法:
Python
num = 1
if num == 1 or num == 3 or num == 5:
type = '奇數'
print(type)
# 結果
奇數
1
2
3
4
5
6
7
num=1
ifnum==1ornum==3ornum==5:
type='奇數'
print(type)
# 結果
奇數
優雅方法:
使用關鍵字 in,讓你的語句更優雅。
Python
num = 1
if num in(1,3,5):
type = '奇數'
print(type)
# 結果
奇數
1
2
3
4
5
6
7
num=1
ifnumin(1,3,5):
type='奇數'
print(type)
# 結果
奇數
判斷是否為空
判斷元素是空還是非空。
常規方法:
一般我們想到的是 len() 方法來判斷元素長度,大于 0 則為非空。
Python
A,B,C =[1,3,5],{},''
if len(A) > 0:
print('A 為非空')
if len(B) > 0:
print('B 為非空')
if len(C) > 0:
print('C 為非空')
# 結果
A 為非空
1
2
3
4
5
6
7
8
9
10
A,B,C=[1,3,5],{},''
iflen(A)>0:
print('A 為非空')
iflen(B)>0:
print('B 為非空')
iflen(C)>0:
print('C 為非空')
# 結果
A為非空
優雅方法:
在之前的文章?零基礎學 python 之 if 語句?中講過,if 后面的執行條件是可以簡寫的,只要條件 是非零數值、非空字符串、非空 list 等,就判斷為 True,否則為 False。
Python
A,B,C =[1,3,5],{},''
if A:
print('A 為非空')
if B:
print('B 為非空')
if C:
print('C 為非空')
# 結果
A 為非空
1
2
3
4
5
6
7
8
9
10
A,B,C=[1,3,5],{},''
ifA:
print('A 為非空')
ifB:
print('B 為非空')
ifC:
print('C 為非空')
# 結果
A為非空
多條件內容判斷至少一個成立
常規方法:
用 or 連接多個條件。
Python
math,English,computer =90,80,88
if math<60 or English<60 or computer<60:
print('not pass')
# 結果
not pass
1
2
3
4
5
6
math,English,computer=90,80,88
ifmath<60orEnglish<60orcomputer<60:
print('not pass')
# 結果
notpass
優雅方法:
使用 any 語句。
Python
math,English,computer =90,59,88
if any([math<60,English<60,computer<60]):
print('not pass')
# 結果
not pass
1
2
3
4
5
6
math,English,computer=90,59,88
ifany([math<60,English<60,computer<60]):
print('not pass')
# 結果
notpass
多條件內容判斷全部成立
常規方法:
使用 and 連接條件做判斷。
Python
math,English,computer =90,80,88
if math>60 and English>60 and computer>60:
print('pass')
# 結果
pass
1
2
3
4
5
6
math,English,computer=90,80,88
ifmath>60andEnglish>60andcomputer>60:
print('pass')
# 結果
pass
優雅方法:
使用 all 方法。
Python
math,English,computer =90,80,88
if all([math>60,English>60,computer>60]):
print('pass')
# 結果
pass
1
2
3
4
5
6
math,English,computer=90,80,88
ifall([math>60,English>60,computer>60]):
print('pass')
# 結果
pass
遍歷序列的元素和元素下標
常規方法:
使用 for 循環進行遍歷元素和下標。
Python
L =['math', 'English', 'computer', 'Physics']
for i in range(len(L)):
print(i, ':', L[i])
# 結果
0 : math
1 : English
2 : computer
3 : Physics
1
2
3
4
5
6
7
8
9
L=['math','English','computer','Physics']
foriinrange(len(L)):
print(i,':',L[i])
# 結果
0:math
1:English
2:computer
3:Physics
優雅方法:
使用 enumerate 函數。
Python
L =['math', 'English', 'computer', 'Physics']
for k,v in enumerate(L):
print(k, ':', v)
# 結果
0 : math
1 : English
2 : computer
3 : Physics
1
2
3
4
5
6
7
8
9
L=['math','English','computer','Physics']
fork,vinenumerate(L):
print(k,':',v)
# 結果
0:math
1:English
2:computer
3:Physics
循環語句優化
之前的文章 Python的列表生成式的基本用法 中講過列表生成時的用法,舉例:生成 [1×1,2×2,3×3,4×4,5×5]。
常規方法:
使用簡單的 for 循環可以達到目的。
Python
L = []
for i in range(1, 6):
L.append(i*i)
print(L)
#結果:
[1, 4, 9, 16, 25]
1
2
3
4
5
6
7
L=[]
foriinrange(1,6):
L.append(i*i)
print(L)
#結果:
[1,4,9,16,25]
優雅方法:
使用列表生成式,一行代碼搞定。
Python
print([x*x for x in range(1, 6)])
#結果:
[1, 4, 9, 16, 25]
1
2
3
4
print([x*xforxinrange(1,6)])
#結果:
[1,4,9,16,25]
Python 這些優雅的寫法學會了嗎?自己趕緊動手試試吧。
此文章如果對你有點幫忙的話希望大家能多給點支持,有什么問題歡迎在后臺聯系我,也可以在后臺加入技術交流群,群里有大神,可以一起交流學習。
總結
以上是生活随笔為你收集整理的python代码书写_Python代码的优雅写法,让代码更简洁的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android实现箭头流程列表_反思|A
- 下一篇: python中汉字与变量不可同时出现_P