- 习题1:第一个程序
# --coding:utf-8 --
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.')
加分习题
1.1 让你的脚本再多打印一行。
print("Hello World!\n")
print("Hello Again\n")
print("I like typing this.\n")
print("This is fun.\n")
print('Yay! Printing.\n')
print("I'd much rather you 'not'.\n")
print('I "said" do not touch this.\n')
1.2 让你的脚本只打印一行。
print('I "said" do not touch this.')
1.3 在一行的起始位置放一个 '#' (octothorpe) 符号。它的作用是什么?
# 在一行的起始位置放一个 '#' (octothorpe) 符号。
# 它的作用是注释,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 of a piece of code:
# print("This won't run.")
print("This will run.")
- 习题3:数字和数学计算
print("I will now count my chickens:")
print("Hens", 25 + 30 / 6)
print("Roosters", 100 - 25 * 3 % 4)
print("Now I will count the eggs:")
print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)
print("Is it ture that 3 + 2 < 5 - 7?")
print(3 + 2 < 5 - 7)
print("What is 3 + 2?", 3 + 2)
print("What is 5 - 7?", 5 - 7)
print("Oh, that's why it's False.")
print("How about some more.")
print("Is it greater?", 5 > -2)
print("Is it greater or equal?", 5 >= -2)
print("Is it less or equal?", 5 <= -2)
加分习题
3.4 有没有发现计算结果是”错”的呢?计算结果只有整数,没有小数部分。研究一下这是为什么,搜索一下“浮点数(floating point number)”是什么东西。
python将带小数点的数字都称为浮点数。在Python2中 计算3/2会得出结果为1,整数除法的结果只包含整数部分,小数部分被删除。
若要避免这种情况,可使用Python3,或者再计算公式中至少保证有一个操作数为浮点数。这样结果也会为浮点数。
下面两组例子。
print(3 + 2) # 输出结果为5
print(3 + 2.0) # 输出结果为5.0
print("Roosters", 100 - 25 * 3 % 4) # 输出结果为'Roosters 97'
print("Roosters", 100.0 - 25 * 3 % 4) # 输出结果为'Roosters 97.0'
- 习题4:变量和命名
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car
average_passengers_per_car = passengers / cars_driven
print("There are", cars, "cars available.\n")
print("There are only", drivers, "divers available.\n")
print("There will be", cars_not_driven, "empty cars today.\n")
print("We can tansport", carpool_capacity, "people today.\n")
print("We have", passengers, "to carpool today.\n")
print("We need to put about", average_passengers_per_car, "in each car.\n")
加分习题
4.0 name 'car_pool_capacity' is not defined错误
“name 'car_pool_capacity' is not defined”是说变量car_pool_capacity未被定义。
比较前面被赋值的变量名称,可以看到car_pool_capacity在car和pool之间多了一个下划线
一般来说出现这种错误有两种情况:
①使用了未被定义/赋值的变量名;
②使用了与已定义/赋值的变量名称不一致的变量名。
此处是第二种情况。即14行调用的变量car_pool_capacity与第7行定义的变量名称carpool_capacity不一致。
'''
4.1 我在程序里用了 4.0 作为 space_in_a_car 的值,这样做有必要吗?如果只用 4 会有什么问题?
有必要。Python2中整数除以整数结果只保留整数位,去除了小数,如果想要结果准确,应该用浮点数4.0而不是整数4.
4.3 在每一个变量赋值的上一行加上一行注解。
# --coding:utf-8 --
# 给cars变量赋值100
cars = 100
# 给space_in_a_car变量赋值4.0
space_in_a_car = 4.0
# 给drivers变量赋值30
drivers = 30
# 给passengers变量赋值90
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
print("There are", cars, "cars available.\n")
print("There are only", drivers, "divers available.\n")
print("There will be", cars_not_driven, "empty cars today.\n")
print("We can tansport", carpool_capacity, "people today.\n")
print("We have", passengers, "to carpool today.\n")
print("We need to put about", average_passengers_per_car, "in each car.\n")
- 习题5:更多的变量和打印
# -- coding:utf-8 --
# 更多的变量和打印
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 %d 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))
加分习题
5.1 修改所有的变量名字,把它们前面的my_
去掉。确认将每一个地方的都改掉,不只是你使用=
赋值过的地方。
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 %s." % name)
print("He's %d inches tall." % height)
print("He's %d pounds heavy." % weight)
print("Actually that's not too heavy.")
print("He's got %s eyes and %s hair." % (eyes, hair))
print("His teeth are usually %s depending on the coffee." % teeth)
# this line is tricky, try to get it exactly right
print("If I add %d, %d, and %d I get %d." %
(age, height, weight, age + height + weight))
5.2 试着使用更多的格式化字符。例如 %r 就是是非常有用的一个,它的含义是“不管什么都打印出来”。
格式化字符串时,Python使用一个字符串作为模板。
模板中有格式符,这些格式符为真实值预留位置,并说明真实数值应该呈现的格式。
Python用一个tuple将多个值传递给模板,每个值对应一个格式符。
%s表示一个字符串。%d表示一个整数。%号代表了格式化操作.
http://www.cnblogs.com/vamei/archive/2013/03/12/2954938.htm
《笨办法学Python》里直接上手代码,新手不太容易理解,可以结合上面给的网址看一下补充理解。
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)
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 lfet side of..."
e = "a string with a right side."
print(w + e)
加分习题
6.2 找到所有的”字符串包含字符串”的位置
x = "There are %d types of people." % 10
y = "Those who know %s and those who %s." % (binary, do_not)
print("I said: %r." % x)
print("I also said: '%s'." % y)
joke_evaluation ="Isn't that joke so funny?! %r"
print(joke_evaluation % hilarious)
6.4 解释一下为什么 w 和 e 用 + 连起来就可以生成一个更长的字符串。
1.大前提是w和e是相同类型字符,w + e不会出现类型错误。
2.如果是数字+数字,那么输出结果为和值。
3.Python使用加号+来合并字符串,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 what happens
print(end1 + end2 + end3 + end4 + end5 + end6,)
print(end7 + end8 + end9 + end10 + end11 + end12)
8.习题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 thingg.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight.")
)
加分习题
8.2 注意最后一行程序中既有单引号又有双引号,你觉得它是如何工作的?
撇号位于两个双引号之间时,Python解释器能够正确的理解这个字符串。
如果在用单引号括起的字符串中,包含撇号,Python会将第一个单引号和撇号之间的内容视为一个字符串,将剩下的文本视为Python代码。
9.打印,打印,打印
# -- coding:utf-8 --
# Here's sonme 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.那是什么?
# -- coding:utf-8 --
# 那是什么?
print("I am 6'2\" tall.") # 使用\将字符串中的双引号转义
print('I am 6\'2" tall.') # 使用\将字符串中的单引号转义
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)
加分习题
10.1 上网搜索一下还有哪些可用的转义字符。
http://blog.chinaunix.net/uid-20794157-id-3038417.html
收到几个网址,但是怎么用还不是很熟悉。
10.2 使用 ''' (三个单引号)取代三个双引号,看看效果是不是一样的?
使用 ''' (三个单引号)取代三个双引号,效果是一样的。这是在不引起Python正确理解的前提下。
170711 《笨办法学Python》0~10习题练习总结
1.新学到的知识点
①格式化字符串
②转义与转义符号
2.尚未掌握的知识点
①格式化字符串
②转义与转义符号
这两个知识点是我学Python以来新出现的知识点,在《Python编程:从入门到实践》中没有涉及到。今天的练习就熟悉了一下代码,大致了解了是什么一个用法、用途,能看得懂习题代码,却还不能够延伸。比如习题5的加分练习里面,有一个用格式化字符串“将英寸和磅转换成厘米和千克”的练习,以及“将转义序列和格式化字符串放到一起,创建一种更复杂的格式。”的练习,我就有点一筹莫展了。
3.学习心得
与我之前学的《Python编程:从入门到实践》循序渐进的教学路数不同,《笨办法学Python》以练代学,在遇到问题时通过自己发现问题、自己查资料、再解决问题。这样的套路对于有一定基础的同学很有帮助,既能够熟悉代码巩固已学知识,又能够学到知识。但《笨办法学Python》对具体的语法没有讲解。我觉得如果有一些Python基础语法基础的同学去学会比较轻松一点。