网站首页 > 基础教程 正文
大家好,今天我们将讨论Python中的if、if elif和if elif else条件语句,并尝试了解不同的用例和编写相同条件语句的不同方式。
if, if elif, and if elif else is 到底是什么鬼?
在 Python 中,要根据满足某些条件来执行语句,我们使用 if 语句。
让我们看几个例子:-
示例 1:-
going_to_office = True #variable that stores the boolean value
if going_to_office == True: print("Work from office")
这里不用检查 going_to_office 的值是否为 True,我们可以直接写成
if going_to_office: print("Work from office")
因此,如果条件被评估为 True 则 print 语句将被执行,否则不执行。
示例 2:-
让我们说我们有
going_to_office = True
if going_to_office:
print("Work from office")
else:
print("Enjoy work from home")
因此,如果不满足第 1 行的条件,则控制移至 else 并执行 else 条件下的语句。
示例 3:-
假设我们有 if elif else 条件
going_to_office = False
be_at_home = True
if going_to_office: #condition1
print(“Work from office”)
elif be_at_home: #condition2
print(“Enjoy work from home”)
else:
print(“Work from Starbucks”)
所以如果条件1和条件2不满足,则执行else条件下的语句。
结论
if、if elif 和 if elif else 等基本条件语句很容易理解,所以请告诉我您对本文的看法。
在此先感谢您阅读本文……
关注七爪网,获取更多APP/小程序/网站源码资源!
- 上一篇: 你或许并不需要使用 if else
- 下一篇: Javascript中的分支语句if-else
猜你喜欢
- 2024-11-20 一文搞懂Golang条件判断:if-else语句详解
- 2024-11-20 无需If-Else语句,状态模式即可编写干净可维护的代码
- 2024-11-20 答应我,别再if/else走天下了可以吗
- 2024-11-20 如何对SpringBoot中的IF-ELSE语句进行优化?
- 2024-11-20 道哥说编程--Java流程控制语句if--else详解
- 2024-11-20 if-elif-else,三目运算符,while条件循环,for迭代循环
- 2024-11-20 设计模式:策略模式避免多重分支语句(ifelse)
- 2024-11-20 Excel VBA流程图解之IF语句,多层IF嵌套,其实很简单
- 2024-11-20 4.2 练习编写简单的if语句和else语句
- 2024-11-20 C/C++编程笔记:if—else语句块,有个细节需要注意
- 最近发表
- 标签列表
-
- jsp (69)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- tail-f (79)
- queryselectorall (63)
- location.search (79)
- bootstrap教程 (74)
- deletesql (62)
- linuxgzip (68)
- 字符串连接 (73)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)