【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)用來表示注釋,在代碼前加上#,此行代碼不運(yùn)行.

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

習(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,用來作為代碼的注釋用,也可以禁止這行代碼的運(yùn)行。

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

習(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é)運(yùn)算結(jié)果, %表示求余數(shù), /表示除以print('Is it true that 3 + 2 < 5 - 7?')#打印這句話,“3+2小于5-7print(3+2<5-7)#打印數(shù)學(xué)運(yùn)算結(jié)果,結(jié)果是false

print('What is 3 + 2?',3+2)#打印字符串及數(shù)學(xué)運(yùn)算結(jié)果,結(jié)果是What is 3 + 2? 5print('What is 5 - 7?',5-7)#打印字符串及數(shù)學(xué)運(yùn)算結(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ù)點(diǎn)后一位。

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

運(yùn)算優(yōu)先級分別是括號,指數(shù),乘,除,加,減。

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

習(xí)題4

cars=100#100carsspace_in_a_car=4.0#4個(gè)車位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.

心得體會(huì):改變變量名字,輸出的結(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í)

紅色代碼是把一個(gè)字符串放進(jìn)另一個(gè)字符串,總共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

心得體會(huì):

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'

錯(cuò)誤分析:括號要把賦予的變量都括起來,代碼改為

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)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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