Python教程-Python if-else 语句
决策是几乎所有编程语言中最重要的方面。顾名思义,决策允许我们针对特定的决策运行特定的代码块。在这里,决策是根据特定条件的有效性而做出的。条件检查是决策制定的基础。
在Python中,使用以下语句执行决策制定。
语句 | 描述 |
---|---|
if 语句 | if 语句用于测试特定条件。如果条件为真,则会执行一段代码块(if-块)。 |
if - else 语句 | if-else 语句与 if 语句类似,只是还提供了条件的假情况的代码块。如果 if 语句中提供的条件为假,则会执行 else 语句。 |
嵌套的 if 语句 | 嵌套的 if 语句使我们可以在外部 if 语句内部使用 if - else 语句。 |
Python 中的缩进
为了编程方便和实现简单性,Python 不允许使用括号来表示块级别的代码。在Python中,缩进用于声明一个代码块。如果两个语句具有相同的缩进级别,那么它们是同一代码块的一部分。
通常,给出四个空格来缩进语句,这是Python中典型的缩进量。
缩进是Python语言中最常用的部分,因为它声明了代码块。一个块的所有语句都具有相同级别的缩进。我们将看到实际的缩进如何在决策制定和其他Python内容中发生。
if 语句
if 语句用于测试特定条件,如果条件为真,则执行一个称为 if-块 的代码块。if 语句的条件可以是任何有效的逻辑表达式,可以计算为真或假。
if 语句的语法如下:
if expression:
statement
示例 1
# Simple Python program to understand the if statement
num = int(input("enter the number:"))
# Here, we are taking an integer num and taking input dynamically
if num%2 == 0:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("The Given number is an even number")
输出:
enter the number: 10
The Given number is an even number
示例 2:编写一个程序,找出三个数中的最大数。
# Simple Python Program to print the largest of the three numbers.
a = int (input("Enter a: "));
b = int (input("Enter b: "));
c = int (input("Enter c: "));
if a>b and a>c:
# Here, we are checking the condition. If the condition is true, we will enter the block
print ("From the above three numbers given a is largest");
if b>a and b>c:
# Here, we are checking the condition. If the condition is true, we will enter the block
print ("From the above three numbers given b is largest");
if c>a and c>b:
# Here, we are checking the condition. If the condition is true, we will enter the block
print ("From the above three numbers given c is largest");
输出:
Enter a: 100
Enter b: 120
Enter c: 130
From the above three numbers given c is largest
if-else 语句
if-else 语句提供了一个与 if 语句结合的 else 块,该块在条件的假情况下执行。
如果条件为真,则执行 if-块。否则,执行 else-块。
if-else 语句的语法如下:
if condition:
#block of statements
else:
#another block of statements (else-block)
示例 1:编写一个程序,判断一个人是否有资格投票。
# Simple Python Program to check whether a person is eligible to vote or not.
age = int (input("Enter your age: "))
# Here, we are taking an integer num and taking input dynamically
if age>=18:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("You are eligible to vote !!");
else:
print("Sorry! you have to wait !!");
输出:
Enter your age: 90
You are eligible to vote !!
示例 2:编写一个程序,判断一个数字是偶数还是奇数。
# Simple Python Program to check whether a number is even or not.
num = int(input("enter the number:"))
# Here, we are taking an integer num and taking input dynamically
if num%2 == 0:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("The Given number is an even number")
else:
print("The Given Number is an odd number")
输出:
enter the number: 10
The Given number is even number
elif 语句
elif 语句允许我们检查多个条件,并根据它们之间的真实条件执行特定的语句块。根据需要,我们的程序可以具有任意数量的 elif 语句。然而,使用 elif 是可选的。
elif 语句的工作方式类似于C中的 if-else-if 梯形语句。它必须跟在 if 语句后面。
elif 语句的语法如下:
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
示例 1
# Simple Python program to understand elif statement
number = int(input("Enter the number?"))
# Here, we are taking an integer number and taking input dynamically
if number==10:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("The given number is equals to 10")
elif number==50:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("The given number is equal to 50");
elif number==100:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("The given number is equal to 100");
else:
print("The given number is not equal to 10, 50 or 100");
输出:
Enter the number?15
The given number is not equal to 10, 50 or 100
示例 2
# Simple Python program to understand elif statement
marks = int(input("Enter the marks? "))
# Here, we are taking an integer marks and taking input dynamically
if marks > 85 and marks <= 100:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("Congrats ! you scored grade A ...")
elif marks > 60 and marks <= 85:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("You scored grade B + ...")
elif marks > 40 and marks <= 60:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("You scored grade B ...")
elif (marks > 30 and marks <= 40):
# Here, we are checking the condition. If the condition is true, we will enter the block
print("You scored grade C ...")
else:
print("Sorry you are fail ?")
输出:
Enter the marks? 89
Congrats ! you scored grade A ...