【Python爬虫作业】笨办法学python 习题1-10

习题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.')

应该看到的结果:

Hello World!

Hello again.

I like typing this.

This is fun.

Yay!Printing.

I'd much rather you not.

I "said" do not touch this.

Process finished with exit code 0

附加练习

# (octothorpe)用来表示注释,在代码前加上#,此行代码不运行.

-----------------------------------------------------------------------------------------------------------------------------------

习题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.')

应该看到的结果

I could have code like this.

This will run.

Process finished with exit code 0

附加练习

#字符的英文为octothorpe或者pound character,用来作为代码的注释用,也可以禁止这行代码的运行。

-----------------------------------------------------------------------------------------------------------------------------------

习题3

print('I will now count my chickens:')#打印字符串print('Hens',25+30/6)#打印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 true that 3 + 2 < 5 - 7?')#打印这句话,“3+2小于5-7print(3+2<5-7)#打印数学运算结果,结果是false

print('What is 3 + 2?',3+2)#打印字符串及数学运算结果,结果是What is 3 + 2? 5print('What is 5 - 7?',5-7)#打印字符串及数学运算结果,结果是What is 5-7? -2

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)#打印字符串并判断真假

应该看到的结果

I will now count my chickens:

Hens 30.0

Roosters 97

Now I will count the eggs:

6.75

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

False

What is 3 + 2? 5

What is 5 - 7? -2

Oh, that's why it's False.

How about some more.

Is it greater? True

Is it greater or equal? True

Is it less or equal? False

Process finished with exit code 0

附加练习

ex3-1.py

print (2 + 5 / 5 -1)

结果是2.0,结果保留至小数点后一位。

浮点数是表示数字的标准,整数也可以用浮点数的格式来存储。

运算优先级分别是括号,指数,乘,除,加,减。

-----------------------------------------------------------------------------------------------------------------------------------

习题4

cars=100#100carsspace_in_a_car=4.0#4个车位drivers=30#30driverpassengers=90#90passengerscars_not_driven=cars-drivers#没有开的车辆数=cars- driverscars_driven=drivers#开的车量数=司机数carpool_capacity=cars_driven*space_in_a_car#车容量=开的车量数×一辆车的车位数average_passengers_per_car=passengers/cars_driven#每辆车的平均乘客数=所有乘客数/开的车辆数

应该看到的结果print('There are',cars,'cars

available.')

print('There are only', drivers,'drivers available.')

print('There will be',

cars_not_driven,'empty cars today.')

print('We can transport',carpool_capacity,'people today')

print('We have',passengers,'to carpool today.')

print('We need to put about',average_passengers_per_car,'in each car.')

-----------------------------------------------------------------------------------------------------------------------------------

习题5

my_name='Zed A. Shaw'my_age=35

my_height=74

my_weight=180

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 exactlyrightprint('If I add %d, %d, and %d I get %d.'%(my_age, my_height,my_weight, my_age+my_height+my_weight))

应该看到的结果

Let's talk about Zed A. Shaw.

He's 74 inches tall.

He's 180 pounds heavy.

Actually that's not too heavy.

He's got Blue eyes and Brown hair.

His teeth are usually White depending on the coffee.

If I add 35, 74, and 180 I get 289.

附加练习

name='Zed A. Shaw'age=35

height=74

weight=180

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 exactlyrightprint('If I add %d, %d, and %d I get %d.'%(age, height, weight,age+height+weight))

Let's talk about Zed A. Shaw.

He's 74 inches tall.

He's 180 pounds heavy.

Actually that's not too heavy.

He's got Blue eyes and Brown hair.

His teeth are usually White depending on the coffee.

If I add 35, 74, and 180 I get 289.

心得体会:改变变量名字,输出的结果是一样的。s%表示把变量带到字符串中,d%表示把变量带到数值中,r%意思为不管什么都打印出来。Python格式化字符包括:%s,%r,%c,%b,%d,%i,%o,%x,%e,%E,%f,%F,%g,%G.

英寸转化成厘米:my_height =74*2.54

磅转化成千克:my_weight =180*0.45

-----------------------------------------------------------------------------------------------------------------------------------

习题6

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'tthat joke so funny?! %r"

print(joke_evaluation%hilarious)

w="This is the leftside of ..."e="a string with a right side."

print(w+e)

应该看到的结果

There are 10 types of people.

Those who know binary and those who don't.

I said: 'There are 10 types of people.'.

I also said: Those who know binary and those who don't.

Isn't that joke so funny?! False

This is the left side of ...a string with a right side.

附加练习

红色代码是把一个字符串放进另一个字符串,总共4处。print(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'dthat 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 atthe end. try removing it to see what happensprint(end1+end2+end3+end4+end5+end6, end7+end8+end9+end10+end11+end12)

应该看到的结果

Mary had a little lamb.

Its fleece was white as snow.

And everywhere that Mary went.

..........

Cheese Burger

心得体会:

print(end1+end2+end3+end4+end5+end6)

print(end7+end8+end9+end10+end11+end12)

输出

Cheese

Burger

print(end1+end2+end3+end4+end5+end6, end7+end8+end9+end10+end11+end12)

输出Cheese Burger

print(end1+end2+end3+end4+end5+end6+end7+end8+end9+end10+end11+end12)

输出输出CheeseBurger


-----------------------------------------------------------------------------------------------------------------------------------

习题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)%("I had this thing.","That you could type up right.","But it didn't sing.","So I said goodnight.")

看到的结果

%r %r %r %r

Traceback (most recent call last):

File "C:/Users/ex8.py",line 3, in

print(formatter) %(1, 2, 3, 4)

TypeError: unsupported operand type(s) for %: 'NoneType' and'tuple'

错误分析:括号要把赋予的变量都括起来,代码改为

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%("I had this thing.","That you could type up right.","But it didn't sing.","So I said goodnight."))

应该看到的结果

1 2 3 4

'one' 'two' 'three' 'four'

True False False True

'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.\nWith the three double - quotes.\nWe'll be able to type as much as we like.\nEven 4 lines if we want, or 5, or 6.")

应该看到的结果

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.

-----------------------------------------------------------------------------------------------------------------------------------

习题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:\n\t* Cat food\n\t* Fishies\n\t* Catnip\n\t* Grass"

print(tabby_cat)

print(persian_cat)

print(backslash_cat)

print(fat_cat)

应该看到的结果

I'm tabbed in.

I'm split

on a line.

I'm \a \ cat.

I'll do a list:

*Catfood

*Fishies

*Catnip

* Grass

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念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

推荐阅读更多精彩内容