失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Python中的条件语句(if if ... else if ... elif ... else和嵌套的if)

Python中的条件语句(if if ... else if ... elif ... else和嵌套的if)

时间:2019-01-25 15:56:09

相关推荐

Python中的条件语句(if if ... else if ... elif ... else和嵌套的if)

Conditional statementsdecide the flow of program execution. In programming whenever we need to make execute any special blocks based on the decision then we use theconditional statements.

条件语句决定程序执行的流程。 在编程中,只要我们需要根据决策执行任何特殊的块,就使用条件语句。

No need to be confused about it because we are using it in our daily life when we need to make some decisions and based on these decisions, we decide what should we have to do next. Similar situations can occur while programming also where we need to make some decisions based on conditions given and based on these decisions we will execute the block of code.

无需对此感到困惑,因为当我们需要做出一些决定时,我们就在日常生活中使用它,并根据这些决定来决定下一步该做什么。 在编程时也会发生类似情况,在这种情况下,我们需要根据给定的条件做出一些决策,并根据这些决策执行代码块。

Conditional statementsavailable in the Python are,

Python中可用的条件语句是

if statements

如果陈述

if...else statements

如果...其他陈述

if...elif...else statements

if ... elif ... else语句

Nested if statements

嵌套if语句

1)Python if语句 (1) Python if statement)

It is one of the most common conditional statements in which some conditions are provided and if the condition is true then block under the if the condition will be executed.

它是最常见的条件语句之一,其中提供了一些条件,如果条件为true,则在条件将被执行时阻塞。

Syntax:

句法:

if condition:# what we want to execute here.

Example:

例:

# input ageage=int(input('what is your age: '))# checking the conditionif age>=18:print('You are Grown-up now !')

Output

输出量

RUN 1:what is your age: 21You are Grown-up now !RUN 2:what is your age: 15

2)Python if ... else语句 (2) Python if...else statement )

In the above, we have seen that if the condition is true then block under if will execute then one thing is, comes to our mind that what happens when the condition will be false. So, to overcome this problem we are usingif...else statements.

在上面的内容中,我们已经看到,如果条件为true,则在条件执行时阻塞,然后发生一件事,我们想到的是,条件为false时会发生什么。 因此,为了克服这个问题,我们使用了if ... else语句。

Syntax:

句法:

if condition:# what we want to execute here.else:# what we want to execute here.

If the condition istrue, then it will execute the block of if statements otherwise else statement.

如果条件为true,则它将执行if语句的块,否则执行else语句。

Example:

例:

# input ageage=int(input('what is your age: '))# checking the conditionif age>=18:print('You are Grown-up now !')else:print('You are Young!')

Output

输出量

RUN 1:what is your age: 21You are Grown-up now !RUN 2:what is your age: 15You are Young!

3)Python if ... elif ... else语句 (3) Python if...elif...else statement)

These conditional statements use where we have to check multiple conditions in the program. If these will nottruethat isfalsethen the else blocks only execute.

这些条件语句用于我们必须检查程序中的多个条件的地方。 如果这些都不是,则返回true,否则else块仅执行。

Syntax:

句法:

if condition:# what we want to execute here.elif conditions:# what we want to execute here.else:# what we want to execute here.

Example:

例:

# input the agen=int(input('Enter marks: '))# checking the conditionsif n>=90:print('Excellent')elif n<90 and n>=75:print('Passed')else:print('Fail')

Output

输出量

RUN 1:Enter marks: 95ExcellentRUN 2:Enter marks: 80PassedRUN 3:Enter marks: 63Fail

4)Python嵌套的if语句 (4) Python Nested if statement)

As we all have familiar with the word nested which means one statement inside another statement same in the programming nested if statements mean an if statement inside another if statement that is we can place one if statements inside another if statements.

众所周知,嵌套一词意味着在编程中相同的另一个语句中的一个语句,嵌套的if语句意味着另一个if语句内部的if语句,我们可以将一个if语句放在另一个if语句中。

Syntax:

句法:

if condition:# what we want to execute here.if condition:# what we want to execute here.else:# what we want to execute here.

Note:In Python,trueandfalseare written asTrueandFalse. Since Python follow the indentation rule so the statements must write under the indentations of if statements or other.

注意:在Python中,true和false分别写为True和False。 由于Python遵循缩进规则,因此语句必须在if语句或其他语句的缩进下编写。

Now, we will see an example based on the above conditional statements which will show us the grade of students.

现在,我们将基于上述条件陈述看到一个示例,该示例将向我们显示学生的成绩。

Example:

例:

# input the agen=int(input('Enter marks: '))# checking the conditionsif n>=75:if n >=95:print('Excellent')else:print('Pass')else:print('Fail')

Output

输出量

RUN 1:Enter marks: 96ExcellentRUN 2:Enter marks: 89PassRUN 3:Enter marks: 69Fail

翻译自: /python/conditional-statements-if-if-else-if-elif-else-and-nested-if.aspx

如果觉得《Python中的条件语句(if if ... else if ... elif ... else和嵌套的if)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。