一、作业内容
笨办法学 Python 习题0-10以及加分题。
二、作业代码:
习题 0就是上一篇文章,【Python爬虫】-第一个脚本
# 习题 1: 第一个程序
print("Hello World!")
print("Hello Again")
print("I like typing this.")
print("This is fun.")
print("Yay! printing.")
print("I'd much rather you 'not'.")
print('I "said" do not touch this.')
# 加分习题
# 一、让你的脚本再多打印一行。
# 使用 \n 换单行
print('I "said" do not\ntouch this.')
# 使用三引号'''...''' 换多行
print('''I
"said"
do
not
touch
this.''''')
# 二、让你的脚本只打印一行。
print("Hello World!")
print("Hello Again")
print("I like typing this.")
# 删除其他的print,将多个字符串合并为一个字符串,用反斜杠表示字符串将在下一行继续,并只打印一行
print("Hello World! \
Hello Again. \
I like typing this.")
# 三、在一行的起始位置放一个 ‘#’ (octothorpe) 符号。它的作用是什么?自己研究一下。
# print("Hello World!")
‘#’ 的作用是注释。从运行的结果来看,如果不想让代码起到作用,又不想删除代码,也可以使用注释符号。
在《简明 Python 教程》中,是这么说明的:
你应该在你的程序中尽可能多地使用有用的注释:
- 解释假设
- 说明重要的决定
- 解释重要的细节
- 说明你想要解决的问题
- 说明你想要在程序中克服的问题,等等。
# 习题 2: 注释和井号
# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print("I could have code like this.") # and the comment after is ignored
# You can also use a comment to "disable" or comment out a piece of code:
# print("This won't run.")
print("This will run.")
加分习题
# 一、弄清楚”#”符号的作用。而且记住它的名字。(中文为井号,英文为 octothorpe 或者 pound character)。
# 二、打开你的 ex2.py 文件,从后往前逐行检查。从最后一行开始,倒着逐个单词单词检查回去。
# 三、有没有发现什么错误呢?有的话就改正过来.
# 四、朗读你写的习题,把每个字符都读出来。有没有发现更多的错误呢?有的话也一样改正过来。
习题 3: 数字和数学计算
# 打印字符串 I will now count my chickens:
print("I will now count my chickens:")
# 打印字符串 Hens和30 除以 6,输出的浮点数与25相加的结果
print("Hens", 25 + 30 / 6)
# 打印字符串 Roosters,给出25与3的乘积,并将乘积返回除法运算后的余数
print("Roosters", 100 - 25 * 3 % 4)
# 打印字符串 Now I will count the eggs:
print("Now I will count the eggs:")
# 打印 运算的结果
print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)
# 打印字符串 Is it true that 3 + 2 < 5 - 7?
print("Is it true that 3 + 2 < 5 - 7?")
# 打印 运算结果的布尔值
print(3 + 2 < 5 - 7)
# 打印字符串 What is 3 + 2? 和整数3与整数2相加运算的结果
print("What is 3 + 2?", 3 + 2)
# 打印字符串 What is 5 - 7 和整数5与整数7相减运算的结果
print("What is 5 - 7?", 5 -7)
# 打印字符串 Oh, that's why it's False.
print("Oh, that's why it's False.")
# 打印字符串 How about some more.
print("How about some more.")
# 打印字符串 Is it greater? 和布尔值
print("Is it greater?", 5 > -2)
# 打印字符串 Is it greater or equal? 和布尔值
print("Is it greater or equal?", 5 >= -2)
# 打印字符串 Is it less or equal? 和布尔值
print("Is it less or equal?", 5 <= -2)
# 加分习题
# 一、使用 # 在代码每一行的前一行为自己写一个注解,说明一下这一行的作用。
# 注解已在上方的输入内容中。
# 二、记得开始时的 <练习 0> 吧?用里边的方法把 Python 运行起来,然后使用刚才学到的运算符号,把Python当做计算器玩玩。
>>> 10 / 3
3.3333333333333335
>>> 9 / 3
3.0
>>> 10 // 3
3
>>> 10 % 3
1
# 三、自己找个想要计算的东西,写一个 .py 文件把它计算出来。
# 略。
# 四、有没有发现计算结果是”错”的呢?计算结果只有整数,没有小数部分。研究一下这是为什么,搜索一下“浮点数(floating point number)”是什么东西。
# 浮点数也就是小数,之所以称为浮点数。
# 是因为按照科学记数法表示时,一个浮点数的小数点位置是可变的。
# 五、使用浮点数重写一遍 ex3.py,让它的计算结果更准确(提示: 20.0 是一个浮点数)。
print("I will now count my chickens:")
print("Hens", 25 + 30 / 6)
print("Roosters", 100 - 25 * 3.0 % 4)
print("Now I will count the eggs:")
print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)
print("Is it true that 3.0 + 2.0 < 5.0 - 7.0?")
print(3.0 + 2.0 < 5.0 - 7.0)
print("What is 3.0 + 2.0?", 3.0 + 2)
print("What is 5.0 - 7.0?", 5.0 -7.0)
print("Oh, that's why it's False.")
print("How about some more.")
print("Is it greater?", 5.0 > -2.0)
print("Is it greater or equal?", 5.0 >= -2.0)
print("Is it less or equal?", 5.0 <= -2.0)
# 习题 4: 变量(variable)和命名
# 输入习题中的内容。在每一行的上面写一行注解,给自己解释一下这一行的作用。 倒着读你的 .py 文件。
# 朗读你的 .py 文件,将每个字符也朗读出来。
# cars 是整数
cars = 100
# space_in_a_car 是浮点数
space_in_a_car = 4.0
# drivers 是整数
drivers = 30
# passengers 是整数
passengers = 90
# 从 cars 中减去 drivers,输出的结果赋值给变量 cars_not_driven
cars_not_driven = cars - drivers
# 把变量 drivers 赋值给变量 cars_driven
cars_driven = drivers
# 给出 cars_driven 和 space_in_a_car 的乘积,并赋值给变量 carpool_capacity
carpool_capacity = cars_driven * space_in_a_car
# passengers 除以 cars_driven,输出的结果赋值给变量 average_passengers_per_car
average_passengers_per_car = passengers / cars_driven
# 打印字符串 There are,变量 cars 和字符串 cars available.
print("There are", cars, "cars available.")
# 打印字符串 There are only,变量 drivers 和字符串 drivers available.
print("There are only", drivers, "drivers available.")
# 打印字符串 There will be,变量 cars_not_driven 和字符串 empty cars today.
print("There will be", cars_not_driven, "empty cars today.")
# 打印字符串 We can transport,变量 carpool_capacity 和字符串 people today.
print("We can transport", carpool_capacity, "people today.")
# 打印字符串 We have,变量 passengers 和字符串 to carpool today.
print("We have", passengers, "to carpool today.")
# 打印字符串 We need to put about,变量 average_passengers_per_car 和字符串 in each car.
print("We need to put about", average_passengers_per_car, "in each car.")
# 更多的加分习题:
# 一、我在程序里用了 4.0 作为 space_in_a_car 的值,这样做有必要吗?如果只用 4 会有什么问题?
# 修改了编辑器的代码,观察运行的结果并得出结论:有必要。如果只用4,得到的结果就不是浮点数了。因为4.0是一个浮点数,4是一个整数。
# 二、记住 4.0 是一个“浮点数”,自己研究一下这是什么意思。
# 浮点数就是小数,按照科学计数法来表示时,一个浮点数的小数点位置是可变的。
# 三、在每一个变量赋值的上一行加上一行注解。
# 每一个变量赋值的注解已添加在上方的输入内容中。
# 四、记住 = 的名字是等于(equal),它的作用是为东西取名。
# 五、记住 _ 是下划线字符(underscore)。
# 六、将 python 作为计算器运行起来,就跟以前一样,不过这一次在计算过程中使用变量名来做计算,常见的变量名有 i, x, j 等等。
>>> i = 100
>>> x = 4.0
>>> j = 30
>>> r = 90
>>> cars_not_driven = i - j
>>> cars_driven = j
>>> carpool_capacity = cars_driven * x
>>> average_passengers_per_car = r / cars_driven
>>>
>>>
>>> print("There are", i, "cars available.")
There are 100 cars available.
>>> print("There are only", j, "drivers available.")
There are only 30 drivers available.
>>> print("There will be", cars_not_driven, "empty cars today.")
There will be 70 empty cars today.
>>> print("We can transport", carpool_capacity, "people today.")
We can transport 120.0 people today.
>>> print("We have", 90, "to carpool today.")
We have 90 to carpool today.
>>> print("We need to put about", average_passengers_per_car, "in each car.")
We need to put about 3.0 in each car.
# 习题 5: 更多的变量和打印
my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
print("Let's talk about %s." % my_name)
print("He's %s inches tall." % my_height)
print("He's %d pounds heavy." % my_weight)
print("Actually that's not too heavy.")
print("He's got %s eyes and %s hair." % (my_eyes, my_hair))
print("His teeth are usually %s depending on the coffee." % my_teeth)
# this line is tricky, try to get it exactly right
print("If I add %d, %d, and %d I get %d." % (
my_age, my_height, my_weight, my_age + my_height + my_weight))
# 加分习题
# 一、修改所有的变量名字,把它们前面的``my_``去掉。确认将每一个地方的都改掉,不只是你使用``=``赋值过的地方。
# 二、试着使用更多的格式化字符。例如`` %r ``就是是非常有用的一个,它的含义是“不管什么都打印出来”。
name = 'Zed A. Shaw'
age = 35 # not a lie
height = 74 # inches
weight = 180 # lbs
eyes = 'Blue'
teeth = 'White'
hair = 'Brown'
print("Let's talk about %r." % name)
print("He's %r inches tall." % height)
print("He's %r pounds heavy." % weight)
print("Actually that's not too heavy.")
print("He's got %r eyes and %r hair." % (eyes, hair))
print("His teeth are usually %r depending on the coffee." % teeth)
# this line is tricky, try to get it exactly right
print("If I add %r, %r, and %r I get %r." % (
age, height, weight, age + height + weight))
# 三、在网上搜索所有的 Python 格式化字符。
# Python 格式化字符:
# 整数
%d
# 浮点数
%f
# 字符串
%s
# 十六进制整数
%x
# 四、试着使用变量将英寸和磅转换成厘米和千克。不要直接键入答案。使用 Python 的计算功能来完成。
height = 74 # inches
weight = 180 # lbs
transfer_height = 2.54 * height # cm
transfer_weight = 0.4535924 * weight # kg
print("He's %r inches tall, %r cm." % (height, transfer_height))
print("He's %r pounds heavy, %r kg." % (weight, transfer_weight))
# 习题 6: 字符串(string)和文本
x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)
print(x)
print(y)
print("I said: %r." % x)
print("I also said: '%s'." % y)
hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"
print(joke_evaluation % hilarious)
w = "This is the left side of..."
e = "a string with a right side."
print(w + e)
# 加分习题
# 一、通读程序,在每一行的上面写一行注解,给自己解释一下这一行的作用。
# 把格式化的变量 %d 放进字符串 There are types of people. 并把字符串赋值给变量 x,变量名为10
x = "There are %d types of people." % 10
# 把字符串 binary 赋值给变量 binary
binary = "binary"
# 把字符串 don't 赋值给变量 do_not
do_not = "don't"
# 把两个格式化的变量 %s 放进字符串 Those who know and those who. 并把字符串赋值给变量 y,变量名分别为binary,do_not
y = "Those who know %s and those who %s." % (binary, do_not)
# 打印变量 x
print(x)
# 打印变量 y
print(y)
# 打印含有格式化变量 %r 的字符串 I said: 变量名为 x
print("I said: %r." % x)
# 打印含有格式化变量 %s 的字符串 I also said: 变量名为 y
print("I also said: '%s'." % y)
# 变量 hilarious 是一个布尔值 False
hilarious = False
# 把格式化的变量 %r 放进字符串 Isn't that joke so funny?! 并把字符串赋值给变量 joke_evaluation
joke_evaluation = "Isn't that joke so funny?! %r"
# 打印变量 joke_evaluation ,变量名为 hilarious
print(joke_evaluation % hilarious)
# 将字符串 This is the left side of... 赋值给变量 w
w = "This is the left side of..."
# 将字符串 a string with a right side. 赋值给变量 e
e = "a string with a right side."
# 打印变量 w 和变量 e
print(w + e)
# 二、找到所有的”字符串包含字符串”的位置,总共有四个位置。
# 1、字符串 Those who know %s and those who %s.中包含名为 binary 的格式化变量%s,而变量 binary 包含字符串 binary
# 2、字符串 Those who know %s and those who %s.中包含名为 do_not 的格式化变量%s,而变量 do_not 包含字符串 don't
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)
# 3、字符串 There are %d types of people. 被赋值给变量x,而字符串 I said: %r.包含名为 x 的格式化变量 %r。
x = "There are %d types of people." % 10
print("I said: %r." % x)
# 4、字符串 Those who know %s and those who %s.被赋值给变量y,而字符串 I also said: '%s'.包含名为 y 的格式化变量 %s
print("I also said: '%s'." % y)
# 三、你确定只有四个位置吗?你怎么知道的?没准我在骗你呢。
# 我检查了一下,应该是四个位置……
# 四、解释一下为什么 w 和 e 用 + 连起来就可以生成一个更长的字符串。
# 通过观察输入的内容:
# 将字符串 This is the left side of... 赋值给变量 w
w = "This is the left side of..."
# 将字符串 a string with a right side. 赋值给变量 e
e = "a string with a right side."
# 打印变量 w 和变量 e
print(w + e)
# 得出结论:因为字符串不是整数、浮点数,不能通过相加运算。
# 习题 7: 更多打印
print("Mary had a little lamb.")
print("Its fleece was white as %s." % 'snow')
print("And everywhere that Mary went.")
print("." * 10) # what'd that do?
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"
# watch that comma at the end. try removing it to see that happens
print(end1 + end2 + end3 + end4 + end5 + end6,)
print(end7 + end8 + end9 + end10 + end11 + end12)
这里有一个疑惑的地方,因为教程里的标准答案(运行的结果)是:
$ python ex7.py
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
..........
Cheese Burger
$
Cheese Burger 只打印了一行……不知道是不是Python 2 和Python 3 的区别?
# 加分习题
# 一、逆向阅读,在每一行的上面加一行注解。
# 打印字符串 Mary had a little lamb.
print("Mary had a little lamb.")
# 打印包含格式化变量 %s的字符串 Its fleece was white as %s. 变量名为字符串snow
print("Its fleece was white as %s." % 'snow')
# 打印字符串 And everywhere that Mary went.
print("And everywhere that Mary went.")
# 打印字符串.重复10次数后的结果
print("." * 10) # what'd that do?
# 将字符串 C 赋值给变量 end1
end1 = "C"
# 将字符串 h 赋值给变量 end2
end2 = "h"
# 将字符串 e 赋值给变量 end3
end3 = "e"
# 将字符串 e 赋值给变量 end4
end4 = "e"
# 将字符串 s 赋值给变量 end5
end5 = "s"
# 将字符串 e 赋值给变量 end6
end6 = "e"
# 将字符串 B 赋值给变量 end7
end7 = "B"
# 将字符串 u 赋值给变量 end8
end8 = "u"
# 将字符串 r 赋值给变量 end9
end9 = "r"
# 将字符串 g 赋值给变量 end10
end10 = "g"
# 将字符串 e 赋值给变量 end11
end11 = "e"
# 将字符串 r 赋值给变量 end12
end12 = "r"
# watch that comma at the end. try removing it to see that happens
# 因'a' + 'b' 则输出 'ab' 所以打印出变量 end1 至 变量 end6 的值,也就是字符串
print(end1 + end2 + end3 + end4 + end5 + end6,)
# 因'a' + 'b' 则输出 'ab' 所以打印出变量 end7 至变量 end12 的值,也就是字符串
print(end7 + end8 + end9 + end10 + end11 + end12)
# 二、倒着朗读出来,找出自己的错误。
# 三、从现在开始,把你的错误记录下来,写在一张纸上。
# 四、在开始下一节习题时,阅读一遍你记录下来的错误,并且尽量避免在下个练习中再犯同样的错误。
# 五、记住,每个人都会犯错误。程序员和魔术师一样,他们希望大家认为他们从不犯错,不过这只是表象而已,他们每时每刻都在犯错。
## 习题 8: 打印,打印
formatter = "%r %r %r %r"
print(formatter % (1, 2, 3, 4))
print(formatter % ("one", "two", "three", "four"))
print(formatter % (True, False, False, True))
print(formatter % (formatter, formatter, formatter, formatter))
print(formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
))
# 加分习题
# 一、自己检查结果,记录你犯过的错误,并且在下个练习中尽量不犯同样的错误。
# 二、注意最后一行程序中既有单引号又有双引号,你觉得它是如何工作的?
# 不清楚。
## 习题 9: 打印,打印,打印
# Here's some new strange stuff, remember type it exactly.
days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
print("Here are the days: ", days)
print("Here are the months: ", months)
print("""
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
""")
# 加分习题
# 一、自己检查结果,记录你犯过的错误,并且在下个练习中尽量不犯同样的错误。
# 习题 10: 那是什么?
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."
fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""
print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)
# 加分习题
# 一、上网搜索一下还有哪些可用的转义字符。
# 续行符,用于行尾
\
# 反斜杠符号
\\
# 单引号
\'
# 双引号
\"
# 响铃
\a
# 退格
\b
# 转义
\e
# 空
\000
# 换行
\n
# 纵向制表符
\v
# 横向制表符
\t
# 回车
\r
# 换页
\f
# 八进制数yy代表的字符,例如:\o12代表换行
\oyy
# 十进制yy代表的字符,例如:\x0a代表换行
\xyy
# 其他的字符以普通格式输出
\other
# 二、使用 ''' (三个单引号)取代三个双引号,看看效果是不是一样的?
fat_cat = '''
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
'''
print(fat_cat)
# 效果是一样的。
# 三、将转义序列和格式化字符串放到一起,创建一种更复杂的格式。
# 将包含格式化变量 %r 的字符串赋值给 变量formatter
formatter = "%r\n%r\000%r\'%r"
# 变量formatter的变量名分别为字符串 one、字符串two 、字符串 three和字符串 four
# 打印变量 formatter
print(formatter % ("one", "two", "three", "four"))
# 四、记得 %r 格式化字符串吗?
# 使用 %r 搭配单引号和双引号转义字符打印一些字符串出来。
# 将 %r 和 %s 比较一下。 注意到了吗?%r 打印出来的是你写在脚本里的内容,而 %s 打印的是你应该看到的内容。
# 将包含格式化变量 %r 的字符串赋值给 变量formatter
formatter = "%r\n%r\"%r\'%r"
print(formatter % (1, 2, 3, 4))
# 变量formatter的变量名分别为字符串 one、字符串two 、字符串 three和字符串 four
# 打印变量 formatter
print(formatter % ("one", "two", "three", "four"))
print(formatter % (True, False, False, True))
print(formatter % (formatter, formatter, formatter, formatter))
print(formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
))
# 将包含格式化变量 %s 的字符串赋值给 变量formatter
formatter = "%s\n%s\"%s\'%s"
print(formatter % (1, 2, 3, 4))
# 变量formatter的变量名分别为字符串 one、字符串two 、字符串 three和字符串 four
# 打印变量 formatter
print(formatter % ("one", "two", "three", "four"))
print(formatter % (True, False, False, True))
print(formatter % (formatter, formatter, formatter, formatter))
print(formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
))
# 对整数、布尔值、没有影响。
# 对字符串有影响。
三、学习总结
总结中,迟些时候再补上。