【Python爬蟲作業(yè)】- 第一周01 笨方法0-10加分題

1粱年、

<pre>
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.
hello world
hello world!hello again
hello world! hello again
</pre>
<pre>
思考題

  1. 讓你的腳本再多打印一行夭谤。
    print("hello world"+'\n')
  2. 讓你的腳本只打印一行冲九。
    print("hello world!",end="")
    print("hello again")
    print("hello world!","hello again")
  3. 在一行的起始位置放一個 ‘#’ (octothorpe) 符號。它的作用是什么辫红?自己研究一下摊灭。
    答:注釋作用断傲。
    </pre>

2、

<pre>

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.
</pre>
<pre>
思考題:
沒有找到錯誤谴古。质涛。
</pre>

3稠歉、

<pre>

打印文字i will now count my chickens:

print("i will now count my chickens:")

輸出hens 后面附上25+(30/6)的結果

print("hens",25+30//6)

輸出roosters 后面附上100減去(25*3)除以4的余數(shù)

print("roosters",100-25*3%4)

打印文字now i will count the eggs:

print("now i will count the eggs:")

打印計算結果(3+2+1-5)+(4%2)-(1//4)+6

print(3+2+1-5+4%2-1//4+6)

打印文字is it true that 3+2<5-7?

print("is it true that 3+2<5-7?")

計算出(3+2)小于(5-7)的布爾值并打印出來

print(3+2<5-7)

打印文字what is 3+2? 后面附上3+2的結果

print("what is 3+2?",3+2)

打印文字what is 5-7?后面附上5-7的結果

print("what is 5-7?",5-7)

打印文字oh,that's why it's false.

print("oh,that's why it's false.")

打印文字how about some more.

print("how about some more.")

打印文字is it greater? 后面附上5大于-2的布爾值結果

print("is it greater?",5>-2)

打印文字is it greater or equal?后面附上5>=-2的布爾值

print("is it greater or equal?",5>=-2)

打印文字is it less or equal?后面附上5<=-2的布爾值

print("is it less or equal?",5<=-2)
輸出:
i will now count my chickens:
hens 30
roosters 97
now i will count the eggs:
7
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
</pre>
<pre>
思考題:

自己找個想要計算的東西,寫一個 .py 文件把它計算出來汇陆。計算10的階乘怒炸。

a = 10
b = 1
while a>1:
b = b*a
a = a-1
else:
print(b)
輸出:3628800
print("i will now count my chickens:")
print("hens",25.0+30.0//6.0)
print("roosters",100.0-25.0*3.0%4.0)
print("now i will count the eggs:")
print(3.0+2.0+1.0-5.0+4.0%2.0-1.0//4.0+6.0)
print("is it true that 3.0+2.0<5.0-7.0?")
print(3.0+2.0<5.0-7.0)
print("what is 3+2?",3.0+2.0)
print("what is 5-7?",5.0-7.0)
print("oh,that's why it's false.")
print("how about some more.")
print("is it greater?",5.0>-2.0)
print("is it greater or equal?",5.0>=-2.0)
print("is it less or equal?",5.0<=-2.0)
輸出:
i will now count my chickens:
hens 30.0
roosters 97.0
now i will count the eggs:
7.0
is it true that 3.0+2.0<5.0-7.0?
False
what is 3+2? 5.0
what is 5-7? -2.0
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
</pre>

4、

<pre>
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90

100-30

cars_not_driven = cars-drivers

30

cars_driven = drivers

30*4.0

carpool_capacity = cars_driven*space_in_a_car

90/30

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 car pool today.")
print("we need to put about",average_passengers_per_car,"in each car")
輸出:
there are 100 cars available
there are only 30 drivers available.
there will be 70 empty cars today.
we can transport 120.0 people today.
we have 90 to car pool today.
we need to put about 3 in each car
</pre>
<pre>
思考題:

作者打錯了變量瞬测,多打了一個

不會有問題横媚,因為人數(shù)肯定是整數(shù)。

a = 10
b = 11
c = a+b
print(c)

</pre>

5月趟、

<pre>
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))

輸出:
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.
</pre>
<pre>
思考題:
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))
輸出:
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.
英鎊和英寸轉換
</pre>

6灯蝴、

<pre>

06

x = "there are %d types of people." % 10

10賦值給了%d的位置。

binary = "binary"
do_not = "don't"
y = "those who know %s and those who %s" %(binary,do_not)

多個參數(shù)賦值需要在括號里孝宗,第一個字符串包含字符串

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)

竟然還可以在變量里放入參數(shù)穷躁,然后賦值的,第四個字符串包含字符串

w = "this is the left side of..."
e = "a string with a right side"
print(w+e)

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
</pre>
<pre>
思考題:

為什么w+e 可以生成一個更長的字符串问潭、

在原來的后面加上代碼

a =w+e
print(a)
輸出:
this is the left side of...a string with a right side
</pre>

7、

<pre>
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 waht happens 沒有任何反應狡忙。。

print(end1+end2+end3+end4+end5+end6),
print(end7+end8+end9+end10+end11+end12)
結果:
mary had a little lamb
its fleece was white as snow.
and everywhere that mary went.
..........
cheese
burger
</pre>
<pre>
思考題:
無任何錯誤
</pre>

8址芯、

<pre>
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 thing.",
"that you could type up right.",
"but it didn't sing.",
"so i said goodnight."
))

最后一行因為其中有段字符串里包含了單引號灾茁,所以輸出的時候括起來要使用雙引號

</pre>
<pre>
思考題:
見上面。
</pre>

9谷炸、

<pre>
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.
""")
輸出:
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.
</pre>
<pre>
思考題:
木有
</pre>

10北专、

<pre>
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)
輸出:
I'm tabbed in.
I'm split
on a line.
I'm \ a \ cat.

I'll do a list:
* Cat food
* Fishies
* Catnip
* Grass
</pre>
<pre>
思考題:

python轉義字符串

改成單引號后沒有區(qū)別。

Q浮M赝恰!將轉義序列和格式化字符串放到一起描孟,創(chuàng)建一種更復雜的格式驶睦。這句話沒有明白。答疑時要提出D湫选3『健!

a = "\t* Catnip\n\t* Grass"
print("ceshi:%r,%s" %(a,a))
輸出:
ceshi:'\t* Catnip\n\t* Grass', * Catnip
* Grass

可以看出%s自動把轉義的省略了青抛。
</pre>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末旗闽,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌适室,老刑警劉巖嫡意,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異捣辆,居然都是意外死亡蔬螟,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門汽畴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來旧巾,“玉大人,你說我怎么就攤上這事忍些÷承桑” “怎么了?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵罢坝,是天一觀的道長廓握。 經常有香客問我,道長嘁酿,這世上最難降的妖魔是什么隙券? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮闹司,結果婚禮上娱仔,老公的妹妹穿的比我還像新娘。我一直安慰自己游桩,他們只是感情好牲迫,可當我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著众弓,像睡著了一般恩溅。 火紅的嫁衣襯著肌膚如雪隔箍。 梳的紋絲不亂的頭發(fā)上谓娃,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天,我揣著相機與錄音蜒滩,去河邊找鬼滨达。 笑死,一個胖子當著我的面吹牛俯艰,可吹牛的內容都是我干的捡遍。 我是一名探鬼主播,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼竹握,長吁一口氣:“原來是場噩夢啊……” “哼画株!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤谓传,失蹤者是張志新(化名)和其女友劉穎蜈项,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體续挟,經...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡紧卒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了诗祸。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片跑芳。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖直颅,靈堂內的尸體忽然破棺而出博个,到底是詐尸還是另有隱情,我是刑警寧澤功偿,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布坡倔,位于F島的核電站,受9級特大地震影響脖含,放射性物質發(fā)生泄漏罪塔。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一养葵、第九天 我趴在偏房一處隱蔽的房頂上張望征堪。 院中可真熱鬧,春花似錦关拒、人聲如沸佃蚜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽谐算。三九已至,卻和暖如春归露,著一層夾襖步出監(jiān)牢的瞬間洲脂,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工剧包, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留恐锦,地道東北人。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓疆液,卻偏偏與公主長得像一铅,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子堕油,可洞房花燭夜當晚...
    茶點故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內容