python示例_Python中的缩进示例

Python的缩进对于代码块的标识至关重要,不同于其他语言仅用于美化,Python中缩进错误会导致程序崩溃。if/elif语句用于复杂条件判断,条件表达式提供了一种在表达式中简洁表示if/else的语法。了解这些规则能帮助编写更加清晰的Python代码。
摘要由CSDN通过智能技术生成

python示例

It is generally good practice for you not to mix tabs and spaces when coding in Python. Doing this can possibly cause a TabError, and your program will crash. Be consistent when you code - choose either to indent using tabs or spaces and follow your chosen convention throughout your program.

通常,对于您来说,在Python中进行编码时,不要混用制表符和空格是一种很好的做法。 这样做可能会导致TabError ,并且您的程序将崩溃。 编码时保持一致-选择使用制表符或空格进行缩进,并在整个程序中遵循所选的约定。

代码块和缩进 (Code Blocks and Indentation)

One of the most distinctive features of Python is its use of indentation to mark blocks of code. Consider the if-statement from our simple password-checking program:

Python最独特的功能之一是它使用缩进来标记代码块。 考虑一下我们简单的密码检查程序中的if语句:

if pwd == 'apple':
    print('Logging on ...')
else:
    print('Incorrect password.')

print('All done!')

The lines print(‘Logging on …’) and print(‘Incorrect password.’) are two separate code blocks. These ones happen to be only a single line long, but Python lets you write code blocks consisting of any number of statements.

行print('Logging on ...')和print('Incorrect password。')是两个单独的代码块。 这些代码恰好只有一行,但是Python允许您编写由任意数量的语句组成的代码块。

To indicate a block of code in Python, you must indent each line of the block by the same amount. The two blocks of code in our example if-statement are both indented four spaces, which is a typical amount of indentation for Python.

要在Python中指示代码块,您必须使代码块的每一行缩进相同的数量。 我们的示例if语句中的两个代码块都缩进了四个空格,这是Python的典型缩进量。

In most other programming languages, indentation is used only to help make the code look pretty. But in Python, it is required for indicating what block of code a statement belongs to. For instance, the final print(‘All done!’) is not indented, and so is not part of the else-block.

在大多数其他编程语言中,缩进仅用于帮助使代码看起来更漂亮。 但是在Python中,需要指出语句属于哪个代码块。 例如,最终打印(“ All done!”)没有缩进,因此也不是else块的一部分。

Programmers familiar with other languages often bristle at the thought that indentation matters: Many programmers like the freedom to format their code how they please. However, Python indentation rules are quite simple, and most programmers already use indentation to make their code readable. Python simply takes this idea one step further and gives meaning to the indentation.

熟悉其他语言的程序员经常会想到缩进很重要:许多程序员喜欢自由地格式化自己喜欢的代码。 但是,Python缩进规则非常简单,大多数程序员已经使用缩进使代码可读。 Python只是将这一想法更进一步,并为缩进赋予了意义。

如果/省略语句 (If/elif-statements)

An if/elif-statement is a generalized if-statement with more than one condition. It is used for making complex decisions. For example, suppose an airline has the following “child” ticket rates: Kids 2 years old or younger fly for free, kids older than 2 but younger than 13 pay a discounted child fare, and anyone 13 years or older pays a regular adult fare. The following program determines how much a passenger should pay:

if / elif语句是具有多个条件的广义if语句。 它用于做出复杂的决定。 例如,假设一家航空公司具有以下“儿童”机票价格:2岁或以下的儿童免费乘坐飞机,2岁以上但13岁以下的儿童可享受折扣儿童票价,而13岁以上的任何人均可享受常规成人票价。 以下程序确定乘客应支付的费用:

# airfare.py
age = int(input('How old are you? '))
if age <= 2:
    print(' free')
elif 2 < age < 13:
    print(' child fare)
else:
    print('adult fare')

After Python gets age from the user, it enters the if/elif-statement and checks each condition one after the other in the order they are given. So first it checks if age is less than 2, and if so, it indicates that the flying is free and jumps out of the elif-condition. If age is not less than 2, then it checks the next elif-condition to see if age is between 2 and 13. If so, it prints the appropriate message and jumps out of the if/elif-statement. If neither the if-condition nor the elif-condition is True, then it executes the code in the else-block.

Python从用户处获得年龄后,它将进入if / elif语句,并按照给出的顺序依次检查每个条件。 因此,首先检查年龄是否小于2,如果小于2,则表明飞行是自由的,并跳出了省略条件。 如果age不小于2,则它检查下一个elif条件,以查看age是否在2到13之间。如果是,则打印适当的消息并跳出if / elif语句。 如果if条件和elif条件都不为True,则它将在else块中执行代码。

条件表达式 (Conditional expressions)

Python has one more logical operator that some programmers like (and some don’t!). It’s essentially a shorthand notation for if-statements that can be used directly within expressions. Consider this code:

Python还有一个逻辑运算符,有些程序员喜欢(有些则不喜欢!)。 它本质上是if语句的简写形式,可以在表达式中直接使用。 考虑以下代码:

food = input("What's your favorite food? ")
reply = 'yuck' if food == 'lamb' else 'yum'

The expression on the right-hand side of = in the second line is called a conditional expression, and it evaluates to either ‘yuck’ or ‘yum’. It’s equivalent to the following:

第二行中=右侧的表达式称为条件表达式,其结果为'yuck'或'yum'。 等效于以下内容:

food = input("What's your favorite food? ")
if food == 'lamb':
   reply = 'yuck'
else:
   reply = 'yum'

Conditional expressions are usually shorter than the corresponding if/else-statements, although not quite as flexible or easy to read. In general, you should use them when they make your code simpler.

条件表达式通常比相应的if / else语句短,尽管不够灵活或不易阅读。 通常,当它们使您的代码更简单时,应使用它们。

Python Documentation - Indentation

Python文档-缩进

翻译自: https://www.freecodecamp.org/news/indentation-in-python/

python示例

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值