一柳譬、作業(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
磁餐、or
和not
運算。
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é)中运吓,遲些時候再補上渴邦。