習題2:注釋和井號
程序里的注釋是很重要的。它們可以用自然語言告訴你某段代碼的功能是什么。在你想要臨時移除一段。代碼時,你還可以用注解的方式將這段代碼臨時禁用。接下來的練習將讓你學會注釋:
1 # A comment, this is so you can read your program later.
2 # Anything after the # is ignored by python.
4 print "I could have code like this." # and the comment after is ignored
6 # You can also use a comment to "disable" or comment out a piece of code:
7 # print "This won't run."
9 print "This will run."
習題3:數字和數學計算
有沒有注意到以上只是些符號,沒有運算操作呢?寫完下面的練習代碼后,再回到上面的列表,寫出每個符號的作用。例如+是用來做加法運算的。
12print "I will now count my chickens:"
3print "Hens", 25 + 30 / 6
4print "Roosters", 100 - 25 * 3 % 4
5
6print "Now I will count the eggs:"
78print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
9 print "Is it true that 3 + 2 < 5 - 7?"
10
11 print 3 + 2 < 5 - 7
12
13 print "What is 3 + 2?", 3 + 2
14 print "What is 5 - 7?", 5 - 7
15
16 print "Oh, that's why it's False."
17
18 print "How about some more."
19
20 print "Is it greater?", 5 > -2
21 print "Is it greater or equal?", 5 >= -2
22 print "Is it less or equal?", 5 <= -2
習題4:變量(variable)和命名
你已經學會了print和算術運算。下一步你要學的是“變量”。在編程中,變量只不過是用來指代某個東西的名字。程序員通過使用變量名可以讓他們的程序讀起來更像英語。而且因為程序員的記性都不怎么地,變量名可以讓他們更容易記住程序的內容。如果他們沒有在寫程序時使用好的變量名,在下一次讀到原來寫的代碼時他們會大為頭疼的。
如果你被這章習題難住了的話,記得我們之前教過的:找到不同點、注意細節。
1.在每一行的上面寫一行注解,給自己解釋一下這一行的作用。2.倒著讀你的.py文件。
3.朗讀你的.py文件,將每個字符也朗讀出來。
12cars = 100
3space_in_a_car = 4.0
drivers = 30
4passengers = 90
5cars_not_driven = cars - drivers
6cars_driven = drivers
7carpool_capacity = cars_driven * space_in_a_car
8average_passengers_per_car = passengers / cars_driven
10 print "There are", cars, "cars available."
11 print "There are only", drivers, "drivers available."
12 print "There will be", cars_not_driven, "empty cars today."
13 print "We can transport", carpool_capacity, "people today."
14 print "We have", passengers, "to carpool today."
15 print "We need to put about", average_passengers_per_car, "in each car."
習題5:更多的變量和打印
我們現在要鍵入更多的變量并且把它們打印出來。這次我們將使用一個叫“格式化字符串(format
string)”的東西.每一次你使用"把一些文本引用起來,你就建立了一個字符串。字符串是程序將信息展示給人的方式。你可以打印它們,可以將它們寫入文件,還可以將它們發送給網站服務器,很多事情都是通過字符串交流實現的。
字符串是非常好用的東西,所以再這個練習中你將學會如何創建包含變量內容的字符串。使用專門的格式和語法把變量的內容放到字符串里,相當于來告訴python:“嘿,這是一個格式化字符串,把這些
變量放到那幾個位置。”
一樣的,即使你讀不懂這些內容,只要一字不差地鍵入就可以了。
12my_name = 'Zed A. Shaw'
3my_age = 35 # not a lie
my_height = 74 # inches
4 ?my_weight = 180 # lbs
5my_eyes = 'Blue'
6my_teeth = 'White'
7my_hair = 'Brown'
8print "Let's talk about %s." % my_name
9print "He's %d inches tall." % my_height
10print "He's %d pounds heavy." % my_weight11print "Actually that's not too heavy."12print "He's got %s eyes and %s hair." % (my_eyes, my_hair)13print "His teeth are usually %s depending on the coffee." % my_teeth
1415# this line is tricky, try to get it exactly right16print "If I add %d, %d, and %d I get %d." % (
17my_age, my_height, my_weight, my_age + my_height + my_weight)
18
習題6:字符串(string)和文本
12x = "There are %d types of people." % 10
3binary = "binary"
do_not = "don't"
4y = "Those who know %s and those who %s." % (binary, do_not)
5
6print x
7print y
8print "I said: %r." % x
9 ?print "I also said: '%s'." % y
10
11 hilarious = False
12 joke_evaluation = "Isn't that joke so funny?! %r"
13
14 print joke_evaluation % hilarious
15
16 w = "This is the left side of..."
17 e = "a string with a right side."
18
19 print w + e
20