【Python爬虫】-笨办法学 Python 习题01-10

一、作业内容:

01、将下面的内容写到一个文件中,取名为ex1.py。这个命名方式很重要,Python文件最好以.py结尾。

1 print "Hello World!"

2 print”Hello Again"

3 print"I like typing this."

4 print"This is fun."

5 print"Yay! Printing."

6 print"I'd much rather you 'not'."

7 print"I "said" do not touch this."
Paste_Image.png

习题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."

习题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

习题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."

习题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

习题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
Paste_Image.png

习题 7: 更多打印
现在我们将做一批练习,在练习的过程中你需要键入代码,并且让它们运行起来。我不会解释太多,因
为这节的内容都是以前熟悉过的。这节练习的目的是巩固你学到的东西。我们几个练习后再见。不要跳
过这些习题。不要复制粘贴!

你应该看到的结果

$ python ex7.py
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
..........
Cheese Burger
$
Paste_Image.png

习题 8: 打印,打印
你应该看到的结果

$ python ex8.py
1 234
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'
$
Paste_Image.png

习题 9: 打印,打印,打印
你应该看到的结果

$ python ex9.py
Here are the days:  Mon Tue Wed Thu Fri Sat Sun
Here are the months:  Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
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.
$
Paste_Image.png

习题 10: 那是什么?

 I'm tabbed in.
I'm split
on a line.
I'm \ a \ cat.
I'll do a list:
       * Cat food
       * Fishies
       * Catnip
* Grass
Paste_Image.png

习题 11: 提问

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

推荐阅读更多精彩内容