網站首頁 編程語言 正文
1.簡單介紹
每條if語句的核心都是一個值為True
或False
的表達式,這種表達式被稱為條件測試。Python 根據條件測試的值為True還是False
來決定是否執行if語句中的代碼。如果條件測試的值為True,Python就執行緊跟在if語句后面的代碼;如果為False,Python就忽略這些代碼。
要判斷是否相等,我們可以使用==來進行判斷:
car = 'Audi' car.lower() == 'audi'
輸出的結果為:
true
比如說我們在測試用戶的用戶名是否與他人重合的時候我們可以使用到這個判斷。
要判斷兩個值是否不等,可結合使用驚嘆號和等號(!=),其中的驚嘆號表示不,在很多編程語言中都如此:
requested_topping = 'mushrooms' if requested_topping != 'anchovies': ? print("Hold the anchovies!")
輸出的結果為:
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("及格了")
輸出的結果為:
不及格
if-else語句可以演變為if-elif-else語句,用來執行2個以上的條件判斷對執行對應的操作:
num = 85 ? if num < 60: ? print("不及格") elif 60<=num and num<=80: ? print("及格") else: ? print("優秀")
運行的結果為:
優秀
3.用if語句來處理列表
我們可以把if語句和列表相結合:
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")
輸出的結果為:
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?")
運行結果為:
Are you sure you want a plain pizza?
Python語言會在列表至少包含一個元素的時候返回True
,而列表為空的是否返回False
。
當我們有著多個列表的時候,我們可以:
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!")
行結果為:
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
相關推薦
- 2022-10-03 Go?Excelize?API源碼閱讀Close及NewSheet方法示例解析_Golang
- 2022-08-15 springboot實現動態數據源切換
- 2022-04-04 webpack-loaders: postcss
- 2022-04-08 WPF控件模板與其觸發器_基礎應用
- 2022-10-12 golang?執行命令行的實現_Golang
- 2022-11-11 Python實現讀取文件夾按數字排序功能_python
- 2022-05-27 詳解Python實現字典合并的四種方法_python
- 2022-08-20 ORACLE中dbms_output.put_line輸出問題的解決過程_oracle
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支