【Python爬蟲】-笨辦法學 Python 習題27-34

一柳譬、作業(yè)內(nèi)容

笨辦法學 Python 習題27-34以及加分題棒厘。

二蕊蝗、作業(yè)代碼:

# 習題 27: 記住邏輯關(guān)系

# 一盯捌、記住邏輯術(shù)語
and # 與
or # 或
not # 非
!= # (not equal) 不等于
== # (equal) 等于
>= # (greater-than-equal) 大于等于
<= # (less-than-equal) 小于等于
True # 真
False # 假

# 二烁巫、真值表
# 書中的方法是通過索引卡片記憶和默寫表達式署隘,來判斷`True` or `False`。

死記硬背很麻煩啊……然后我去找其他的教程亚隙。
廖雪峰的 Python3 教程中:

布爾值可以用and磁餐、ornot運算。
and運算是與運算阿弃,只有所有都為True诊霹,and運算結(jié)果才是True

>>> True and True
True
>>> True and False
False
>>> False and False
False
>>> 5 > 3 and 3 > 1
True

or運算是或運算,只要其中有一個為True渣淳,or運算結(jié)果就是True

>>> True or True
True
>>> True or False
True
>>> False or False
False
>>> 5 > 3 or 1 > 3
True

這樣很容易就可以記住了脾还。

# 習題 28: 布爾表達式練習

>>> True and True
True
>>> False and True
False
>>> 1 == 1 and 2 == 1
False
>>> "test" == "test"
True
>>> 1 == 1 or 2 != 1
True
>>> True and 1 == 1
True
>>> False and 0 != 0
False
>>> True or 1 == 1
True
>>> "test" == "testing"
False
>>> 1 != 0 and 2 == 1
False
>>> "test" != "testing"
True
>>> "test" == 1
False
>>> not (True and False)
True
>>> not (1 == 1 and 0 != 1)
False
>>> not (10 == 1 or 1000 == 1000)
False
>>> not (1 != 10 or 3 == 4)
False
>>> not ("testing" == "testing" and "Zed" == "Cool Guy")
True
>>> 1 == 1 and not ("testing" == 1 or 1 == 0)
True
>>> "chunky" == "bacon" and not (3 == 4 or 3 == 3)
False
>>> 3 == 3 and not ("testing" == "testing" or "Python" == "Fun")
False
# 加分習題

# 一、Python 里還有很多和 != 入愧、 == 類似的操作符. 試著盡可能多地列出 Python 中的等價運算符鄙漏。例如 < 或者 <= 就是。
# 二棺蛛、寫出每一個等價運算符的名稱怔蚌。例如 != 叫 “not equal(不等于)”。

# != 叫 not equal(不等于)
# == 叫 equal(等于)
# < 叫 less than(小于)
# <= 叫 less than or equal(小于或等于)



# 三鞠值、在 python 中測試新的布爾操作媚创。在敲回車前你需要喊出它的結(jié)果渗钉。不要思考彤恶,憑自己的第一感就可以了钞钙。把表達式和結(jié)果用筆寫下來再敲回車,最后看自己做對多少声离,做錯多少芒炼。


# 四、把習題 3 那張紙丟掉术徊,以后你不再需要查詢它了本刽。
# 習題 29: 如果(if)



people = 20
cats = 30
dogs = 15

if people < cats:
    print("Too many cats! The world is doomed!")
if people > cats:
    print("Not many cats! The world is saved!")
if people < dogs:
    print("The world is drooled on!")
if people > dogs:
    print("The world is dry!")

dogs += 5

if people >= dogs:
    print("People are greater than or equal to dogs.")
if people <= dogs:
    print("People are less than or equal to dogs")
if people == dogs:
    print("People are dogs.")

運算結(jié)果如下:

Too many cats! The world is doomed!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs
People are dogs.

Process finished with exit code 0
# 加分習題

# 猜猜“if語句”是什么,它有什么用處赠涮。
# 在做下一道習題前子寓,試著用自己的話回答下面的問題:
# 一、你認為 if 對于它下一行的代碼做了什么笋除?

# 答:if根據(jù)條件判斷要不要執(zhí)行下一行的代碼斜友。


# 二、為什么 if 語句的下一行需要 4 個空格的縮進垃它?
# 答:規(guī)則鲜屏。便于查看?


# 三国拇、如果不縮進洛史,會發(fā)生什么事情?
# 答:會報錯酱吝。
IndentationError: expected an indented block


# 四也殖、把習題 27 中的其它布爾表達式放到``if語句``中會不會也可以運行呢?試一下。
people = 20
cats = 30
dogs = 15

if people != cats:
    print("Too many cats! The world is doomed!")
if people == cats:
    print("Not many cats! The world is saved!")
if people >= dogs:
    print("The world is drooled on!")
if people > dogs:
    print("The world is dry!")

# 答:可以運行径缅。

五醇疼、如果把變量 people, cats, 和 dogs 的初始值改掉,會發(fā)生什么事情霎褐?

執(zhí)行的結(jié)果會發(fā)生變化。
# 習題 30: Else 和 If

people = 40
cars = 39
buses = 15


if cars > people:
    print("We should take the cars.")
else:
    print("We can''t decide.")

if buses > cars:
    print("That's too many buses.")
elif buses < cars:
    print("Maybe we could take the buses.")
else:
    print("We still can't decide.")

if people > buses:
    print("Alright, let's juse take the buses.")
else:
    print("Fine, let's stay home then.")
# 加分習題
# 一该镣、猜想一下 elif 和 else 的功能冻璃。
# elif
# 二、將 cars, people, 和 buses 的數(shù)量改掉损合,然后追溯每一個 if 語句省艳。看看最后會打印出什么來嫁审。
# 三跋炕、試著寫一些復(fù)雜的布爾表達式,例如 cars > people and buses < cars律适。
# 四辐烂、在每一行的上面寫注解遏插,說明這一行的功用。
# 習題 31: 作出決定

print("You enter a dark room with two doors.  Do you go through door #1 or door #2?")

door = input('> ')

if door == "1":
    print("There's a giant bear here eating a cheese cake.  What do you do?")
    print("1. Take the cake.")
    print("2. Scream at the bear.")

    bear = input('> ')

    if bear == "1":
        print("The bear eats your face off.  Good job!")
    elif bear == "2":
        print("The bear eats your legs off.  Good job!")
    else:
        print("Well, doing %s is probably better.  Bear runs away.") % bear

elif door == "2":
    print("You stare into the endless abyss at Cthulhu's retina.")
    print("1. Blueberries.")
    print("2. Yellow jacket clothespins.")
    print("3. Understanding revolvers yelling melodies.")

    insanity = input('> ')
    if insanity == "1" or insanity == "2":
        print("Your body survives powered by a mind of jello.  Good job!")
    else:
        print("The insanity rots your eyes into a pool of muck.  Good job!")

else:
    print("You stumble around and fall on a knife and die.  Good job!")
# 加分習題

為游戲添加新的部分纠修,改變玩家做決定的位置胳嘲。盡自己的能力擴展這個游戲,不過別把游戲弄得太怪異了扣草。
# 習題 32: 循環(huán)和列表

the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']

# this first kind of for-loop goes through a list.
for number in the_count:
    print("This is count %d" % number)

# same as above
for fruit in fruits:
    print("A fruit of type: %s" % fruit)

# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
    print("I got %r" % i)

# we can also build lists, first start with an empty one
elements = []

# then use the range function to do 0 to 5 counts
for i in range(0, 6):
    print("Adding %d to the list." %i)
    # append is a function that  lists understand
    elements.append(i)

#now we can print them out too
for i in elements:
    print("Element was: %d" % i)

運行結(jié)果如下:

This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got 'pennies'
I got 2
I got 'dimes'
I got 3
I got 'quarters'
Adding 0 to the list.
Adding 1 to the list.
Adding 2 to the list.
Adding 3 to the list.
Adding 4 to the list.
Adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5

Process finished with exit code 0

# 加分習題
# 一了牛、注意一下 range 的用法。查一下 range 函數(shù)并理解它辰妙。

# 打印由函數(shù)range(5)生成從0開始小于5的整數(shù)
print(list(range(5)))
# 打印整數(shù)1到整數(shù)100的序列鹰祸。
print(list(range(101)))


sum = 0
for x in range(101):
    sum = sum + x
print(sum)

運行結(jié)果如下:

[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
5050

Process finished with exit code 0


# 二、在第 22 行密浑,你可不可以直接將 elements 賦值為 range(0,6)福荸,而無需使用 for 循環(huán)?

# 可以
the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']

# this first kind of for-loop goes through a list.
for number in the_count:
    print("This is count %d" % number)

# same as above
for fruit in fruits:
    print("A fruit of type: %s" % fruit)

# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
    print("I got %r" % i)

# we can also build lists, first start with an empty one
elements = []


elements = range(0, 6)
print(elements)
#now we can print them out too
for i in elements:
    print("Element was: %d" % i)

運行結(jié)果如下:

This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got 'pennies'
I got 2
I got 'dimes'
I got 3
I got 'quarters'
range(0, 6)
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5

Process finished with exit code 0

# 三肴掷、在 Python 文檔中找到關(guān)于列表的內(nèi)容敬锐,仔細閱讀一下,除了 append 以外列表還支持哪些操作呆瞻?
squares = [1, 2, 3, 4, 5]
print(squares)

# 列表可以被索引
print(squares[0])
print(squares[1])
print(squares[-1])

#列表可以被切片
print(squares[1:])
print(squares[-3:])
print(squares[:])

#列表支持連接這樣的操作
print(squares + [7, 8, 9])

#列表是可變的台夺,它允許修改元素
print(squares)
squares[3] = 1217
print(squares)

#用pop(i)方法,刪除list指定位置的元素痴脾,i是索引位置
#若想刪除list末尾的元素颤介,用pop()即可
print(squares)
squares.pop(3)
print(squares)


運行結(jié)果如下:

[1, 2, 3, 4, 5]
1
2
5
[2, 3, 4, 5]
[3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 7, 8, 9]
[1, 2, 3, 4, 5]
[1, 2, 3, 1217, 5]
[1, 2, 3, 1217, 5]
[1, 2, 3, 5]

Process finished with exit code 0

# 習題 33: While 循環(huán)

i = 0
numbers = []

while i < 6:
    print("At the top i is %d" % i)
    numbers.append(i)

    i = i + 1
    print("Numbers now: ", numbers)
    print("At the bottom i is %d" % i)

print("The numbers: ")

for num in numbers:
    print(num)

運行結(jié)果如下:

At the top i is 0
Numbers now:  [0]
At the bottom i is 1
At the top i is 1
Numbers now:  [0, 1]
At the bottom i is 2
At the top i is 2
Numbers now:  [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers now:  [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now:  [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now:  [0, 1, 2, 3, 4, 5]
At the bottom i is 6
The numbers: 
0
1
2
3
4
5

Process finished with exit code 0

# 加分習題
# 一、將這個 while 循環(huán)改成一個函數(shù)赞赖,將測試條件(i < 6)中的 6 換成一個變量滚朵。
# 二、使用這個函數(shù)重寫你的腳本前域,并用不同的數(shù)字進行測試辕近。
# 三、為函數(shù)添加另外一個參數(shù)匿垄,這個參數(shù)用來定義第 8 行的加值 + 1 移宅,這樣你就可以讓它任意加值了。
# 四椿疗、再使用該函數(shù)重寫一遍這個腳本漏峰。看看效果如何届榄。
# 五浅乔、接下來使用 for-loop 和 range 把這個腳本再寫一遍。你還需要中間的加值操作嗎铝条?如果你不去掉它靖苇,會有什么樣的結(jié)果席噩?
# 習題 34: 訪問列表的元素
animals = ['bear', 'python', 'peacock', 'kangaroo', 'whale', 'platypus']

print("The 1st animal is at 0 and is a", animals[0], ".")
print("The animal at 0 is the 1st animal and  is a", animals[0], ".")
print("The 2nd animal is at 1 and is a", animals[1], ".")
print("The animal at 1 is the 2nd animal and is a", animals[1], ".")

print("The 3rd animal is at 2 and is a", animals[2], ".")
print("The animal at 2 is the 3rd animal and  is a", animals[2], ".")

print("The 4th animal is at 3 and is a", animals[3], ".")
print("The animal at 3 is the 4th animal and is a", animals[3], ".")

print("The 5th animal is at 4 and is a", animals[4], ".")
print("The animal at 4 is the 5th animal and is a", animals[4], ".")

print("The 6th animal is at 5 and is a", animals[5], ".")
print("The animal at 5 is the 6th animal and is a", animals[5], ".")


運行結(jié)果如下:

The 1st animal is at 0 and is a bear .
The animal at 0 is the 1st animal and  is a bear .
The 2nd animal is at 1 and is a python .
The animal at 1 is the 2nd animal and is a python .
The 3rd animal is at 2 and is a peacock .
The animal at 2 is the 3rd animal and  is a peacock .
The 4th animal is at 3 and is a kangaroo .
The animal at 3 is the 4th animal and is a kangaroo .
The 5th animal is at 4 and is a whale .
The animal at 4 is the 5th animal and is a whale .
The 6th animal is at 5 and is a platypus .
The animal at 5 is the 6th animal and is a platypus .

Process finished with exit code 0

# 加分習題
# 一、上網(wǎng)搜索一下關(guān)于序數(shù)(ordinal number)和基數(shù)(cardinal number)的知識并閱讀一下顾复。
# 二、以你對于這些不同的數(shù)字類型的了解鲁捏,解釋一下為什么 “January 1, 2010” 里是 2010 而不是 2009芯砸?(提示:你不能隨機挑選年份。)
# 三给梅、再寫一些列表假丧,用一樣的方式作出索引,確認自己可以在兩種數(shù)字之間互相翻譯动羽。
# 使用 python 檢查自己的答案包帚。

三、學習總結(jié)

總結(jié)中运吓,遲些時候再補上渴邦。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市拘哨,隨后出現(xiàn)的幾起案子谋梭,更是在濱河造成了極大的恐慌,老刑警劉巖倦青,帶你破解...
    沈念sama閱讀 212,454評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瓮床,死亡現(xiàn)場離奇詭異,居然都是意外死亡产镐,警方通過查閱死者的電腦和手機隘庄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,553評論 3 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來癣亚,“玉大人丑掺,你說我怎么就攤上這事∈鑫恚” “怎么了吼鱼?”我有些...
    開封第一講書人閱讀 157,921評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長绰咽。 經(jīng)常有香客問我菇肃,道長,這世上最難降的妖魔是什么取募? 我笑而不...
    開封第一講書人閱讀 56,648評論 1 284
  • 正文 為了忘掉前任琐谤,我火速辦了婚禮,結(jié)果婚禮上玩敏,老公的妹妹穿的比我還像新娘斗忌。我一直安慰自己质礼,他們只是感情好,可當我...
    茶點故事閱讀 65,770評論 6 386
  • 文/花漫 我一把揭開白布织阳。 她就那樣靜靜地躺著眶蕉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪唧躲。 梳的紋絲不亂的頭發(fā)上造挽,一...
    開封第一講書人閱讀 49,950評論 1 291
  • 那天,我揣著相機與錄音弄痹,去河邊找鬼饭入。 笑死,一個胖子當著我的面吹牛肛真,可吹牛的內(nèi)容都是我干的谐丢。 我是一名探鬼主播,決...
    沈念sama閱讀 39,090評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼蚓让,長吁一口氣:“原來是場噩夢啊……” “哼乾忱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起历极,我...
    開封第一講書人閱讀 37,817評論 0 268
  • 序言:老撾萬榮一對情侶失蹤饭耳,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后执解,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體寞肖,經(jīng)...
    沈念sama閱讀 44,275評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,592評論 2 327
  • 正文 我和宋清朗相戀三年衰腌,在試婚紗的時候發(fā)現(xiàn)自己被綠了新蟆。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,724評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡右蕊,死狀恐怖琼稻,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情饶囚,我是刑警寧澤帕翻,帶...
    沈念sama閱讀 34,409評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站萝风,受9級特大地震影響嘀掸,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜规惰,卻給世界環(huán)境...
    茶點故事閱讀 40,052評論 3 316
  • 文/蒙蒙 一睬塌、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦揩晴、人聲如沸勋陪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,815評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽诅愚。三九已至,卻和暖如春劫映,著一層夾襖步出監(jiān)牢的瞬間违孝,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,043評論 1 266
  • 我被黑心中介騙來泰國打工苏研, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留等浊,地道東北人腮郊。 一個月前我還...
    沈念sama閱讀 46,503評論 2 361
  • 正文 我出身青樓摹蘑,卻偏偏與公主長得像,于是被迫代替她去往敵國和親轧飞。 傳聞我的和親對象是個殘疾皇子衅鹿,可洞房花燭夜當晚...
    茶點故事閱讀 43,627評論 2 350

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