一吴超、作業(yè)內(nèi)容:
01见芹、將下面的內(nèi)容寫到一個文件中剂娄,取名為ex1.py。這個命名方式很重要玄呛,Python文件最好以.py結(jié)尾阅懦。
1 print "Hello World!"
2 print”Hello Again"
3 print"I like typing this."
4 print"This is fun."
5 print"Yay! Printing."
6 print"I'd much rather you 'not'."
7 print"I "said" do not touch this."
習(xí)題2:注釋和井號
程序里的注釋是很重要的。它們可以用自然語言告訴你某段代碼的功能是什么徘铝。在你想要臨時移除一段耳胎。代碼時,你還可以用注解的方式將這段代碼臨時禁用惕它。接下來的練習(xí)將讓你學(xué)會注釋:
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é)計算
有沒有注意到以上只是些符號怕午,沒有運算操作呢?寫完下面的練習(xí)代碼后,再回到上面的列表淹魄,寫出每個符號的作用郁惜。例如+是用來做加法運算的。
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é)會了print和算術(shù)運算甲锡。下一步你要學(xué)的是“變量”兆蕉。在編程中,變量只不過是用來指代某個東西的名字缤沦。程序員通過使用變量名可以讓他們的程序讀起來更像英語虎韵。而且因為程序員的記性都不怎么地,變量名可以讓他們更容易記住程序的內(nèi)容缸废。如果他們沒有在寫程序時使用好的變量名包蓝,在下一次讀到原來寫的代碼時他們會大為頭疼的。
如果你被這章習(xí)題難住了的話企量,記得我們之前教過的:找到不同點养晋、注意細節(jié)。
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."
習(xí)題5:更多的變量和打印
我們現(xiàn)在要鍵入更多的變量并且把它們打印出來冒嫡。這次我們將使用一個叫“格式化字符串(format
string)”的東西.每一次你使用"把一些文本引用起來拇勃,你就建立了一個字符串。字符串是程序?qū)⑿畔⒄故窘o人的方式孝凌。你可以打印它們方咆,可以將它們寫入文件,還可以將它們發(fā)送給網(wǎng)站服務(wù)器蟀架,很多事情都是通過字符串交流實現(xiàn)的瓣赂。
字符串是非常好用的東西榆骚,所以再這個練習(xí)中你將學(xué)會如何創(chuàng)建包含變量內(nèi)容的字符串。使用專門的格式和語法把變量的內(nèi)容放到字符串里煌集,相當于來告訴python:“嘿妓肢,這是一個格式化字符串,把這些
變量放到那幾個位置苫纤〉锬疲”
一樣的,即使你讀不懂這些內(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
習(xí)題 7: 更多打印
現(xiàn)在我們將做一批練習(xí),在練習(xí)的過程中你需要鍵入代碼栗弟,并且讓它們運行起來污筷。我不會解釋太多,因
為這節(jié)的內(nèi)容都是以前熟悉過的横腿。這節(jié)練習(xí)的目的是鞏固你學(xué)到的東西颓屑。我們幾個練習(xí)后再見。不要跳
過這些習(xí)題耿焊。不要復(fù)制粘貼!
你應(yīng)該看到的結(jié)果
$ python ex7.py
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
..........
Cheese Burger
$
習(xí)題 8: 打印揪惦,打印
你應(yīng)該看到的結(jié)果
$ python ex8.py
1 234
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'
$
習(xí)題 9: 打印,打印罗侯,打印
你應(yīng)該看到的結(jié)果
$ python ex9.py
Here are the days: Mon Tue Wed Thu Fri Sat Sun
Here are the months: Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
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.
$
習(xí)題 10: 那是什么?
I'm tabbed in.
I'm split
on a line.
I'm \ a \ cat.
I'll do a list:
* Cat food
* Fishies
* Catnip
* Grass
習(xí)題 11: 提問
How old are you? 35
How tall are you? 6'2"
How much do you weigh? 180lbs
So, you're '35' old, '6\'2"' tall and '180lbs' heavy.