博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python中的条件语句(if,if ... else,if ... elif ... else和嵌套的if)
阅读量:2531 次
发布时间:2019-05-11

本文共 4348 字,大约阅读时间需要 14 分钟。

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 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 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 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 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 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, 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 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

翻译自:

转载地址:http://tkxzd.baihongyu.com/

你可能感兴趣的文章
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
64位MATLAB和C混合编程以及联合调试
查看>>
原生js大总结二
查看>>
PHP基础
查看>>
UVa 11488 超级前缀集合(Trie的应用)
查看>>
Django 翻译与 LANGUAGE_CODE
查看>>
[转]iOS教程:SQLite的创建数据库,表,插入查看数据
查看>>
【转载】OmniGraffle (一)从工具栏开始
查看>>
初识ionic
查看>>
java 中打印调用栈
查看>>
开发 笔记
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>