matlabfor循环语句举例_什么是条件语句(1)

副标题: 17. What are Conditionals (1)

这个专栏的文字, 不仅仅是学习 Python 的基础知识, 同时, 也按照 2/8 规律 学习关键知识的关键部分 - python 核心英语词汇. 我们开始这一节的正文.

Conditionals 条件语句

什么是条件语句? 在程序设计中,有3种控制结构。 In programming, there are 3 types of control structures:

  1. sequencial execution 顺序执行
  2. if then else decisions
  3. looping back 循环
  4. 条件句属于 if then else decisions 控制结构。使用条件语句, 改变程序的执行流(程)。
英文:Conditionals falls under the 'if then else decision' control structure. Conditional statements are used to change the flow of execution of a program.

到目前为止我们在这门课上遇到的一切程序, 它基于代码的顺序执行。 Everything we come across so far in this course, has its basis on sequential execution of codes within a program.

概念:

sequencial execution 顺序执行

在顺序执行结构中,代码按编码顺序一个接一个地执行。 In a sequential execution, codes are performed one after another, in the sequence in which they are coded.

然而,我们可以通过条件分支语句和循环语句, 更改其程序的执行流。Loop 将在下一节介绍 This flow of execution of a program can be changed, through conditional branching statements and loop statements.

Conditional branching statements 条件分支语句

执行第一代码块 first block

条件分支语句举例:

your_score = 48
passing_score = 50

if your_score >= passing_score:
    print("Congrats!")
    print("You have passed!")
        # output: 没有输出
        ## 当我们运行这个程序时,因为你的分数(48分)不大于或等于通过分数(50分),所以它不会运行第一个缩进的代码块
           英文: When we run this program, because your score, which is 48, is NOT greater than or equal to passing score,which is 50, it will NOT run the first indented block of codes.


else:
    print("You did not pass.") 
    print("Please try again next time. ") 
        # output: else 缩进块的内容全部输出.
        ## 相反,它将跳到 else 缩进代码块,并打印You did not pass 和 Please try again next time.
           英文:Instead, it will skip to the else indented block of codes, and print You did not pass, and Please try again next time.

print("Thank you for taking the exam.")

最终输出的是如下三项:

print("You did not pass.") 
print("Please try again next time.") 
print("Thank you for taking the exam.")

print("Thank you for taking the exam.") 无论何时都会被输出,

  • 因为两点:
  • 1. 第一代码块都将被执行
  • 2. "print Thank you for taking the exam",不是任何缩进代码块的一部分,无论是在 if 块,还是在 else 块。
    英文: "print Thank you for taking the exam", is not part of any indented block of codes, under neither the if block, nor the else block.
if and else 的标准格式: 条件语句后必须有冒号, 执行code 必须缩进 4 个空格.

if <condition>:
    <code to be executed if condition is True> 
    <code to be executed if condition is True>
    .....

else: 在 if 条件语句 False 后, 将被执行, 
    <code to be executed if condition is False> 
    <code to be executed if condition is False>
    .....
注意,如果代码 “在if条件为假的情况下,不打算执行任何操作”,那么可以省略 else 语句和 else 块。 英文: Note that the else statement, and the else block, can be omitted if your code does not "intend to execute anything, when the if condition is False.
再次强调冒号的位置并举例:

if <condition>:
    <indented block of codes> 
    <indented block of codes>
    .....
else:
    <indented block of codes> 
    <indented block of codes>
    .....
  • 请注意,在Python中,冒号非常重要,并且必须放在任何缩进代码块开始之前的行尾。
  • 英文: Notice that in Python, the colon is very important, and must be place at the end of the line preceding the START of ANY indented block of codes.
在本例中,if 和 else语句下面都有一个缩进的代码块,它们必须在语句的末尾有一个冒号。 Python中的首要准则是,在任何缩进代码块之前的行上,该行必须始终以冒号结束。
In this case, the if and else statements, both of which have an indented block of codes following them, must have a colon at the end of the statements. The rule of thumb in Python is that on THE line preceding ANY indented block of codes, THAT line must always end with a colon.

有关缩进的空格数目 -建议是 4 个空格

  • 一个关于 Python 编程中的常见实践, 使用空格来缩进你的代码。

建议使用的空格数为4。你可以使用2个空格,或者3个空格,或者任意数量的空格,
但是要确保在整个程序中保持相同的空格数。

你也可以用Tab键来缩进你的代码。但是,如果将制表符和空格混合使用,就会遇到很多问题, 因为Python在发现不一致时就会出错。因此,坚持使用空格或制表符,并确保在整个程序中保持这种一致性。
英文: You can use Tab to indent your codes too. However, you will run into plenty of problems, if you mix Tabs with spaces, because Python will give errors when it finds inconsistency. So, stick to either spaces or Tab, and make sure you maintain that consistency throughout your program.

空格与 Tab

Python是一种对空格敏感的编程语言,对空格和制表符的处理是不同的。 在Python中遵守有关缩进的规则非常重要,因为Python使用缩进来解释您的编码.

英文: Python is a whitespace sensitive programming language, and treats spaces and Tabs differently. It is very important to follow the rules regarding indentation in Python, because Python uses indentation to interpret your coding logic.

词汇

The rule of thumb 首要准则

faffda2122487199b1715b6d0c0d6d27.png

Python 的首要原则:

The rule of thumb in Python is that on THE line preceding ANY indented block of codes, THAT line must always end with a colon.

发布时间: 2020 年 2 月 27 日 知乎链接: 什么是条件语句(1)当前可任意转载,转载请保存以上信息即可。无需获得授权

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值