【Python爬蟲】-笨辦法學(xué) Python 習(xí)題27-34

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

笨辦法學(xué) Python 習(xí)題27-34以及加分題。

二、作業(yè)代碼:

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

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

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

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

布爾值可以用andornot運算。
and運算是與運算,只有所有都為Trueand運算結(jié)果才是True

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

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

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

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

# 習(xí)題 28: 布爾表達式練習(xí)

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

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

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



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


# 四、把習(xí)題 3 那張紙丟掉,以后你不再需要查詢它了。
# 習(xí)題 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
# 加分習(xí)題

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

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


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


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


# 四、把習(xí)題 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ā)生變化。
# 習(xí)題 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.")
# 加分習(xí)題
# 一、猜想一下 elif 和 else 的功能。
# elif
# 二、將 cars, people, 和 buses 的數(shù)量改掉,然后追溯每一個 if 語句。看看最后會打印出什么來。
# 三、試著寫一些復(fù)雜的布爾表達式,例如 cars > people and buses < cars。
# 四、在每一行的上面寫注解,說明這一行的功用。
# 習(xí)題 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!")
# 加分習(xí)題

為游戲添加新的部分,改變玩家做決定的位置。盡自己的能力擴展這個游戲,不過別把游戲弄得太怪異了。
# 習(xí)題 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

# 加分習(xí)題
# 一、注意一下 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

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

# 加分習(xí)題
# 一、將這個 while 循環(huán)改成一個函數(shù),將測試條件(i < 6)中的 6 換成一個變量。
# 二、使用這個函數(shù)重寫你的腳本,并用不同的數(shù)字進行測試。
# 三、為函數(shù)添加另外一個參數(shù),這個參數(shù)用來定義第 8 行的加值 + 1 ,這樣你就可以讓它任意加值了。
# 四、再使用該函數(shù)重寫一遍這個腳本。看看效果如何。
# 五、接下來使用 for-loop 和 range 把這個腳本再寫一遍。你還需要中間的加值操作嗎?如果你不去掉它,會有什么樣的結(jié)果?
# 習(xí)題 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

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

三、學(xué)習(xí)總結(jié)

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,825評論 6 546
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,814評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,980評論 0 384
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 64,064評論 1 319
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,779評論 6 414
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,109評論 1 330
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,099評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,287評論 0 291
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,799評論 1 338
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,515評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,750評論 1 375
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,221評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,933評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,327評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,667評論 1 296
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,492評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,703評論 2 380

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