網(wǎng)站首頁 編程語言 正文
1.簡單介紹
每條if語句的核心都是一個值為True
或False
的表達式,這種表達式被稱為條件測試。Python 根據(jù)條件測試的值為True還是False
來決定是否執(zhí)行if語句中的代碼。如果條件測試的值為True,Python就執(zhí)行緊跟在if語句后面的代碼;如果為False,Python就忽略這些代碼。
要判斷是否相等,我們可以使用==來進行判斷:
car = 'Audi' car.lower() == 'audi'
輸出的結(jié)果為:
true
比如說我們在測試用戶的用戶名是否與他人重合的時候我們可以使用到這個判斷。
要判斷兩個值是否不等,可結(jié)合使用驚嘆號和等號(!=),其中的驚嘆號表示不,在很多編程語言中都如此:
requested_topping = 'mushrooms' if requested_topping != 'anchovies': ? print("Hold the anchovies!")
輸出的結(jié)果為:
Hold the anchovies!
如果需要對多個條件進行比較,則可以使用and和or兩個符號:
num1 = 15 num2 = 20 ? num3 = 25 num4 = 30 ? if num1 == 15 and num2 == 20: ? print("All Right") ? if num3 == 25 or num4 == 40: ? print("One of them is right")
and需要多個條件同時成立才能夠成立,而or只需要一個條件成立就能夠成立。
2.if-else語句
最簡單的if語句只有一個測試和一個操作,但是使用了if-else語句之后便可以有兩個操作:
num = 50 ? if num < 60: ? print("不及格") else: ? print("及格了")
輸出的結(jié)果為:
不及格
if-else語句可以演變?yōu)閕f-elif-else語句,用來執(zhí)行2個以上的條件判斷對執(zhí)行對應(yīng)的操作:
num = 85 ? if num < 60: ? print("不及格") elif 60<=num and num<=80: ? print("及格") else: ? print("優(yōu)秀")
運行的結(jié)果為:
優(yōu)秀
3.用if語句來處理列表
我們可以把if語句和列表相結(jié)合:
food_list = ['apple', 'banana','orange'] ? for food in food_list: ? if food == 'apple': ? ? print("Apple is here") ? elif food == 'bana': ? ? print("Banana is here") ? else: ? ? print("Orange is here")
輸出的結(jié)果為:
Apple is here
Orange is here
Orange is here
或者我們可以用來檢測列表是否為空:
requested_toppings = [] if requested_toppings: ? for requested_topping in requested_toppings: ? ? print("Adding " + requested_topping + ".") ? print("\nFinished making your pizza!") else: ? print("Are you sure you want a plain pizza?")
運行結(jié)果為:
Are you sure you want a plain pizza?
Python語言會在列表至少包含一個元素的時候返回True
,而列表為空的是否返回False
。
當(dāng)我們有著多個列表的時候,我們可以:
available_toppings = ['mushrooms', 'olives', 'green peppers','pepperoni', 'pineapple', 'extra cheese'] requested_toppings = ['mushrooms', 'french fries', 'extra cheese'] ? for requested_topping in requested_toppings: ? if requested_topping in available_toppings: ? ? print("Adding " + requested_topping + ".") ? else: ? ? print("Sorry, we don't have " + requested_topping + ".") ? print("\nFinished making your pizza!")
行結(jié)果為:
Adding mushrooms.
?
Finished making your pizza!
Sorry, we don't have french fries.
?
Finished making your pizza!
Adding extra cheese.
?
Finished making your pizza!
原文鏈接:https://blog.csdn.net/weixin_56659172/article/details/123042350
相關(guān)推薦
- 2023-11-16 當(dāng)pytorch找不到 CUDA 時,如何修復(fù)錯誤:版本 libcublasLt.so.11 未在帶
- 2021-11-06 C語言?如何用堆解決Topk問題_C 語言
- 2022-10-15 C語言詳細分析不同類型數(shù)據(jù)在內(nèi)存中的存儲_C 語言
- 2022-09-17 Python高效處理大文件的方法詳解_python
- 2022-05-06 詳解go語言中sort如何排序_Golang
- 2022-05-10 V-for中通過變量+索引實現(xiàn)單獨控制
- 2022-12-24 VS2019調(diào)試C語言程序(監(jiān)視操作)的詳細步驟_C 語言
- 2022-03-03 GitHub 私人private倉庫添加成員(協(xié)作者Collaborators)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支