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

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

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

二、作業(yè)代碼:

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

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

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

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

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

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

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

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

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

# 習(xí)題 28: 布爾表達(dá)式練習(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 中的等價(jià)運(yùn)算符。例如 < 或者 <= 就是。
# 二、寫出每一個(gè)等價(jià)運(yùn)算符的名稱。例如 != 叫 “not equal(不等于)”。

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



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


# 四、把習(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.")

運(yùn)算結(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語(yǔ)句”是什么,它有什么用處。
# 在做下一道習(xí)題前,試著用自己的話回答下面的問(wèn)題:
# 一、你認(rèn)為 if 對(duì)于它下一行的代碼做了什么?

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


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


# 三、如果不縮進(jìn),會(huì)發(fā)生什么事情?
# 答:會(huì)報(bào)錯(cuò)。
IndentationError: expected an indented block


# 四、把習(xí)題 27 中的其它布爾表達(dá)式放到``if語(yǔ)句``中會(huì)不會(huì)也可以運(yùn)行呢?試一下。
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!")

# 答:可以運(yùn)行。

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

執(zhí)行的結(jié)果會(huì)發(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ù)量改掉,然后追溯每一個(gè) if 語(yǔ)句。看看最后會(huì)打印出什么來(lái)。
# 三、試著寫一些復(fù)雜的布爾表達(dá)式,例如 cars > people and buses < cars。
# 四、在每一行的上面寫注解,說(shuō)明這一行的功用。
# 習(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í)題

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

運(yùn)行結(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開(kāi)始小于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)

運(yùn)行結(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),而無(wú)需使用 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)

運(yùn)行結(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)容,仔細(xì)閱讀一下,除了 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)


運(yùn)行結(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)

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


運(yùn)行結(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í)并閱讀一下。
# 二、以你對(duì)于這些不同的數(shù)字類型的了解,解釋一下為什么 “January 1, 2010” 里是 2010 而不是 2009?(提示:你不能隨機(jī)挑選年份。)
# 三、再寫一些列表,用一樣的方式作出索引,確認(rèn)自己可以在兩種數(shù)字之間互相翻譯。
# 使用 python 檢查自己的答案。

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

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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