【Python爬蟲作業(yè)】笨辦法學(xué)python 習(xí)題1-10

習(xí)題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.')

應(yīng)該看到的結(jié)果:

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

附加練習(xí)

# (octothorpe)用來表示注釋,在代碼前加上#撵幽,此行代碼不運行.

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

習(xí)題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.')

應(yīng)該看到的結(jié)果

I could have code like this.

This will run.

Process finished with exit code 0

附加練習(xí)

#字符的英文為octothorpe或者pound character身害,用來作為代碼的注釋用镀赌,也可以禁止這行代碼的運行碗硬。

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

習(xí)題3

print('I will now count my chickens:')#打印字符串print('Hens',25+30/6)#打印Hens, 25+ 30/6, /表示除以print('Roosters',100-25*3%4)表示乘以瘫析,%表示求余數(shù)print('Now I will count the eggs:')#打印字符串print(3+2+1-5+4%2-1/4+6)#打印數(shù)學(xué)運算結(jié)果, %表示求余數(shù), /表示除以print('Is it true that 3 + 2 < 5 - 7?')#打印這句話,“3+2小于5-7江醇?print(3+2<5-7)#打印數(shù)學(xué)運算結(jié)果佛寿,結(jié)果是false

print('What is 3 + 2?',3+2)#打印字符串及數(shù)學(xué)運算結(jié)果,結(jié)果是What is 3 + 2? 5print('What is 5 - 7?',5-7)#打印字符串及數(shù)學(xué)運算結(jié)果,結(jié)果是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)#打印字符串并判斷真假

應(yīng)該看到的結(jié)果

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

附加練習(xí)

ex3-1.py

print (2 + 5 / 5 -1)

結(jié)果是2.0幌墓,結(jié)果保留至小數(shù)點后一位。

浮點數(shù)是表示數(shù)字的標(biāo)準(zhǔn)冀泻,整數(shù)也可以用浮點數(shù)的格式來存儲常侣。

運算優(yōu)先級分別是括號,指數(shù)弹渔,乘胳施,除,加肢专,減舞肆。

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

習(xí)題4

cars=100#100carsspace_in_a_car=4.0#4個車位drivers=30#30driverpassengers=90#90passengerscars_not_driven=cars-drivers#沒有開的車輛數(shù)=cars- driverscars_driven=drivers#開的車量數(shù)=司機(jī)數(shù)carpool_capacity=cars_driven*space_in_a_car#車容量=開的車量數(shù)×一輛車的車位數(shù)average_passengers_per_car=passengers/cars_driven#每輛車的平均乘客數(shù)=所有乘客數(shù)/開的車輛數(shù)

應(yīng)該看到的結(jié)果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.')

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

習(xí)題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))

應(yīng)該看到的結(jié)果

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.

附加練習(xí)

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.

心得體會:改變變量名字,輸出的結(jié)果是一樣的博杖。s%表示把變量帶到字符串中椿胯,d%表示把變量帶到數(shù)值中,r%意思為不管什么都打印出來欧募。Python格式化字符包括:%s,%r,%c,%b,%d,%i,%o,%x,%e,%E,%f,%F,%g,%G.

英寸轉(zhuǎn)化成厘米:my_height =74*2.54

磅轉(zhuǎn)化成千克:my_weight =180*0.45

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

習(xí)題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)

應(yīng)該看到的結(jié)果

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.

附加練習(xí)

紅色代碼是把一個字符串放進(jìn)另一個字符串压状,總共4處。print(w

+ e)和print(w, e)輸出的字符串是一樣的,為什么种冬?

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

習(xí)題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)

應(yīng)該看到的結(jié)果

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


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

習(xí)題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.")

看到的結(jié)果

%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."))

應(yīng)該看到的結(jié)果

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.'

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

習(xí)題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.")

應(yīng)該看到的結(jié)果

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.

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

習(xí)題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)

應(yīng)該看到的結(jié)果

I'm tabbed in.

I'm split

on a line.

I'm \a \ cat.

I'll do a list:

*Catfood

*Fishies

*Catnip

* Grass

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市娱两,隨后出現(xiàn)的幾起案子莺匠,更是在濱河造成了極大的恐慌,老刑警劉巖十兢,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件趣竣,死亡現(xiàn)場離奇詭異,居然都是意外死亡旱物,警方通過查閱死者的電腦和手機(jī)遥缕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宵呛,“玉大人单匣,你說我怎么就攤上這事”λ耄” “怎么了户秤?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵,是天一觀的道長逮矛。 經(jīng)常有香客問我鸡号,道長,這世上最難降的妖魔是什么须鼎? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任鲸伴,我火速辦了婚禮,結(jié)果婚禮上莉兰,老公的妹妹穿的比我還像新娘挑围。我一直安慰自己,他們只是感情好糖荒,可當(dāng)我...
    茶點故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著模捂,像睡著了一般捶朵。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上狂男,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天综看,我揣著相機(jī)與錄音,去河邊找鬼岖食。 笑死红碑,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播析珊,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼羡鸥,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了忠寻?” 一聲冷哼從身側(cè)響起惧浴,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎奕剃,沒想到半個月后衷旅,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡纵朋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年柿顶,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片操软。...
    茶點故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡九串,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出寺鸥,到底是詐尸還是另有隱情猪钮,我是刑警寧澤,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布胆建,位于F島的核電站烤低,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏笆载。R本人自食惡果不足惜扑馁,卻給世界環(huán)境...
    茶點故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望凉驻。 院中可真熱鬧腻要,春花似錦、人聲如沸涝登。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽胀滚。三九已至趟济,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間咽笼,已是汗流浹背顷编。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留剑刑,地道東北人媳纬。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親钮惠。 傳聞我的和親對象是個殘疾皇子茅糜,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內(nèi)容