Python learning from the introductory operation list

高洛峰
Release: 2017-03-26 17:00:18
Original
2765 people have browsed it

4.1 Traverse the entire list

We often need to traverse all elements in the list and perform the same operation on each list. This is useful when doing repetitive work, repetitive work. For example, in a game, you may need to translate each interface element the same distance; for a list containing numbers, you may need to perform the same statistical operation on each element; in a website, you may need to display each title in 3 articles . When you need to perform the same operation on each element in the list, you can use the for loop in Python.

If we have a list of magicians, we need to print out the name of each magician. To do this, we could get each name in the list individually, but this would cause a lot of problems. For example, if the list is too long, it will contain a lot of duplicate code. Additionally, the code must be modified every time the length of the list changes. By using a for loop, you can let Python handle these problems.

The following uses a for loop to print all the names in the magician's list:

magicians = ["alice","david","carolina"]
for magician in magicians:
(magician)
运行结果:
alice
david
carolina 首先,我们像第三章那样定义了一个列表。接下来,我们定义了一个for循环;这行代码让Python从列表magicians中去除一个名字,并将其存储在
magician中。最后,我们让Python打印前面存储到变量magician中的名字。这样,对于列表中的每个名字,Python都将重复执行。我们可以这样解读这些代码
:对于列表magician中的每位魔术师,都将其名字打印出来。输出很简单,就是列表中所有的姓名:
4.1.1 深入地研究循环循环这种概念很重要,尤其在操作重复性的问题的时候,使用自动循环会提高很多效率,而且它是让计算机自动完成重复工作的常见方式之一。例如,在前
面的magicians.py中使用的简单循环中,Python将首先读取其中的第一行代码:
    for magician in magicians:
    这行代码,让Python获取列表中magicians中的第一个值("alice"),并将其存储到变量magician中。接下来,Python读取下一行代码。
    print(magician)
    它让Python打印magician的值---依然是"alice"。鉴于该列表还包含其他值,Python返回循环的第一行:
    for magician in magicians:
    python获取列表中下一个名字——"david",并将其存储到变量magician中,在执行下面的代码:
    print(magician)
    Python再次打印变量magician的值"david"。接下来,Python再次执行整个循环,对列表中的最后一个值——"carolina"进行处理。至此,列表中没有其
它的值了,因此Python接着执行程序的下一行代码。在这个实例中,for循环后面没有其他的代码,因此程序就此结束。
    刚开始使用循环时请牢记,对列表中的每个元素,都将执行循环制定的步骤,而不管列表包含多少元素。如果列表包含一百万个元素,Python就重复执行一
百万次,且通常速度非常快。
    另外,编写for循环时,对于用于存储列表中每个值的临时变量,可指定任何名称。然而,选择描述单个列表元素的有意义的名称大有帮助。例如,对于小
猫列表,小狗列表和一般性列表,像下面这样编写for循环的第一行代码是不错的选择:
    for dog in dogs:
    for cat in cats:
    for item in _of_items:
    这些命名约定有助于我们明白for循环中将对每个元素执行的操作。使用单数和复数式名称,可帮助我们判断代码处理的是单个列表元素还是整个列表。
4.1.2 在for循环中执行更多的操作在for循环中,可对每个元素执行任何操作。
Copy after login
magicians = ["alice","david","carolina"]
for magician in magicians:
print(magician.title() + ", that was a greet trick!")
    下面输出结果表明,对于列表中的每位魔术师,都打印了一条个性化消息:
Alice, that was a greet trick!
David, that was a greet trick!
Carolina, that was a greet trick!在for循环中,想包含多少行代码都可以。在代码行for magician in magicians后面,每个缩进的代码行都是循环的一部分,且将针对列表中的每个值都
执行一次。因此,可对列表中能够的每个值执行任意次数的操作。
下面再添加一行代码,告诉每位魔术师,我们期待他的下一次表演:
Copy after login
magicians = ["alice","david","carolina"]
for magician in magicians:
print(magician.title() + ", that was a greet trick!")
print("I can't wait to see your  trick, " + magician.title() + ".\n" )
    由于两条print语句都缩进了,因此它们都将针对列表中的每位魔术师执行一次。第二条print语句中的换行符"\n"在每次迭代结束后都插入一
个空行,从而整洁地将针对各位魔术师的消息编组:
Alice, that was a greet trick!
I can't wait to see your next trick, Alice.
David, that was a greet trick!
I can't wait to see your next trick, David.
Carolina, that was a greet trick!
I can't wait to see your next trick, Carolina! 在for循环中,想包含多少行代码都可以。实际上,我们会发现使用for循环对每个元素执行众多不同的操作很有用。
4.1.3  在for循环结束后执行一些操作for循环结束后再怎么做呢?通常,我们需要提出总结性输出或接着执行程序必须完成的其他任务。
    在for循环后面,没有缩进的代码都只执行一次,而不会重复执行。下面来打印一条向全体魔术师致谢的消息,感谢它们的精彩表演。想要在打印给各位
魔术师的消息后面打印一条给全体魔术师的致谢消息,需要将相应的代码放在for循环后面,且不缩进:
Copy after login
magicians = ["alice","david","carolina"]
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n" )
print("Thank you,everyone. That was a great magic show!")
    我们在前面看到了,开头两条print语句针对列表中每位魔术师重复执行。然而,由于第三条print语句没有缩进,因此只执行一次:
Alice, that was a great trick!
I can't wait to see your next trick, Alice.
David, that was a great trick!
I can't wait to see your next trick, David.
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
Thank you,everyone. That was a great magic show!使用for循环处理数据是一种对执行整体操作的不错的方式。例如,你可能使用for循环来初始化游戏——遍历角色列表,将每个角色都显示到屏幕
上;再在循环后面添加一个不缩进的代码块,在屏幕上绘制所有角色后显示一个play Now。
4.2  避免缩进错误 Python根据缩进来判断代码行与前一个代码行的关系。在前面的实例中,向各位魔术师显示消息的代码行是for循环的一部分,因为它们缩进了。Python通
过缩进让代码更易读;简单地说,它要求我们使用缩进让代码整洁而结构清晰。在较长的Python程序中,我们将看到缩进程度各不相同的代码块,这让我们队程
序的组织结构有大致的认识。
    当我们开始编写必须正确缩进的代码时,需要注意一些常见的缩进错误。例如,有时候,程序员会将不需要缩进的代码块缩进,而对于必须缩进的代码块却
忘了缩进。通过查看这样的错误示例,有助于我们以后避开它们,以及在它们出现在程序中时进行修复。
4.2.1  忘记缩进对于位于后面且属于循环组成部分的代码行,一定要缩进。如果我们忘记缩进,Python会提醒:
Copy after login
magicians = ["alice","david","carolina"]
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print语句应缩进却没有缩进。Python没有找到期望缩进的代码块时,会让我们知道代码行有问题。
   "/home/zhuzhu/title4/magicians.py", line 3
    print(magician.title() + ", that was a great trick!")
        ^
IndentationError: expected an indented block提示我们代码没有缩进,预期应该出现缩进的,通常,将紧跟在for语句后面的代码行缩进,可消除这种缩进错误。
4.2.2  忘记缩进额外的代码行
Copy after login
magicians = ["alice","david","carolina"]
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n" )
Copy after login
magicians = ["alice","david","carolina"]
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n" )
print("Thank you,everyone. That was a great magic show!") 由于第三条print代码行被缩进,它将针对列表中的每位魔术师执行一次
Alice, that was a great trick!
I can't wait to see your next trick, Alice.
Thank you,everyone. That was a great magic show!
David, that was a great trick!
I can't wait to see your next trick, David.
Thank you,everyone. That was a great magic show!
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
Thank you,everyone. That was a great magic show!
    这也是一个逻辑错误,与4.2.2节的错误类似。Python不知道你的本意,只要代码符合语法,它就会运行。如果原本只应执行一次的操作执行了多次,请确
定我们是否不应该缩进执行该操作的代码。
4.2.5  遗漏了冒号:for语句在末尾冒号告诉Python,下一行是循环的第一行。
Copy after login
magicians = ["alice","david","carolina"]
for magician in magicians
print(magician.title() + ", that was a great trick!")
运行:
 File "/home/zhuzhu/title4/magicians.py", line 2
    for magician in magicians
                            ^
SyntaxError: invalid syntax (语法无效)如果我们不小心遗漏了冒号,将导致,运行会提示无效语法,因为Python不知道我们意欲何为。这种错误虽然易于消除,但并不那么容
易发现。程序员为找出这样的单字符错误,花费的时间多的令人惊讶。这样的错误之所以难以发现,是因为通常在我们的意料之外。
动手练一练
Copy after login
运行结果如下:
Copy after login
animals = ["lion","tiger","wolf","leopard"]
for animal in animals:
print(animal.title() + "s are good at hunting.")
print("Any of these animals like eating meat!")
首先定义了一个动物列表,使用for循环遍历列表中每一个动物,第一个print打印所以动物的一个共性,属于for循环的;第二个print在for循环外
面,当运行结束for循环后进行运行,属于总结性的语言,所以只会打印一次:
Lions are good at hunting.
Tigers are good at hunting.
Wolfs are good at hunting.
Leopards are good at hunting.
Any of these animals like eating meat!
4.3  创建数值列表 需要存储一组数字的原因有很多,例如,在游戏中,需要跟踪每个角色的位置,还可能需要跟踪玩家的几个最高得分。在数据可视化中,处理的几乎都是
数字(如温度、距离、人口数量、经度和纬度等等)组成的集合。
    列表非常适合用于存储数字集合,而且Python提供了很多工具,可帮助我们处理数字列表。明白如何有效地使用这些工具后,即便包含数百万个元素,我们
编写的代码也能很好的运行。
4.3.1  ()
Copy after login
for value in range(1,6):
    print(value)
这样,输出将从1开始,到5结束:
1
2
3
4
5
使用range()函数时,如果输出不符合预期,请尝试将指定的值加1或减1.

4.3.2  使用range()函数创建数字列表
Copy after login
numbers = list(range(1,6))
print(numbers)
结果如下:
[1, 2, 3, 4, 5]
使用函数range()时,还可指定步长。例如,下面的代码打印1~10内的奇数:
Copy after login
numbers = list(range(1,10,2))
print(numbers)
    在这个实例中,函数range()从2开始数,然后不断地加2,直到达到或超过终值(10),因此输出的结果如下:
[1, 3, 5, 7, 9]
 使用函数range()几乎能够创建任何需要的数字集,例如,如何创建一个列表,其中包含前10个(既1~10)的平方呢?在Python中,两个星号(**)表示
乘方运算。下面的代码演示了如何将前10个整数的平方加入到一个列表中:
Copy after login
squares = []
for num in range(1,11):
    square = num**2
    squares.append(square)
print(squares)首先,我们创建了一个空列表;接下来,使用函数range()让Python遍历1~10的值。在循环中,计算当前值的平方,并将结果存储到变量square中,然后
将新计算的到的平方值附加到列表squares的末尾。最后,循环结束后,打印列表squares
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
    创建更复杂的列表时,可使用上述两种方法中的任何一种。有时候,使用临时变量会让代码更易读;而在其他情况下,这样只会让代码无所谓地变长。我们
应该考虑的是,编写清晰易懂且能完成所需功能的代码;等到代码时,在考虑采用更高效的方法。
Copy after login
squares = [value**2 for value in range(1,11)]
print(squares) 要使用这种语法,首先指定一个描述性的列表名,如squares;然后指定一个左方括号,并定义一个,用于生成你要存储得到列表中的值。在这个示
例中,表达式为value**2,它计算平方值。接下来,编写一个for循环,用于给表达式提供值,再加上右方括号。在这个实例中,for循环为for value in 
range(1,11),它将值1~10提供给表达式value**2.请注意,这里的for语句末尾没有冒号。
    结果与我们在前面看到的平方数列表相同:
    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
    要创建自己的列表解析,需要经过一定的练习,但能够熟练地创建常规列表后,你会发现这样做是完全值得的。当你觉得编写三四行代码来生成列表有点繁
复时,就应该考虑创建列表解析了。
    其实,我挺喜欢列表解析的,这种方法太省事了,又不用定义列表,只需要把语句写在括号里面,好省事,很方便,要多尝试使用这种方式,创建简短的数
列表表示方式。
动手试一试
    4-3 数到20:使用一个for循环打印数字1~20(含);
    4-4 一百万:创建一个列表,其中包含数字1~1000000,在使用一个for循环将这些数字打印出来(如果输出的时间太长,按Ctrl + C停止输出,或关闭
输出窗口)。
    4-5 计算1~1000000的总和:创建一个列表,其中包含数字1~1000000,在使用min()函数和max()函数核实该列表确实是从1开始,从1000000结束的。
另外,对这个列表调用函数sum(),看看Python将一百万个数字相加需要多长时间。
    4-6 奇数: 通过给函数range()指定第三个参数来创建一个列表,其中包含1~20的奇数;在使用一个for循环将这些数字都打印出来;
    4-7 3的倍数: 创建一个列表,其中包含3~30内能被3整除的数字;在使用一个for循环将这个列表中的数字都打印出来;
    4-8 立方: 将同一个数字乘三次成为立方。例如,在Python中,2的立方用2**3表示。请创建一个列表,其中包含前10个整数(即1~10)的立方,在使用
一个for循环将这些立方数都打印出来;
    4-9立方解析:使用列表解析生成一个列表,其中包含前10个整数的立方。
分析:里面的试题主要考察我们for循环的使用,已经min()函数、max()函数和sum()函数等,要多尝试使用列表解析,因为我觉得这真是一个很好的方法,只需
一步就能产生我们需要的列表,要记住函数range()的差一性,list()函数是生成列表的。表达式的情况,对数字列表进行操作。
4-3:本题就是练习我们for循环的使用,还有range()函数的差一性for num in range(1,21):
print(num)4-4:本题还是锻炼我们for循环的使用情况,以及range()函数的差一性,但是想告诉我们,Python可以处理很大的数字,并不简单是书上很小的数
Copy after login
for num in range(1,1000001):
print(num)运行的时候,遍历1~1000000还是花费了十几秒的时间,要记得差一性,比我们想要的数加一
4-5:本题是要使用list()函数创建列表,然后验证最大值,最小值,并对数字进行求和
Copy after login
numbers =
Copy after login
numbers = []
#首先创建一个空列表用于存储生成的列表数字
for num in range(1,11):
    number = num * 3
    numbers.append(number)
print(numbers)
digits = [value * 3 for value in range(1,11)]
print(digits)
Copy after login
第一种方法,由于要生成一个列表,首先我们定义了一个空的列表,然后定义一个循环,把每次生成的数值添加到这个空列表的末尾,最后生成我们所需的列表
,第二种方法是使用列表解析,可以看出,我们只需要一条语句就生成了一个数字列表,只要我们知道了列表的表达式就可。
4-8:本题跟上一题相似,只是列表表达式的方式不一样而已,解决的思路都是一样的
Copy after login
numbers = []
#首先创建一个空列表用于存储生成的列表数字
for num in range(1,11):
    number = num ** 3
    numbers.append(number)
print(numbers)
digits = [value ** 3 for value in range(1,11)]
print(digits)
Copy after login

When using assignment to generate friend_foods, my_foods and friend_food s shares a memory, and no new memory is opened in Python. The memory, as shown in the picture above, is just related together, so when we assign a value, it becomes like this:

Python learning from the introductory operation list

When we add a new element to the my_foods list when we add elements to friend_foods, the new elements are also added to the original memory list, so when assignment is used, Python will not open up new memory, but will just merge the two The two variables are related together, as shown in the figure above, so the final results of the two are still the same.

Let’s look at the situation when using slices:

Python learning from the introductory operation list

Since slicing is used, slicing opens up a new memory list for storing friend_foods , when we add a new element, as shown below:

Python learning from the introductory operation list

Since it is a list of two different memory allocations, when adding a new element to my_foods, only add it to in the memory list of my_foods; when adding new elements to friend_foods, new elements are only added to the memory list to which friend_foods belongs, so the output results are different.

Therefore, the operating mechanisms of assignment and slicing in Python are different. When we assign values, we must pay attention to this. We did not pay much attention to it before, but we will pay attention to it in the future. Especially when copying operations.

Summary: When we make one parameter equal to another parameter, directly a = b, this is an assignment. When an assignment is made between two parameters, it is a shared memory, Python

There is no new space inside.

Give it a try

4-10 Slicing: Choose a program you wrote in this chapter and add a few lines of code at the end to complete the following tasks.

1. Print the message "The first three items in the list are:", use slicing to print the first three elements of the list;

2. Print the message "Three items from the middle of list are: ", use slicing to print the three elements in the middle of the list;

3. Print the message "The last three items in the list are: ", use slicing to print the last three elements of the list .

4-11 Your Pizza and My Pizza: In the program you wrote to complete Exercise 4-1, create a copy of the pizza list and store it in the variable friend_pizzas. Complete the following tasks .

1. Add a pizza to the original pizza list;

2. Add another pizza to the friend_pizzas list;

3. Verify that you have two Different lists. To do this, print the message "My favorite pizzas are:", and then use a for loop to print the second list. Verify that added pizzas are added to the correct list.

4-10: This question mainly examines the representation method of list slices. How to represent list slices, especially L.[-1] represents the last element. The use of this method is very critical. Examine the last few elements. It will be used every time. The representation of list slicing is from the number of elements to the number of elements, separated by colons in the middle.

digits = [2,3,8,9,0,3,5,7]
print("The first three items in the list are: ")
print(digits[: 3])
print("\nThree items from the middle of the list are: ")
print(digits[2:5])
print("\nThe last three items in the list are: ")
print(digits[-3:])

The running result is as follows:

The first three items in the list are:
[2, 3, 8]
Three items from the middle of the list are:
[8, 9, 0]
The last three items in the list are:
[3, 5, 7]

4-11 This question mainly examines the use of slicing and the method of copying lists. List copying must be completed through slicing, which is determined by the working principle of Python.

pizzas = ["New York Style","Chicago Style","California Style"]
friend_pizzas = pizzas[:]
pizzas.append("noodle" )
friend_pizzas.append("rice")
print("My favorite pizzas are: ")
for pizza in pizzas:
print(pizza)
print(pizzas)

print("\nMy friend's favorite pizzas are: ")
for friend_pizza in friend_pizzas:
print(friend_pizza)
print(friend_pizzas)

Run result:

My favorite pizzas are:
New York Style
Chicago Style
California Style
noodle
['New York Style', 'Chicago Style', 'California Style', 'noodle' ]
My friend's favorite pizzas are:
New York Style
Chicago Style
California Style
rice
['New York Style', 'Chicago Style', 'California Style', 'rice']

4.5 Yuanzu

Lists are ideal for storing data sets that may change while the program is running. Lists are modifiable, which is crucial when working with a list of users for a website or a list of characters in a game. However, sometimes we need to create a series of immutable elements, and Yuanzu can meet this need. Python calls values ​​that cannot be modified immutable, and immutable lists are called primitives.

4.5.1 Definition of Yuanzu

Yuanzu looks like a list, but is marked with parentheses instead of square brackets. Once a tuple is defined, its elements can be accessed using indexes, just like list elements.

For example, if you have a rectangle whose size should not change, you can store its length and width in a tuple, ensuring that they cannot be modified:

dimensions.py

dimensions = (200,50)
print(dimensions[0])
print(dimensions[1])

We first define the original dimensions, for which we use parentheses instead of square brackets. Next, we print each element of the tuple separately, using the same syntax we use when accessing list elements:

200

50

We know that the tuple is by definition The elements cannot be modified. Let's try to modify an element in the element dimensions and see what the result is:

dimensions = (200,50)
dimensions[0] = 250
print(dimensions) The code attempts to modify the value of the first element, causing Python to return a type error message. Since attempts to modify the ancestor are prohibited, Python points out that values ​​cannot be assigned to elements of the ancestor:

Traceback (most recent call last):
File "/home/zhuzhu/title4/dimensions.py ", line 2, in module>
dimensions[0] = 250
TypeError: 'tuple' object does not support item assignment

Python reports an error when the code tries to modify the dimensions of the rectangle, which is good because that’s what we want.

4.5.2 Traverse all values ​​in the ancestor

像列表一样,也可以使用for循环来遍历元组中的所有值:

    dimensions = (200,50)
    for dimension in dimensions:
            print(dimension)

    就像遍历列表时一样,Python返回元组中所有的元素:

    200

    50

4.5.3  修改元组变量

    虽然不能修改元组的元素,但可以给存储元组的变量赋值。因此,如果要修改前述矩形的尺寸,可重新定义整个元组:

    dimensions = (200,50)
    print("Original dimensions: ")
    for dimension in dimensions:
           print(dimension)
    dimensions = (400,100)
    print("\nModified dimensions: ")
    for dimension in dimensions:
            print(dimension)

    我们首先定义了一个元组,并将其存储的尺寸打印了出来;接下来,将一个新元组存储到变量dimensions中;然后,打印新的尺寸。这次,Python不会报告任何错误,因为给元组变量赋值是合法的:

    Original dimensions:
    200
    50
    Modified dimensions:
    400
    100

    相比于列表,元组是更简单的数据结构.如果需要存储的一组值在程序的整个生命周期内都不变,可使用元组。

动手试一试

    4-13 自助餐: 有一家自助式餐厅,只提供五种简单的食品。请想出五种简单的食品,并将其存储在一个元祖中。

    1、使用一个for循环将该餐馆提供的五种食品都打印出来;

    2、尝试修改其中的一个元素,核实Python确实会拒绝我们这样做;

    3、餐馆调整了菜单,替换了它提供的某中两种实物。请编写一个这样的代码块;给元组变量赋值,并使用一个for循环将新元组中的每个元素都打印出来。

    分析:本题主要考察元组的性质及用法,我们知道,元组中的元素是不可以修改的,但是元组是可以重新赋值的,我们虽然不能修改元组中的元素,但是我们可以整体修改元组,元组其他的性质跟列表的性质都是一样的,就是不能对元组本身就行修改,比如添加,删除,赋值等。

    cafeterias = ("rice","noodle","porridge","hamburger","pizza")
    for cafeteria in cafeterias:
            print(cafeteria)
    cafeterias[-1] = "dami"

    运行结果如下:

rice

noodle
porridge
hamburger
pizza
Traceback (most recent call last):
  File "/home/zhuzhu/title4/Cafeterias.py", line 5, in
    cafeterias[-1] = "dami"
TypeError: 'tuple' object does not support item assignment

从运行结果可以看出,当我们试图修改元组的元素是,发生了错误,提示我们不能修改元组的元组值

cafeterias = ("rice","noodle","porridge","hamburger","pizza")
print("Original menu: ")
for cafeteria in cafeterias:
    print(cafeteria)
#cafeterias[-1] = "dami"
cafeterias = ("pizza",'hamburger','rice','milk','Drumsticks')
print("\nModified menu: ")
for cafeteria in cafeterias:print(cafeteria)运行结果如下:Original menu: 
rice
noodle
porridge
hamburger
pizza
Modified menu: 
pizza
hamburger
rice
milk
Drumsticks
从运行结果可以看出,虽然我们不能修改元组中元素的值,但我们可以整体重新给元组进行赋值,这样是允许的,因而我们可以对元组重新赋值,这是允许的,
也是合法的,系统不会报错。
4.6  设置代码格式    随着我们编写的程序越来越长,有必要了解一些代码格式设置的约定。请花时间让我们的代码进可能易于阅读;让代码易于阅读有助于我们掌握程序是做什
么的,也可以帮助他人理解我们编写的代码。
    为确保所有人编写的代码的结构都大致一致,Python程序员都遵循一些格式设置约定。学会编写整洁的Python后,就能明白他人编写的Python代码的整体
结构——只要他们和你遵循相同的指南。要称为专业程序员,应从现在开始就遵循这些指南,以养成良好的习惯。

4.6.1  格式设置指南若要提出Python语言修改建议,需要编写Python改进提案(Python Enhancement Proposal,PEP).PEP8是最古老的PEP之一,它向Python程序员提供了代
码格式设置指南。PEP8的篇幅很长,但大都与复杂的编码结构相关。
    Python格式设置指南的编写者深知,代码被阅读的次数比编写的次数多。代码编写出来后,调试时你需要阅读它;给程序添加新功能时,需要花很长的时间
阅读代码;与其他程序员分享代码时,这些程序员也将阅读它们。
    如果一定要在代码易于编写和易于阅读之间做出选择,Python程序员几乎总是选择后者。下面的指南可帮助我们从一开始就编写除清晰的代码。
4.6.2  缩进
    PEP 8建议每级缩进都使用四个空格,这即可提高可读性,又留下了足够的多级缩进空间。
    在字处理文档中,大家常常使用制表符而不是空格来缩进。对于字处理文档来说,这样做的效果很好,但混合使用制表符和空格会让Python解释器感到迷惑
。每款文本都提供了一种设置,可将输入的制表符转换为指定数量的空格。我们在编写代码时应该使用制表符键,但一定要对编辑器进行设置,使其在文
档中插入空格而不是制表符。
    在程序中混合制表符和空格可能导致极难解决的问题。如果我们混合使用了制表符和空格,可将文件中所有的制表符转换为空格,大多数编辑器都提供了这
样的功能。
4.6.3 行长
    很多Python程序员都建议每行不超过80字符。最初制定这样的指南时,在大多数计算机中,终端窗口每行只能容纳79字符;当前,计算机屏幕每行可容纳的
字符数多得多,为何还要使用79字符的标准行长呢?这里有别的原因。专业程序员通常会在同一个屏幕上打开多个文件,使用标准行长可以让我们在屏幕上并排
打开两三个文件时能同时看到各个文件的整行。PEP 8还建议的行长都不超过72字符,因为有些工具为大型项目自动生成文档时,会在每行注释开头添加格式
化字符。
    PEP 8中有关行长的指南并非不可逾越的红线,有些小组将最大行长设置为99字符。在学习期间,我们不用过多考虑代码的行长,但别忘了,协作编写程序,
大家几乎都遵守PEP 8指南。在大多数编辑器中,都可设置一个视觉标志——通常是一条竖线,让你知道不能越过的界限在什么地方。
4.6.4  空行
要将程序不同部分分开,可使用空行。我们应该使用空行来组织程序文件,但也不能滥用;只要按本书的示例展示的那样做,就能掌握其中的平衡。例如,
如果我们有5行创建列表的代码,还有3行处理该列表的代码,那么用一个空行将这两部分隔开是合适的。然而,我们不应该使用三四个空行将它们隔开。
    空行不会影响代码的运行,但会影响代码的可读性。Python解释器根据水平缩进情况来解读代码,但不关心垂直间距。
Copy after login

Original menu:
rice
noodle
porridge
hamburger
pizza
Modified menu:
pizza
hamburger
rice
milk
Drumsticks

The above is the detailed content of Python learning from the introductory operation list. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!