【Python爬虫】-【第一周】01-作业

  1. 习题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编译器将忽略这段注释,从下一个不以 '#'开头的代码段开始或继续运行。
  1. 习题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.")
  1. 习题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'
  1. 习题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") 
  1. 习题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基础语法基础的同学去学会比较轻松一点。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容