Python3 基础学习笔记 C04【if 语句】
CSDN 課程推薦:《8小時Python零基礎輕松入門》,講師齊偉,蘇州研途教育科技有限公司CTO,蘇州大學應用統計專業碩士生指導委員會委員;已出版《跟老齊學Python:輕松入門》《跟老齊學Python:Django實戰》、《跟老齊學Python:數據分析》和《Python大學實用教程》暢銷圖書。
Python3 基礎學習筆記第四章【if 語句】
目錄
- 【4.1】一個簡單的數列
- 【4.1.1】檢查特定值是否包含在列表當中
- 【4.2】if-else 語句
- 【4.3】if-elif-else 結構
- 【4.3.1】使用多個 elif 代碼塊
- 【4.3.2】省略 else 代碼塊
- 【4.4】測試多個條件
- 【4.5】使用 if 語句處理列表
- 【4.5.1】檢查特殊元素
- 【4.5.2】確定列表不是空的
- 【4.5.3】使用多個列表
【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,輸出結果如下:
請輸入你的年齡查看是否可以去網吧:19You are old enough to go to the net bar!You should go to net bar less,study more! 請輸入你的年齡查看是否可以去網吧:15You 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 = 12if age < 4:price = 0elif age < 18:price = 5else:price = 10print("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!總結
以上是生活随笔為你收集整理的Python3 基础学习笔记 C04【if 语句】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 京东方还有戏吗?曝三星拿下8000万块i
- 下一篇: 巨龙军团来了!《魔兽世界:巨龙时代》开启