EX27
代碼:
#ex27練習(xí),布爾邏輯表達(dá)式,我用筆在本子上書寫記錄了下來。
#and中的運(yùn)算,有錯(cuò)就錯(cuò),全對(duì)才對(duì)
#or中的運(yùn)算,有對(duì)就對(duì),全錯(cuò)才錯(cuò)
現(xiàn)在使用這些表格創(chuàng)建你自己的卡片,再花一個(gè)星期慢慢記住它們。記住一點(diǎn),這本書不會(huì)要求你成功或者失敗,只要每天盡力去學(xué),在盡力的基礎(chǔ)上多花一點(diǎn)功夫就可以了
我自己用筆記下來了,感覺理解難度還可以,有訣竅。
EX28
代碼:
#ex28的練習(xí)
True
False
False
True
True
True
False
True
False
False
True
False
True
False
False
False
True
True
False
False
加分習(xí)題
- Python 里還有很多和 != 、 == 類似的操作符 . 試著盡可能多地列出 Python 中的等價(jià)運(yùn)算符。
例如 < 或者 <= 就是。 - 寫出每一個(gè)等價(jià)運(yùn)算符的名稱。例如 != 叫 “ not equal (不等于)”。
- 在 python 中測(cè)試新的布爾操作。在敲回車前你需要喊出它的結(jié)果。不要思考,憑自己的第一感就可以了。把表達(dá)式和結(jié)果用筆寫下來再敲回車,最后看自己做對(duì)多少,做錯(cuò)多少。
- 把習(xí)題 3 那張紙丟掉,以后你不再需要查詢它了。
這章的練習(xí)有點(diǎn)懵,不清楚做這個(gè)到底是干嘛的,跟著練習(xí)吧。
ex29
代碼:
#ex29的練習(xí)
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.")
加分習(xí)題
猜猜“ if 語句”是什么,它有什么用處。在做下一道習(xí)題前,試著用自己的話回答下面的問題 :
- 你認(rèn)為 if 對(duì)于它下一行的代碼做了什么? \ 判斷
- 為什么 if 語句的下一行需要 4 個(gè)空格的縮進(jìn)?\ python的統(tǒng)一格式,只要統(tǒng)一的空格數(shù)就可以了。
- 如果不縮進(jìn),會(huì)發(fā)生什么事情?
- 把習(xí)題 27 中的其它布爾表達(dá)式放到
if 語句中會(huì)不會(huì)也可以運(yùn)行呢?試一下。 - 如果把變量 people, cats, 和 dogs 的初始值改掉,會(huì)發(fā)生什么事情? \ 其實(shí)是沒有影響的,他們是變量,只是比較后面的數(shù)字大小。
+= 是什么意思?
x += 1 和 x = x + 1 一樣,只不過可以少打幾個(gè)字母.這句話蠻重要。
ex30
代碼
# ex30練習(xí)的代碼
people = 30#變量
cars = 40
buses = 15
if cars > people:#如果汽車的數(shù)量大于人們的數(shù)量
print("We should take the cars.")#輸出我們應(yīng)該乘坐汽車
elif cas < people:#如果汽車的數(shù)量,小于人們的數(shù)量
print("We should not take the cars.")#輸出我們不應(yīng)該乘坐汽車
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 just take the buses.")
else:
print("Fine, let's stay home then")
總結(jié):
如果多個(gè) elif 區(qū)塊都是 True 是 python 會(huì)如何處理?
Python 只會(huì)運(yùn)行它碰到的是 True 的第一個(gè)區(qū)塊,所以只有第一個(gè)為 True 的區(qū)塊會(huì)被運(yùn)行。
ex31
#ex31的代碼練習(xí)
print("You enter a dark room with two doors. Do you go through door #1 or #2?")
door = input("> ")#b百度了下,input的用法在3.0以上的版本,使用方法改變了
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 tetina.")
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 poor of muck. Good job!")
else:
print("You stumble around and fall on a knife and die. Good job!")
總結(jié):#b百度了下,input的用法在3.0以上的版本,使用方法改變了
另外一個(gè),我的代碼運(yùn)行的結(jié)果,我不確定正確與否,雖然沒有報(bào)錯(cuò)。
ex32
代碼
#ex32的代碼練習(xí)
the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pear', 'apricots'] #水果 = 蘋果 橘子 梨子 杏仁
change = [1, 'pennies', 2, 'dimes']#變化 = 1,便士,2,10美分
# this first kind of for-loop goes through a list
for number in the_count:
print("This is count %d" % number)
# same as above #t同上
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:# for XX in YY ,這個(gè)格式很重要
print("I got %r" % i)
# We can also build lists, first start with an empty one #我們還可以創(chuàng)建列表,首先從空列表開始。
elements = []
# then use the range fuction to do 0 to 5 counts
for i in range(0, 6):
print("Adding %d to the list." % i)
# append is a funcation that lists understand #添加一個(gè)功能列表
elements.append(i)
# now we can print them out too
for i in elements:
print("Element was: %d" % i)
運(yùn)行結(jié)果
"C:\Program Files\Python36\python.exe" D:/小克學(xué)習(xí)/python/項(xiàng)目/ex32.py
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: pear
A fruit of type: apricots
I got 1
I got 'pennies'
I got 2
I got 'dimes'
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(start, end, scan):
參數(shù)含義:start:計(jì)數(shù)從start開始。默認(rèn)是從0開始。例如range(5)等價(jià)于range(0, 5);
end:技術(shù)到end結(jié)束,但不包括end.例如:range(0, 5) 是[0, 1, 2, 3, 4]沒有5
scan:每次跳躍的間距,默認(rèn)為1。例如:range(0, 5) 等價(jià)于 range(0, 5, 1)
示例
range(5) #代表從0到5(不包含5)
[0, 1, 2, 3, 4]
range(0,5) #代表從0到5(不包含5)
[0, 1, 2, 3, 4]
range(1,5) #代表從1到5(不包含5)
[1, 2, 3, 4]
range(1,5,2) #代表從1到5,間隔2(不包含5)
[1, 3]
- 在第 22 行,你可以可以直接將 elements 賦值為 range(0,6) ,而無需使用 for 循環(huán)?
- 在 Python 文檔中找到關(guān)于列表的內(nèi)容,仔細(xì)閱讀以下,除了 append 以外列表還支持哪些操作?
ex33
代碼
#ex33 while循環(huán)的練習(xí)
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é)果
"C:\Program Files\Python36\python.exe" D:/小克學(xué)習(xí)/python/項(xiàng)目/wx33.py
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
常見問題回答
for-loop 和 while-loop 有何不同?
for-loop 只能對(duì)一些東西的集合進(jìn)行循環(huán), while-loop 可以對(duì)任何對(duì)象進(jìn)行馴化。
然而, while-loop 比起來更難弄對(duì),而一般的任務(wù)用 for-loop 更容易一些。
循環(huán)好難理解啊,我該怎樣理解?
覺得循環(huán)不好理解,很大程度上是因?yàn)椴粫?huì)順著代碼的運(yùn)行方式去理解代碼。當(dāng)循環(huán)開始時(shí),它會(huì)運(yùn)行整個(gè)區(qū)塊,區(qū)塊結(jié)束后回到開始的循環(huán)語句。如果想把整個(gè)過程視覺化,你可以在循環(huán)的各處塞入 print 語句,用來追蹤變量的變化過程。你可以在循環(huán)之前、循環(huán)的第一句、循環(huán)中間、以及循環(huán)結(jié)尾都放一些 print 語句,研究最后的輸出,并試著理解循環(huán)的工作過程。
總結(jié):
有點(diǎn)朦朧,這個(gè)時(shí)候多寫,總有一天會(huì)豁然開朗的。
ex34
代碼
# ex34 訪問列表的元素的練習(xí)
animals = ['bear', 'python', 'peacock', 'kangaroo', 'whale', 'platypus']
The animals at 1 and is the 2nd animals and is python
The animals at 2 and is the 3rd animals and is peacock
The animals at 0 and is the 1st animals and is bear
The animals at 3 and is the 4th animals and is kangaroo
The animals at 4 and is the 5th animals and is whale
The animals at 2 and is the 3rd animals and is peacock
The animals at 5 and is the 6th animals and is platypus
The animals at 4 and is the 5th animals and is whale
總結(jié):
這幾天事情比較多,沒及時(shí)完成作業(yè),現(xiàn)在總算補(bǔ)全了,松了口氣。
訓(xùn)練量一直有,如果拉下很多,以后更不好補(bǔ),可能就放棄了,以后盡量不要補(bǔ)作業(yè)了。