python条件语句多条件_Python中的条件语句

python条件语句多条件

From the previous tutorials in this series, you now have quite a bit of Python code under your belt. Everything you have seen so far has consisted of sequential execution, in which statements are always performed one after the next, in exactly the order specified.

从本系列的先前教程中,您现在掌握了很多Python代码。 到目前为止,您所看到的所有内容都包含顺序执行 ,其中语句始终按照指定的顺序在下一个之后执行。

But the world is often more complicated than that. Frequently, a program needs to skip over some statements, execute a series of statements repetitively, or choose between alternate sets of statements to execute.

但是世界往往比这更复杂。 通常,程序需要跳过某些语句,重复执行一系列语句,或者在要执行的备用语句集之间进行选择。

That is where control structures come in. A control structure directs the order of execution of the statements in a program (referred to as the program’s control flow).

这就是控制结构的用武之地。控制结构指示程序中语句的执行顺序(称为程序的控制流 )。

Here’s what you’ll learn in this tutorial: You’ll encounter your first Python control structure, the if statement.

这是在本教程中学习的内容:您将遇到第一个Python控件结构if语句。

In the real world, we commonly must evaluate information around us and then choose one course of action or another based on what we observe:

在现实世界中,我们通常必须评估我们周围的信息,然后根据我们观察到的情况选择一种行动方案或另一种行动方案:

If the weather is nice, then I’ll mow the lawn. (It’s implied that if the weather isn’t nice, then I won’t mow the lawn.)

如果天气晴朗,那我去修剪草坪。 (这意味着如果天气不好,那我就不会割草坪了。)

In a Python program, the if statement is how you perform this sort of decision-making. It allows for conditional execution of a statement or group of statements based on the value of an expression.

在Python程序中, if语句是您执行这种决策的方式。 它允许基于表达式的值有条件地执行一个语句或一组语句。

The outline of this tutorial is as follows:

本教程的概述如下:

  • First, you’ll get a quick overview of the if statement in its simplest form.
  • Next, using the if statement as a model, you’ll see why control structures require some mechanism for grouping statements together into compound statements or blocks. You’ll learn how this is done in Python.
  • Lastly, you’ll tie it all together and learn how to write complex decision-making code.
  • 首先,您将以最简单的形式快速了解if语句。
  • 接下来,使用if语句作为模型,您将了解为什么控制结构需要某种机制来将语句分组为复合语句 。 您将学习如何在Python中完成此操作。
  • 最后,将它们结合在一起,并学习如何编写复杂的决策代码。

Ready? Here we go!

准备? 开始了!

if语句简介 (Introduction to the if Statement)

We’ll start by looking at the most basic type of if statement. In its simplest form, it looks like this:

我们将从查看最基本的if语句类型开始。 最简单的形式如下:

 if if << exprexpr >> :
    :
    << statementstatement >
>

In the form shown above:

在上面显示的形式中:

  • <expr> is an expression evaluated in Boolean context, as discussed in the section on Logical Operators in the Operators and Expressions in Python tutorial.
  • <statement> is a valid Python statement, which must be indented. (You will see why very soon.)
  • <expr>是在布尔上下文中评估的表达式,如Python运算符和表达式中有关逻辑运算符的部分所述。
  • <statement>是有效的Python语句,必须缩进。 (您很快就会明白为什么。)

If <expr> is true (evaluates to a value that is “truthy”), then <statement> is executed. If <expr> is false, then <statement> is skipped over and not executed.

如果<expr>为true(评估为“ true”的值),则执行<statement> 。 如果<expr>为false,则将跳过<statement>并且不执行。

Note that the colon (:) following <expr> is required. Some programming languages require <expr> to be enclosed in parentheses, but Python does not.

注意,冒号( : )以下<expr>是必需的。 某些编程语言要求将<expr>括在括号中,而Python则不需要。

Here are several examples of this type of if statement:

以下是这种类型的if语句的几个示例:

Note: If you are trying these examples interactively in a REPL session, you’ll find that, when you hit Enter after typing in the print('yes') statement, nothing happens.

注意:如果您在REPL会话中以交互方式尝试这些示例,则会发现,在输入print('yes')语句后按Enter时,什么都不会发生。

Because this is a multiline statement, you need to hit Enter a second time to tell the interpreter that you’re finished with it. This extra newline is not necessary in code executed from a script file.

由于这是多行语句,因此您需要再次按Enter键以告诉解释器您已完成该语句。 从脚本文件执行的代码中不需要多余的换行符。

分组语句:缩进和块 (Grouping Statements: Indentation and Blocks)

So far, so good.

到目前为止,一切都很好。

But let’s say you want to evaluate a condition and then do more than one thing if it is true:

但是,假设您要评估一个条件,然后在满足条件的情况下做不止一件事情:

If the weather is nice, then I will:

如果天气晴朗,我将:

  • Mow the lawn
  • Weed the garden
  • Take the dog for a walk
  • 修剪草坪
  • 在花园里除草
  • 带着狗散步

In all the examples shown above, each if <expr>: has been followed by only a single <statement>. There needs to be some way to say “If <expr> is true, do all of the following things.”

在上面显示的所有示例中,每个if <expr>:之后仅是一个<statement> 。 必须有某种方式说“如果<expr>为true,请执行以下所有操作。”

The usual approach taken by most programming languages is to define a syntactic device that groups multiple statements into one compound statement or block. A block is regarded syntactically as a single entity. When it is the target of an if statement, and <expr> is true, then all the statements in the block are executed. If <expr> is false, then none of them are.

大多数编程语言采用的通常方法是定义一种语法设备,该设备将多个语句分组为一个复合语句 。 块在语法上被视为单个实体。 如果它是if语句的目标并且<expr>为true,则将执行块中的所有语句。 如果<expr>为false,则都不是。

Virtually all programming languages provide the capability to define blocks, but they don’t all provide it in the same way. Let’s see how Python does it.

几乎所有的编程语言都提供了定义块的功能,但它们并非都以相同的方式提供。 让我们看看Python是如何做到的。

Python:与缩进有关 (Python: It’s All About the Indentation)

Python follows a convention known as the off-side rule, a term coined by British computer scientist Peter J. Landin. (The term is taken from the offside law in association football.) Languages that adhere to the off-side rule define blocks by indentation. Python is one of a relatively small set of off-side rule languages.

Python遵循称为越位规则的惯例,该惯例是英国计算机科学家Peter J. Landin创造的。 (该术语取自协会足球比赛的越位法。)遵守越位规则的语言通过缩进来定义块。 Python是相对较小的越位规则语言中的一种

Recall from the previous tutorial on Python program structure that indentation has special significance in a Python program. Now you know why: indentation is used to define compound statements or blocks. In a Python program, contiguous statements that are indented to the same level are considered to be part of the same block.

回顾先前有关Python程序结构的教程, 缩进在Python程序中具有特殊的意义 。 现在您知道原因了:缩进用于定义复合语句或块。 在Python程序中,缩进相同级别的连续语句被视为同一块的一部分。

Thus, a compound if statement in Python looks like this:

因此,Python中的复合if语句如下所示:

 if if << exprexpr >> :
    :
    << statementstatement >
    >
    << statementstatement >
    >
    ...
    ...
    << statementstatement >
>
<< following_statementfollowing_statement >
>

Here, all the statements at the matching indentation level (lines 2 to 5) are considered part of the same block. The entire block is executed if <expr> is true, or skipped over if <expr> is false. Either way, execution proceeds with <following_statement> (line 6) afterward.

在此,所有匹配缩进级别的语句(第2至5行)都被视为同一块的一部分。 整个块执行如果<expr>为真,或跳过如果<expr>是假的。 无论哪种方式,执行都会在<following_statement> (第6行)之后进行。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值