Python Else-If 语句示例
条件语句有助于决策制定,是所有编程语言中的核心概念。
在本文中,你将学习如何在 Python 中编写条件语句。
具体来说,你将学习如何在 Python 中编写 if
、if else
和 elif
(也称为 else if
)语句。
以下是我们将介绍的内容:
- 什么是
if
语句 if
语句的语法if
语句示例- 什么是
if else
语句 if else
语句示例- 什么是
elif
语句 elif
语句示例
Python 中的 if 语句是什么
if
语句也叫作条件语句,条件语句是决策的主要内容。
条件语句基于检查或比较采取特定操作。
总而言之,if
语句根据条件做出决定。
条件是一个布尔表达式。布尔表达式只能是两个值之一——True
或 False
。
因此,本质上,if
语句是:“当且仅当此条件评估为 True
时才运行以下代码一次。否则根本不运行此代码,忽略它并完全跳过它” .
如何在 Python 中创建 if 语句——语法分解
Python 中 if
语句的一般语法如下:
if expression:
#run this code if expression evaluates to True
code statement(s)
让我们分解一下:
- 使用
if
关键字启动if
语句。 - 你留下一个空格,然后添加一个布尔值。布尔值将是计算结果为
True
或False
的表达式。 - 然后添加一个冒号,
:
。 - 在新行上,添加一级缩进。许多代码编辑器会自动为你执行此操作。例如,当使用带有 Python 扩展的 Visual Studio Code 编辑器时,在你写上上一步中的冒号并按
Enter
后,它会自动以正确的缩进级别缩进你的代码。这种缩进级别是 Python 如何知道你将编写的代码语句与if
语句相关联的方式。 - 最后,编写任意行代码语句。当且仅当表达式的计算结果为 True 时,这些行才会运行。如果表达式的计算结果为 False,它们将不会运行。
Python 中 if 语句的示例
接下来,让我们看一个 if
语句的例子。
我想提示用户输入他们最喜欢的编程语言,并将他们的答案存储在一个名为 language
的变量中。
language = input("Please enter your favorite programming language: ")
然后,我会设置一个条件。
如果用户输入 Python 作为他们最喜欢的语言,那么只在这种情况下,我想向控制台打印一条消息,说明这是正确的答案。
因此,条件将检查变量 language
中存储的值是否等于 Python
。
为此,你使用相等运算符(==
)检查存储在变量 language
中的值是否等于字符串 Python
。
language = input("Please enter your favorite programming language: ")
if language == "Python":
print("Correct! Of course it is Python!")
我运行我的代码,当出现 “Please enter your favorite programming language:” 提示时,我输入了 Python
。
然后我得到以下输出:
# output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
条件(language == "Python"
)为 True
,因此执行 if
语句中的代码。
如果我重新运行我的程序并输入不同的编程语言,将不会有任何输出,因为条件将为 False
。
if
语句中的代码不会运行,if
语句将被完全跳过:
#output
# Please enter your favorite programming language: Java
此时,还值得一提的是,你应该确保在 if
语句中缩进代码。如果你忘记缩进该打印语句,那么你会收到以下缩进错误:
language = input("Please enter your favorite programming language: ")
if language == "Python":
# Don't do this!
print("Correct! Of course it is Python!")
#output
# print("Correct! Of course it is Python!")
# ^
# IndentationError: expected an indented block after 'if' statement on line 3
Python 中的 if else 语句是什么
单独编写 if
语句,尤其是多个语句,并没有太大帮助。当程序变得越来越大时,它也不被认为是最佳实践。这就是为什么 if
语句通常伴随着 else
语句。
if else
语句本质上是:“if
这个条件为 True
,则执行以下操作,else
请执行此操作”。
else
语句中的代码是当且仅当你在 if
语句中设置的条件评估为 False
时要运行的代码。
如果 if
语句中的条件计算为 True
,则 else
语句中的代码将永远不会运行。
else
关键字是当 if
条件为 False
并且 if
块内的代码不运行时的解决方案。它提供了另一种选择。
Python 中 if else
语句的一般语法如下:
if condition:
#run this code if condition is True
code statement(s)
else:
# if the condition above is False run this code
code statement(s)
Python 中 if else 语句的示例
让我们回顾一下前面的例子:
language = input("Please enter your favorite programming language: ")
if language == "Python":
print("Correct! Of course it is Python!")
如你之前所见,当我输入字符串 Python
时,print()
函数中的代码会运行,因为条件的计算结果为 True
。
但是,当用户输入不等于字符串 Python
的内容时,运行什么呢?
这是 else
语句派上用场的地方,它被添加到 if
语句中:
language = input("Please enter your favorite programming language: ")
if language == "Python":
print("Correct! Of course it is Python!")
else:
print("Hmm..Are you sure that it is not Python??")
如果条件为 False
,则跳过并忽略 if
语句中的代码,而运行 else
语句中的代码:
# output
# Please enter your favorite programming language: Java
# Hmm..Are you sure that it is not Python??
在这一点上要注意的一件事是,你不能在 if else
语句之间编写任何额外的代码:
language = input("Please enter your favorite programming language: ")
if language == "Python":
print("Correct! Of course it is Python!")
# Don't do this!!
print("Hello world")
else:
print("Hmm..Are you sure that it is not Python??")
# output
# else:
^^^^
# SyntaxError: invalid syntax
Python 中的 elif 语句是什么
elif
表示 else if
。
当你想设置更多的条件,而不是只有 if
和 else
语句可供选择时,你可以引入 elif
语句。
如果 if
语句为 False
,Python 将继续执行 elif
语句并尝试检查该块中设置的条件。
你还可以编写多个 elif
块,具体取决于你想要的各种选项。
一个 elif
语句本质上的意思是:“if
这个条件为 True
,请执行以下操作。如果不是,请尝试执行此操作。但是,如果以上都不为真,并且所有其他操作都失败,最后执行此操作。”
elif
语句的一般语法如下:
if condition:
#if condition is True run this code
code statement(s)
elif:
#if the above condition was False and this condition is True,
# run the code in this block
code statement(s)
else:
#if the two above conditions are False run this code
code statement
代码按照编写的顺序从上到下进行评估。
当 Python 找到计算结果为 True
的条件时,它将运行该块中的代码并忽略其余部分。
因此,如果 if
块中的代码为 True
,则其他块都不会运行。它们将被跳过和忽略。
如果 if
块中的代码为 False
,它将移动到 elif
块。
如果为 True
,则忽略其余块。
如果为 False
,Python 将移动到其他的 elif
块。
最后,如果所有条件都为 False
,那么只有在 else
块中的代码才会运行。else
块本质上意味着“当所有其他条件都不符合时,运行此代码”。
Python 中 elif 语句的示例
让我们看一个 elif
语句如何工作的示例。
看下面的例子:
age = int(input("Please enter your age: "))
if age < 18:
print("You need to be over 18 years old to continue")
elif age < 21:
print("You need to be over 21 years old")
else:
print("You are over 18 and 21 so you can continue")
如果 if
语句为 True
,则跳过其余代码:
# output
# Please enter your age: 14
# You need to be over 18 years old to continue
当 if
语句为 False
时,Python 继续执行 elif
块并检查该条件。如果 elif
语句为 True
,则跳过其余代码:
如果为 True
,Python 将运行 elif
块中的代码并忽略其余代码:
# output
# Please enter your age: 19
# You need to be over 21 years old
如果前面的两个条件都是 False
,那么运行 else
块:
# output
# Please enter your age: 45
# You are over 18 and 21 so you can continue
总结
就是这些啦!你现在知道如何在 Python 中编写 if
、if else
和 elif
语句。
我希望本教程对你有所帮助。