習(xí)題2:注釋和井號(hào)
程序里的注釋是很重要的癞松。它們可以用自然語(yǔ)言告訴你某段代碼的功能是什么爽撒。在你想要臨時(shí)移除一段。代碼時(shí)响蓉,你還可以用注解的方式將這段代碼臨時(shí)禁用硕勿。接下來(lái)的練習(xí)將讓你學(xué)會(huì)注釋:
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."
習(xí)題3:數(shù)字和數(shù)學(xué)計(jì)算
有沒(méi)有注意到以上只是些符號(hào),沒(méi)有運(yùn)算操作呢?寫(xiě)完下面的練習(xí)代碼后枫甲,再回到上面的列表源武,寫(xiě)出每個(gè)符號(hào)的作用。例如+是用來(lái)做加法運(yùn)算的想幻。
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
習(xí)題4:變量(variable)和命名
你已經(jīng)學(xué)會(huì)了print和算術(shù)運(yùn)算粱栖。下一步你要學(xué)的是“變量”。在編程中举畸,變量只不過(guò)是用來(lái)指代某個(gè)東西的名字查排。程序員通過(guò)使用變量名可以讓他們的程序讀起來(lái)更像英語(yǔ)。而且因?yàn)槌绦騿T的記性都不怎么地抄沮,變量名可以讓他們更容易記住程序的內(nèi)容跋核。如果他們沒(méi)有在寫(xiě)程序時(shí)使用好的變量名,在下一次讀到原來(lái)寫(xiě)的代碼時(shí)他們會(huì)大為頭疼的叛买。
如果你被這章習(xí)題難住了的話(huà)砂代,記得我們之前教過(guò)的:找到不同點(diǎn)、注意細(xì)節(jié)率挣。
1.在每一行的上面寫(xiě)一行注解刻伊,給自己解釋一下這一行的作用。2.倒著讀你的.py文件椒功。
3.朗讀你的.py文件捶箱,將每個(gè)字符也朗讀出來(lái)。
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."
習(xí)題5:更多的變量和打印
我們現(xiàn)在要鍵入更多的變量并且把它們打印出來(lái)动漾。這次我們將使用一個(gè)叫“格式化字符串(format
string)”的東西.每一次你使用"把一些文本引用起來(lái)丁屎,你就建立了一個(gè)字符串。字符串是程序?qū)⑿畔⒄故窘o人的方式旱眯。你可以打印它們晨川,可以將它們寫(xiě)入文件证九,還可以將它們發(fā)送給網(wǎng)站服務(wù)器,很多事情都是通過(guò)字符串交流實(shí)現(xiàn)的共虑。
字符串是非常好用的東西愧怜,所以再這個(gè)練習(xí)中你將學(xué)會(huì)如何創(chuàng)建包含變量?jī)?nèi)容的字符串。使用專(zhuān)門(mén)的格式和語(yǔ)法把變量的內(nèi)容放到字符串里妈拌,相當(dāng)于來(lái)告訴python:“嘿拥坛,這是一個(gè)格式化字符串,把這些
變量放到那幾個(gè)位置尘分】事撸”
一樣的,即使你讀不懂這些內(nèi)容音诫,只要一字不差地鍵入就可以了。
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
習(xí)題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