📜  Python中If-else语句

📅  最后修改于: 2020-10-24 08:57:54             🧑  作者: Mango

Python If-else语句

决策是几乎所有编程语言中最重要的方面。顾名思义,决策制定使我们可以为特定决策运行特定代码块。在此,将根据特定条件的有效性做出决定。条件检查是决策的基础。

在Python,决策由以下语句执行。

Statement Description
If Statement The if statement is used to test a specific condition. If the condition is true, a block of code (if-block) will be executed.
If – else Statement The if-else statement is similar to if statement except the fact that, it also provides the block of the code for the false case of the condition to be checked. If the condition provided in the if statement is false, then the else statement will be executed.
Nested if Statement Nested if statements enable us to use if ? else statement inside an outer if statement.

Python的缩进

为了便于编程和简化, Python不允许在块级代码中使用括号。在Python,缩进用于声明一个块。如果两个语句处于相同的缩进级别,则它们是同一块的一部分。

通常,给定四个空格以使语句缩进,这是Python典型的缩进量。

缩进是Python语言最常用的部分,因为它声明了代码块。一个块的所有语句都打算在同一级别缩进。我们将了解Python中决策和其他内容中实际缩进的方式。

if语句

if语句用于测试特定条件,如果条件为true,它将执行称为if-block的代码块。 if语句的条件可以是可以评估为true或false的任何有效逻辑表达式。

下面给出if语句的语法。

if expression:
    statement

例子1

num = int(input("enter the number?"))
if num%2 == 0:
    print("Number is even")

输出:

enter the number?10
Number is even

示例2:程序print三个数字中最大的一个。

a = int(input("Enter a? "));
b = int(input("Enter b? "));
c = int(input("Enter c? "));
if a>b and a>c:
    print("a is largest");
if b>a and b>c:
    print("b is largest");
if c>a and c>b:
    print("c is largest");

输出:

Enter a? 100
Enter b? 120
Enter c? 130
c is largest

if-else语句

if-else语句提供一个else块与if语句组合,该if语句在条件为假的情况下执行。

如果条件为真,则执行if块。否则,执行else块。

下面给出了if-else语句的语法。

if condition:
    #block of statements 
else: 
    #another block of statements (else-block) 

示例1:检查一个人是否有资格投票的程序。

age = int (input("Enter your age? "))
if age>=18:
    print("You are eligible to vote !!");
else:
    print("Sorry! you have to wait !!");

输出:

Enter your age? 90
You are eligible to vote !!

示例2:检查数字是否为偶数的程序。

num = int(input("enter the number?"))
if num%2 == 0:
    print("Number is even...")
else:
    print("Number is odd...")

输出:

enter the number?10
Number is even

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

number = int(input("Enter the number?"))
if number==10:
    print("number is equals to 10")
elif number==50:
    print("number is equal to 50");
elif number==100:
    print("number is equal to 100");
else:
    print("number is not equal to 10, 50 or 100");

输出:

Enter the number?15
number is not equal to 10, 50 or 100

例子2

 marks = int(input("Enter the marks? "))
if marks > 85 and marks <= 100:
    print("Congrats ! you scored grade A ...")
elif marks > 60 and marks <= 85:
    print("You scored grade B + ...")
elif marks > 40 and marks <= 60:
    print("You scored grade B ...")
elif (marks > 30 and marks <= 40):
    print("You scored grade C ...")
else:
    print("Sorry you are fail ?")