Python学习笔记(九)- 介绍Python语句(Introducing Python Statements)

1.哪三样东西在类C语言中被需要但是在Python被忽略?
答:类C语言需要在某些语句中在测试周围加上括号,在每个语句的末尾使用分号,并在嵌套的代码块周围用大括号表示。如下面的类C语言:

if (x > y) {
 x = 1;
 y = 2;
}

Python版的:

if x > y:
 x = 1
 y = 2

2.在Python中,正常情况语句如何被终止?
答:行的末尾终止出现在该行上的语句。或者,如果多个语句出现在同一行中,则可以用分号;类似地,如果一条语句跨越多行,则必须通过结束括号来结束它。

3.在Python中,嵌套代码块中的语句通常是如何关联的?
答:嵌套块中的语句都缩进了相同数量的制表符或空格。

4.你如何使得单个语句跨越多行?
答:通过将语句的一部分括在括号、方括号或大括号中,可以使语句跨越多行;当python看到包含结束部分括号的行时,语句就会结束。

如:

>>>mylist = [1111,
 2222,
 3333]

5.你如何在单行上写复合语句?
答:复合语句的正文可以移动到冒号后的标头行(header line),但只有在主体仅由非复合语句组成的情况下才能移动。

标头行(Header line):
    嵌套语句块(Nested statement block)

6.在Python语句末尾键入分号有任何正当理由吗?
答:只有当您需要将多个语句压缩到一行代码上时,才会这样做。即使这样,这也只能在所有语句都是非复合的情况下才能起作用,并且不鼓励使用,因为它可能导致代码很难读懂。

7.try语句用来干啥?
答:try语句用于捕获Python脚本中的异常(错误)并从中恢复。它通常是手动检查代码中错误的替代方法。

8.在Python初学者中,最常见的代码错误是啥?
答:忘记在复合语句的标题行末尾键入冒号字符(:)是初学者最常见的错误。如果您是Python新手,还没有出现,那么很快就会了。

标注:转载《Learning Python 5th Edition》[奥莱理]


1. What three things are required in a C-like language but omitted in Python?
2. How is a statement normally terminated in Python?
3. How are the statements in a nested block of code normally associated in Python?
4. How can you make a single statement span multiple lines?
5. How can you code a compound statement on a single line?
6. Is there any valid reason to type a semicolon at the end of a statement in Python?
7. What is a try statement for?
8. What is the most common coding mistake among Python beginners?

1. C-like languages require parentheses around the tests in some statements, semicolons at the end of each statement, and braces around a nested block of code.
2. The end of a line terminates the statement that appears on that line. Alternatively, if more than one statement appears on the same line, they can be terminated with semicolons; similarly, if a statement spans many lines, you must terminate it by closing a bracketed syntactic pair.
3. The statements in a nested block are all indented the same number of tabs or spaces.
4. You can make a statement span many lines by enclosing part of it in parentheses, square brackets, or curly braces; the statement ends when Python sees a line that contains the closing part of the pair.
5. The body of a compound statement can be moved to the header line after the colon, but only if the body consists of only noncompound statements.
6. Only when you need to squeeze more than one statement onto a single line of code. Even then, this only works if all the statements are noncompound, and it's discouraged because it can lead to code that is difficult to read.
7. The try statement is used to catch and recover from exceptions (errors) in a Python script. It's usually an alternative to manually checking for errors in your code.
8. Forgetting to type the colon character at the end of the header line in a compound statement is the most common beginner’s mistake. If you’re new to Python and haven't made it yet, you probably will soon!

猜你喜欢

转载自blog.csdn.net/Enderman_xiaohei/article/details/87208488