【Python爬虫】-笨办法学 Python 习题02-04

习题2:注释和井号

程序里的注释是很重要的。它们可以用自然语言告诉你某段代码的功能是什么。在你想要临时移除一段。代码时,你还可以用注解的方式将这段代码临时禁用。接下来的练习将让你学会注释:

1 # A comment, this is so you can read your program later.

2 # Anything after the # is ignored by python.

4 print "I could have code like this." # and the comment after is ignored

6 # You can also use a comment to "disable" or comment out a piece of code:

7 # print "This won't run."

9 print "This will run."

【Python爬虫】-笨办法学 Python 习题02-04_第1张图片

习题3:数字和数学计算

有没有注意到以上只是些符号,没有运算操作呢?写完下面的练习代码后,再回到上面的列表,写出每个符号的作用。例如+是用来做加法运算的。

12print "I will now count my chickens:"

3print "Hens", 25 + 30 / 6

4print "Roosters", 100 - 25 * 3 % 4

5

6print "Now I will count the eggs:"

78print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6

9 print "Is it true that 3 + 2 < 5 - 7?"

10

11 print 3 + 2 < 5 - 7

12

13 print "What is 3 + 2?", 3 + 2

14 print "What is 5 - 7?", 5 - 7

15

16 print "Oh, that's why it's False."

17

18 print "How about some more."

19

20 print "Is it greater?", 5 > -2

21 print "Is it greater or equal?", 5 >= -2

22 print "Is it less or equal?", 5 <= -2


【Python爬虫】-笨办法学 Python 习题02-04_第2张图片

习题4:变量(variable)和命名

你已经学会了print和算术运算。下一步你要学的是“变量”。在编程中,变量只不过是用来指代某个东西的名字。程序员通过使用变量名可以让他们的程序读起来更像英语。而且因为程序员的记性都不怎么地,变量名可以让他们更容易记住程序的内容。如果他们没有在写程序时使用好的变量名,在下一次读到原来写的代码时他们会大为头疼的。

如果你被这章习题难住了的话,记得我们之前教过的:找到不同点、注意细节。

1.在每一行的上面写一行注解,给自己解释一下这一行的作用。2.倒着读你的.py文件。

3.朗读你的.py文件,将每个字符也朗读出来。

12cars = 100

3space_in_a_car = 4.0

drivers = 30

4passengers = 90

5cars_not_driven = cars - drivers

6cars_driven = drivers

7carpool_capacity = cars_driven * space_in_a_car

8average_passengers_per_car = passengers / cars_driven


10 print "There are", cars, "cars available."

11 print "There are only", drivers, "drivers available."

12 print "There will be", cars_not_driven, "empty cars today."

13 print "We can transport", carpool_capacity, "people today."

14 print "We have", passengers, "to carpool today."

15 print "We need to put about", average_passengers_per_car, "in each car."

【Python爬虫】-笨办法学 Python 习题02-04_第3张图片

习题5:更多的变量和打印

我们现在要键入更多的变量并且把它们打印出来。这次我们将使用一个叫“格式化字符串(format

string)”的东西.每一次你使用"把一些文本引用起来,你就建立了一个字符串。字符串是程序将信息展示给人的方式。你可以打印它们,可以将它们写入文件,还可以将它们发送给网站服务器,很多事情都是通过字符串交流实现的。

字符串是非常好用的东西,所以再这个练习中你将学会如何创建包含变量内容的字符串。使用专门的格式和语法把变量的内容放到字符串里,相当于来告诉python:“嘿,这是一个格式化字符串,把这些

变量放到那几个位置。”

一样的,即使你读不懂这些内容,只要一字不差地键入就可以了。

12my_name = 'Zed A. Shaw'

3my_age = 35 # not a lie

my_height = 74 # inches

4  my_weight = 180 # lbs

5my_eyes = 'Blue'

6my_teeth = 'White'

7my_hair = 'Brown'

8print "Let's talk about %s." % my_name

9print "He's %d inches tall." % my_height

10print "He's %d pounds heavy." % my_weight11print "Actually that's not too heavy."12print "He's got %s eyes and %s hair." % (my_eyes, my_hair)13print "His teeth are usually %s depending on the coffee." % my_teeth

1415# this line is tricky, try to get it exactly right16print "If I add %d, %d, and %d I get %d." % (

17my_age, my_height, my_weight, my_age + my_height + my_weight)

18


【Python爬虫】-笨办法学 Python 习题02-04_第4张图片

习题6:字符串(string)和文本

12x = "There are %d types of people." % 10

3binary = "binary"

do_not = "don't"

4y = "Those who know %s and those who %s." % (binary, do_not)

5

6print x

7print y

8print "I said: %r." % x

9  print "I also said: '%s'." % y

10

11 hilarious = False

12 joke_evaluation = "Isn't that joke so funny?! %r"

13

14 print joke_evaluation % hilarious

15

16 w = "This is the left side of..."

17 e = "a string with a right side."

18

19 print w + e

20

你可能感兴趣的:(【Python爬虫】-笨办法学 Python 习题02-04)