Conditional statements decide the flow of program execution. In programming whenever we need to make execute any special blocks based on the decision then we use the conditional 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 statements available in the Python are,

Python中可用的条件语句

  1. if statements

    如果陈述

  2. if...else statements

    如果...其他陈述

  3. if...elif...else statements

    if ... elif ... else语句

  4. 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 age
age=int(input('what is your age: '))
# checking the condition
if age>=18:
print('You are Grown-up now !')

Output

输出量

RUN 1:
what is your age: 21
You 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 using if...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 is true, then it will execute the block of if statements otherwise else statement.

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

Example:

例:

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

Output

输出量

RUN 1:
what is your age: 21
You are Grown-up now !
RUN 2:
what is your age: 15
You 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 not true that is false then 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 age
n=int(input('Enter marks: '))
# checking the conditions
if n>=90:
print('Excellent')
elif n<90 and n>=75:
print('Passed')
else:
print('Fail')

Output

输出量

RUN 1:
Enter marks: 95
Excellent
RUN 2:
Enter marks: 80
Passed
RUN 3:
Enter marks: 63
Fail

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, true and false are written as True and False. Since Python follow the indentation rule so the statements must write under the indentations of if statements or other.

注意:在Python中, truefalse分别写为TrueFalse 。 由于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 age
n=int(input('Enter marks: '))
# checking the conditions
if  n>=75:
if n >=95:
print('Excellent')
else:
print('Pass')
else:
print('Fail')

Output

输出量

RUN 1:
Enter marks: 96
Excellent
RUN 2:
Enter marks: 89
Pass
RUN 3:
Enter marks: 69
Fail

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

Python中的条件语句(if,if ... else,if ... elif ... else和嵌套的if)相关推荐

  1. Python中的条件语句

    Python中的条件语句是通过一条或多条语句的执行结果(True或者False)来决定要执行的代码块.主要通过if关键字实现,条件中的其他分支用else. 回到以前读书的时候经常用到的一个例子:给学生 ...

  2. python 条件语句漫画解析_【Python】解析Python中的条件语句和循环语句

    1.if语句 if语句有好几种格式,比如: ifcondition: statement 使用 if ... else ...: ifcondition: statement(1)else: stat ...

  3. Python中的条件语句和循环语句

    1.if语句 if语句有好几种格式,比如: if condition:statement 使用if ... else ...: if condition:statement(1) else:state ...

  4. 解析Python中的条件语句和循环语句

    1.if语句 if语句有好几种格式,比如: if condition:statement 使用if ... else ...: if condition:statement(1) else:state ...

  5. python中if条件语句的代码实例

    上一篇讲的if条件语句的语法和一些注意事项以及简单的使用了下,这篇文章就用一个上网的实例分为简单版和进阶版来更深层次的加强学习if语句. 实例:上网 需求: 如果用户年龄大于等于18岁,即成年,输出& ...

  6. python中多条件语句_Python中的带条件语句

    所以我编写了这段代码:它的调用方式如下: with c_with(needs_with(), lambda: get_stuff()) as gs: ##DOESN't call get_stuff( ...

  7. python语言基本语句-Python中的基本语句

    本文简单的介绍下Python的几个基本语句. print语句 print可同时打印多个表达式,只要将他们用逗号隔开. >>> name='Gumy' >>> gre ...

  8. Python if else条件语句

    条件语句用来判定所给条件是否满足要求,根据判定的结果,决定接下来的操作.条件语句是一种控制结构,因为它们允许根据定义的特定条件,控制在何时执行哪一部分的代码. 条件语句是 Python 中最重要的概念 ...

  9. python编程的条件语句_Python条件语句实例

    Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块.可以通过下图来简单了解条件语句的执行过程. Python中的条件语句包括if.elif.else语句,用 ...

最新文章

  1. 全球首个「活体机器人」生娃!100%青蛙基因,杀不死,可繁衍4世
  2. 动图|几张动图告诉你,工业机器人无所不能!
  3. 量子计算生态:市场预期、行业应用与“霸权”争夺
  4. 实现接口Controller定义控制器
  5. c#抓取別的網頁的內容
  6. 计算机之父童年的故事ppt,《“计算机之父”童年的故事》PPT课件2
  7. mysql date类型计算_MySQL date类型
  8. centos7将网卡名字改成eth样式
  9. java abstractnumlist_如何从List集合中删除对象
  10. C++ Primer 第二章 学习笔记及习题答案
  11. UVa1586 - Molar mass
  12. 【5G系列】MAC (Medium Access Control)协议详解
  13. linaro软件源更新问题
  14. SCSI硬盘数据如何用EasyRecovery恢复
  15. 轻量级服务器和ECS云服务器有什么区别?
  16. sql server 导入excel数据表
  17. Linux 永久设置时间
  18. 初学者如何搭建React开发环境并且创建react项目
  19. XZ_Python3之使用Python批量打企业ipa包遇到的问题和解决
  20. 程序员该如何管理后宫:朕只爱一个皇后!(单例模式)

热门文章

  1. CVE-2021-41773 CVE-2021-42013 Apache HTTPd最新RCE漏洞复现 目录穿越漏洞
  2. 安装npm_Npm安装包的版本号是如何更新的?
  3. mysql 乘法_测试面试题集Python花式打印九九乘法口诀表
  4. python批量新建文件_python批量处理
  5. 生成一个GitHub的token用于git推送本地库至远程库
  6. Angular自动取消订阅RxJs
  7. UVA - 202 Repeating Decimals
  8. IIS 5 与IIS 6 原理介绍
  9. BlockingQueue详解
  10. ASP.NET 5 Beta8 已经发布