Python3 自學筆記 C04【if語句】
阿新 • • 發佈:2018-12-17
- 4.1 一個簡單的數列
給定一個汽車列表,將其中每一輛汽車的名稱打印出來,要求列印 ‘bmw’ 時所有字母都要大寫,其餘名稱只需要首字母大寫:
cars = ['audi' , 'bmw' , 'subaru' , 'toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
輸出結果如下:
Audi
BMW
Subaru
Toyota
- 4.1.1 檢查特定值是否包含在列表當中
要判斷特定的值是否已包含在列表當中,可使用關鍵字 in
user_names = ['andia' , 'david' , 'liwa']
user = 'andia'
if user in user_names:
print(user.title() + "is in user_name.")
輸出結果如下:
Andiais in user_name.
要判斷特定的值是否不包含在列表當中,可使用關鍵字 not in
user_names = ['andia' , 'david' , 'liwa']
user = 'kivle'
if user not in user_names:
print(user.title( ) + "is not in user_name.")
輸出結果如下:
Kivleis not in user_name.
- 4.2 if-else 語句
age = input("請輸入你的年齡檢視是否可以去網咖:")
if int(age) >= 18:
print("You are old enough to go to the net bar!")
print("You should go to net bar less,study more!")
else:
print("You are too young to go to the net bar!" )
print("Wait until you are 18 to go to the net bar!")
分別輸入19和15,輸出結果如下:
請輸入你的年齡檢視是否可以去網咖:19
You are old enough to go to the net bar!
You should go to net bar less,study more!
請輸入你的年齡檢視是否可以去網咖:15
You are too young to go to the net bar!
Wait until you are 18 to go to the net bar!
- 4.3 if-elif-else 結構
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
else:
price = 10
print("Your admission cost is $" + str(price) + ".")
輸出結果如下:
Your admission cost is $5.
- 4.3.1 使用多個 elif 程式碼塊
age = 20
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 15
else:
price = 10
print("Your admission cost is $" + str(price) + ".")
輸出結果如下:
Your admission cost is $15.
- 4.3.2 省略 else 程式碼塊
Python並不要求 if-elif 結構後面必須有 else 程式碼塊:
age = 20
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 15
elif age >= 65:
price = 10
print("Your admission cost is $" + str(price) + ".")
輸出結果仍與3.3.1一樣
- 4.4 測試多個條件
if-elif-else結構功能強大,但僅適用於只有一個條件滿足的情況:遇到通過了的測試後,Python就會跳過餘下的測試:
names = ['Zhangshan' , 'Wanger']
if 'Zhangshan' in names:
print("Zhangshan is here!")
if 'Wanger' in names:
print("Wanger is here!")
if 'Xiaoming' in names:
print("Xiaoming is here!")
print("All the students are here!")
輸出結果如下:
Zhangshan is here!
Wanger is here!
All the students are here!
相同的程式,如果使用 if-elif-else 結構,程式碼將不能正確執行:
names = ['Zhangshan' , 'Wanger']
if 'Zhangshan' in names:
print("Zhangshan is here!")
elif 'Wanger' in names:
print("Wanger is here!")
elif 'Xiaoming' in names:
print("Xiaoming is here!")
print("All the students are here!")
輸出結果如下:
Zhangshan is here!
All the students are here!
總之,如果我們只想執行一個程式碼塊,就使用 if-elif-else 結構;如果要執行多個程式碼塊,就必須使用一系列獨立的 if 語句!
- 4.5 使用 if 語句處理列表
- 4.5.1 檢查特殊元素
對3.4例子改版,加入姓名 ‘Xiaoming’,當檢索到Xiaoming時告訴他,他媽媽叫他回家吃飯
names = ['Zhangshan' , 'Wanger' , 'Xiaoming']
for name in names:
if name == 'Xiaoming':
print("Xiaoming,Your mother told you to go home for dinner!")
else:
print(name +"is here!")
print("All the students are here!")
輸出結果如下:
Zhangshanis here!
Wangeris here!
Xiaoming,Your mother told you to go home for dinner!
All the students are here!
- 4.5.2 確定列表不是空的
在檢索姓名前檢查姓名是否為空,不為空則打印出所有姓名,為空則提示沒有姓名:
names = []
if names:
for name in names:
print(name +" is here!")
print("All the students are here!")
else:
print("There is no students!")
輸出結果如下:
There is no students!
在if語句中將列表名用在條件表示式中時,Python將在列表至少包含一個元素時返回Ture,並在列表為空時返回False
- 4.5.3 使用多個列表
兩個列表names_1和names_2,要求輸出既在names_2中又在names_1中的元素:
names_1 = ['Zhangshan' , 'Liyang' , 'Wanger' , 'Tangyang' , 'Xiaoming']
names_2 = ['Liyang' , 'Zhangwei' , 'Tangyang']
for names in names_2:
if names in names_1:
print(names +" is here!")
print("All the students are here!")
輸出結果如下:
Liyang is here!
Tangyang is here!
All the students are here!