- 習(xí)題1:第一個(gè)程序
# --coding:utf-8 --
print("Hello World!")
print("Hello Again")
print("I like typing this.")
print("This is fun.")
print('Yay! Printing.')
print("I'd much rather you 'not'.")
print('I "said" do not touch this.')
加分習(xí)題
1.1 讓你的腳本再多打印一行。
print("Hello World!\n")
print("Hello Again\n")
print("I like typing this.\n")
print("This is fun.\n")
print('Yay! Printing.\n')
print("I'd much rather you 'not'.\n")
print('I "said" do not touch this.\n')
1.2 讓你的腳本只打印一行。
print('I "said" do not touch this.')
1.3 在一行的起始位置放一個(gè) '#' (octothorpe) 符號(hào)。它的作用是什么?
# 在一行的起始位置放一個(gè) '#' (octothorpe) 符號(hào)。
# 它的作用是注釋?zhuān)琍ython編譯器將忽略這段注釋?zhuān)瑥南乱粋€(gè)不以 '#'開(kāi)頭的代碼段開(kāi)始或繼續(xù)運(yùn)行。
- 習(xí)題2: 注釋和井號(hào)
# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print("I could have code like this.") # and the comment after is ignored
# You can also use a comment to "disable" or comment out of a piece of code:
# print("This won't run.")
print("This will run.")
- 習(xí)題3:數(shù)字和數(shù)學(xué)計(jì)算
print("I will now count my chickens:")
print("Hens", 25 + 30 / 6)
print("Roosters", 100 - 25 * 3 % 4)
print("Now I will count the eggs:")
print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)
print("Is it ture that 3 + 2 < 5 - 7?")
print(3 + 2 < 5 - 7)
print("What is 3 + 2?", 3 + 2)
print("What is 5 - 7?", 5 - 7)
print("Oh, that's why it's False.")
print("How about some more.")
print("Is it greater?", 5 > -2)
print("Is it greater or equal?", 5 >= -2)
print("Is it less or equal?", 5 <= -2)
加分習(xí)題
3.4 有沒(méi)有發(fā)現(xiàn)計(jì)算結(jié)果是”錯(cuò)”的呢?計(jì)算結(jié)果只有整數(shù),沒(méi)有小數(shù)部分。研究一下這是為什么,搜索一下“浮點(diǎn)數(shù)(floating point number)”是什么東西。
python將帶小數(shù)點(diǎn)的數(shù)字都稱(chēng)為浮點(diǎn)數(shù)。在Python2中 計(jì)算3/2會(huì)得出結(jié)果為1,整數(shù)除法的結(jié)果只包含整數(shù)部分,小數(shù)部分被刪除。
若要避免這種情況,可使用Python3,或者再計(jì)算公式中至少保證有一個(gè)操作數(shù)為浮點(diǎn)數(shù)。這樣結(jié)果也會(huì)為浮點(diǎn)數(shù)。
下面兩組例子。
print(3 + 2) # 輸出結(jié)果為5
print(3 + 2.0) # 輸出結(jié)果為5.0
print("Roosters", 100 - 25 * 3 % 4) # 輸出結(jié)果為'Roosters 97'
print("Roosters", 100.0 - 25 * 3 % 4) # 輸出結(jié)果為'Roosters 97.0'
- 習(xí)題4:變量和命名
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car
average_passengers_per_car = passengers / cars_driven
print("There are", cars, "cars available.\n")
print("There are only", drivers, "divers available.\n")
print("There will be", cars_not_driven, "empty cars today.\n")
print("We can tansport", carpool_capacity, "people today.\n")
print("We have", passengers, "to carpool today.\n")
print("We need to put about", average_passengers_per_car, "in each car.\n")
加分習(xí)題
4.0 name 'car_pool_capacity' is not defined錯(cuò)誤
“name 'car_pool_capacity' is not defined”是說(shuō)變量car_pool_capacity未被定義。
比較前面被賦值的變量名稱(chēng),可以看到car_pool_capacity在car和pool之間多了一個(gè)下劃線
一般來(lái)說(shuō)出現(xiàn)這種錯(cuò)誤有兩種情況:
①使用了未被定義/賦值的變量名;
②使用了與已定義/賦值的變量名稱(chēng)不一致的變量名。
此處是第二種情況。即14行調(diào)用的變量car_pool_capacity與第7行定義的變量名稱(chēng)carpool_capacity不一致。
'''
4.1 我在程序里用了 4.0 作為 space_in_a_car 的值,這樣做有必要嗎?如果只用 4 會(huì)有什么問(wèn)題?
有必要。Python2中整數(shù)除以整數(shù)結(jié)果只保留整數(shù)位,去除了小數(shù),如果想要結(jié)果準(zhǔn)確,應(yīng)該用浮點(diǎn)數(shù)4.0而不是整數(shù)4.
4.3 在每一個(gè)變量賦值的上一行加上一行注解。
# --coding:utf-8 --
# 給cars變量賦值100
cars = 100
# 給space_in_a_car變量賦值4.0
space_in_a_car = 4.0
# 給drivers變量賦值30
drivers = 30
# 給passengers變量賦值90
passengers = 90
# 將cars - drivers的計(jì)算結(jié)果賦值給cars_not_driven
cars_not_driven = cars - drivers
#將drivers的值賦給cars_driven
cars_driven = drivers
# 將 cars_driven * space_in_a_car的值賦給carpool_capacity
carpool_capacity = cars_driven * space_in_a_car
# 將passengers / cars_driven的值賦給average_passengers_per_car
average_passengers_per_car = passengers / cars_driven
print("There are", cars, "cars available.\n")
print("There are only", drivers, "divers available.\n")
print("There will be", cars_not_driven, "empty cars today.\n")
print("We can tansport", carpool_capacity, "people today.\n")
print("We have", passengers, "to carpool today.\n")
print("We need to put about", average_passengers_per_car, "in each car.\n")
- 習(xí)題5:更多的變量和打印
# -- coding:utf-8 --
# 更多的變量和打印
my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
print("Let's talk about %s." % my_name)
print("He's %d inches tall." % my_height)
print("He's %d pounds heavy." % my_weight)
print("Actually that's not too heavy.")
print("He's got %s eyes and %s hair." % (my_eyes, my_hair))
print("His teeth are usually %s depending on the coffee." % my_teeth)
# this line is tricky, try to get it exactly right
print("If I add %d, %d, and %d I get %d." % (my_age, my_height, my_weight, my_age + my_height + my_weight))
加分習(xí)題
5.1 修改所有的變量名字,把它們前面的my_去掉。確認(rèn)將每一個(gè)地方的都改掉,不只是你使用=賦值過(guò)的地方。
name = 'Zed A. Shaw'
age = 35 # not a lie
height = 74 # inches
weight = 180 # lbs
eyes = 'Blue'
teeth = 'White'
hair = 'Brown'
print("Let's talk about %s." % name)
print("He's %d inches tall." % height)
print("He's %d pounds heavy." % weight)
print("Actually that's not too heavy.")
print("He's got %s eyes and %s hair." % (eyes, hair))
print("His teeth are usually %s depending on the coffee." % teeth)
# this line is tricky, try to get it exactly right
print("If I add %d, %d, and %d I get %d." %
(age, height, weight, age + height + weight))
5.2 試著使用更多的格式化字符。例如 %r 就是是非常有用的一個(gè),它的含義是“不管什么都打印出來(lái)”。
格式化字符串時(shí),Python使用一個(gè)字符串作為模板。
模板中有格式符,這些格式符為真實(shí)值預(yù)留位置,并說(shuō)明真實(shí)數(shù)值應(yīng)該呈現(xiàn)的格式。
Python用一個(gè)tuple將多個(gè)值傳遞給模板,每個(gè)值對(duì)應(yīng)一個(gè)格式符。
%s表示一個(gè)字符串。%d表示一個(gè)整數(shù)。%號(hào)代表了格式化操作.
http://www.cnblogs.com/vamei/archive/2013/03/12/2954938.htm
《笨辦法學(xué)Python》里直接上手代碼,新手不太容易理解,可以結(jié)合上面給的網(wǎng)址看一下補(bǔ)充理解。
name = 'Zed A. Shaw'
age = 35 # not a lie
height = 74 # inches
weight = 180 # lbs
eyes = 'Blue'
teeth = 'White'
hair = 'Brown'
print("Let's talk about %r." % name)
print("He's %r inches tall." % height)
6.字符串(string)和文本
x = "There are %d types of people." % 10
binary = 'binary'
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)
print(x)
print(y)
print("I said: %r." % x)
print("I also said: '%s'." % y)
hilarious = False
joke_evaluation ="Isn't that joke so funny?! %r"
print(joke_evaluation % hilarious)
w = "This is the lfet side of..."
e = "a string with a right side."
print(w + e)
加分習(xí)題
6.2 找到所有的”字符串包含字符串”的位置
x = "There are %d types of people." % 10
y = "Those who know %s and those who %s." % (binary, do_not)
print("I said: %r." % x)
print("I also said: '%s'." % y)
joke_evaluation ="Isn't that joke so funny?! %r"
print(joke_evaluation % hilarious)
6.4 解釋一下為什么 w 和 e 用 + 連起來(lái)就可以生成一個(gè)更長(zhǎng)的字符串。
1.大前提是w和e是相同類(lèi)型字符,w + e不會(huì)出現(xiàn)類(lèi)型錯(cuò)誤。
2.如果是數(shù)字+數(shù)字,那么輸出結(jié)果為和值。
3.Python使用加號(hào)+來(lái)合并字符串,w + e這種合并字符串的方法稱(chēng)為拼接。
7.更多打印
print("Mary had a little lamb.")
print("Its fleece was white as %s." % 'snow')
print("And everywhere that Mary went.")
print("." *10 ) # what'd that do?
end1 = "C"
end2 = 'h'
end3 = 'e'
end4 = 'e'
end5 = 's'
end6 = 'e'
end7 = 'B'
end8 = 'u'
end9 = 'r'
end10 = 'g'
end11 = 'e'
end12 = 'r'
# watch that comma at the end. try removing it to see what happens
print(end1 + end2 + end3 + end4 + end5 + end6,)
print(end7 + end8 + end9 + end10 + end11 + end12)
8.習(xí)題8:打印,打印
formatter = "%r %r %r %r"
print(formatter % (1, 2, 3, 4))
print(formatter % ('one', 'two', 'three', 'four'))
print(formatter % (True, False, False, True))
print(formatter % (formatter, formatter, formatter, formatter))
print(formatter %
("I had this thingg.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight.")
)
加分習(xí)題
8.2 注意最后一行程序中既有單引號(hào)又有雙引號(hào),你覺(jué)得它是如何工作的?
撇號(hào)位于兩個(gè)雙引號(hào)之間時(shí),Python解釋器能夠正確的理解這個(gè)字符串。
如果在用單引號(hào)括起的字符串中,包含撇號(hào),Python會(huì)將第一個(gè)單引號(hào)和撇號(hào)之間的內(nèi)容視為一個(gè)字符串,將剩下的文本視為Python代碼。
9.打印,打印,打印
# -- coding:utf-8 --
# Here's sonme new strange stuff, remember type it exactly.
days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
print("Here are the days: ", days)
print("Here are the months: ", months)
print("""
There's something going on here.
with the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""
)
10.那是什么?
# -- coding:utf-8 --
# 那是什么?
print("I am 6'2\" tall.") # 使用\將字符串中的雙引號(hào)轉(zhuǎn)義
print('I am 6\'2" tall.') # 使用\將字符串中的單引號(hào)轉(zhuǎn)義
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."
fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t Grass
"""
print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)
加分習(xí)題
10.1 上網(wǎng)搜索一下還有哪些可用的轉(zhuǎn)義字符。
http://blog.chinaunix.net/uid-20794157-id-3038417.html
收到幾個(gè)網(wǎng)址,但是怎么用還不是很熟悉。
10.2 使用 ''' (三個(gè)單引號(hào))取代三個(gè)雙引號(hào),看看效果是不是一樣的?
使用 ''' (三個(gè)單引號(hào))取代三個(gè)雙引號(hào),效果是一樣的。這是在不引起Python正確理解的前提下。
170711 《笨辦法學(xué)Python》0~10習(xí)題練習(xí)總結(jié)
1.新學(xué)到的知識(shí)點(diǎn)
①格式化字符串
②轉(zhuǎn)義與轉(zhuǎn)義符號(hào)
2.尚未掌握的知識(shí)點(diǎn)
①格式化字符串
②轉(zhuǎn)義與轉(zhuǎn)義符號(hào)
這兩個(gè)知識(shí)點(diǎn)是我學(xué)Python以來(lái)新出現(xiàn)的知識(shí)點(diǎn),在《Python編程:從入門(mén)到實(shí)踐》中沒(méi)有涉及到。今天的練習(xí)就熟悉了一下代碼,大致了解了是什么一個(gè)用法、用途,能看得懂習(xí)題代碼,卻還不能夠延伸。比如習(xí)題5的加分練習(xí)里面,有一個(gè)用格式化字符串“將英寸和磅轉(zhuǎn)換成厘米和千克”的練習(xí),以及“將轉(zhuǎn)義序列和格式化字符串放到一起,創(chuàng)建一種更復(fù)雜的格式?!?/em>的練習(xí),我就有點(diǎn)一籌莫展了。
3.學(xué)習(xí)心得
與我之前學(xué)的《Python編程:從入門(mén)到實(shí)踐》循序漸進(jìn)的教學(xué)路數(shù)不同,《笨辦法學(xué)Python》以練代學(xué),在遇到問(wèn)題時(shí)通過(guò)自己發(fā)現(xiàn)問(wèn)題、自己查資料、再解決問(wèn)題。這樣的套路對(duì)于有一定基礎(chǔ)的同學(xué)很有幫助,既能夠熟悉代碼鞏固已學(xué)知識(shí),又能夠?qū)W到知識(shí)。但《笨辦法學(xué)Python》對(duì)具體的語(yǔ)法沒(méi)有講解。我覺(jué)得如果有一些Python基礎(chǔ)語(yǔ)法基礎(chǔ)的同學(xué)去學(xué)會(huì)比較輕松一點(diǎn)。