笨办法学Python(learn python the hard way)--练习程序1-10

下面是当初看这本书时按照书中的代码做的练习,一行一行敲下来的,都已经试运行过,没有错误(基于python3),练习1-练习10
#ex1.py
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.')
8 print('')
#ex2.py
1
# A comment, this is so you can read your program later. 2 # Anything after the # is ignored by python. 3 4 print("I could have code like this.") # and the comment after is ignored) 5 6 # You can also use a comment to "disable"or comment out a piece of code: 7 # print("This won't run.") 8 9 print("This will run.")
 #ex3.py
1
print("I will now count my chickens:") 2 3 print("Hens",25+30/6) 4 print("Poosters",100-25*3%4) 5 6 print("Now I will count the eggs:") 7 8 print(3+2+1-5+4%2-1//4+6) 9 10 print("Is it true that 3+2<5-7?") 11 12 print(3+2<5-7) 13 14 print("What is 3+2?",3+2) 15 print("What is 5-7?",5-7) 16 17 print("Oh,that's why it's False.") 18 19 print("How about some more.") 20 21 print("Is it greater?",5 > -2) 22 print("Is it greater or equal?",5 >= -2) 23 print("Is it less or equal?",5 <= -2)
 #ex4.py
1
#给变量cars赋值为100 2 cars = 100 3 #给变量space_in_a_car赋值为4.0 4 space_in_a_car = 4 5 #给变量drivers赋值为30 6 drivers = 30 7 #给变量passengers赋值为90 8 passengers = 90 9 cars_not_driven = cars - drivers 10 cars_driven = drivers 11 carpool_capacity = cars_driven * space_in_a_car 12 average_passengers_per_car = passengers // cars_driven 13 14 15 print("There are",cars,"cars available.") 16 print("There are only",drivers,"drivers available.") 17 print("There will be",cars_not_driven,"empty cars today.") 18 print("We can transport",carpool_capacity,"people today.") 19 print("We have",passengers,"to carpool today.") 20 print("We need to put about",average_passengers_per_car,"in each car.")
 #ex5.py
1
my_name = 'Zed A. Shaw' 2 my_age = 35 # not a lie 3 my_height = 74 # inches 4 my_weight = 180 #lbs 5 my_eyes = 'Blue' 6 my_teeth = 'White' 7 my_hair = 'Brown' 8 9 print("Let's talk about %s."% my_name) 10 print("He's %d inches tall."% my_height) 11 print("He's %d pounds heavy."% my_weight) 12 print("Actually that's not too heavy.") 13 print("His teeth are usually %s depending on the coffee."% my_teeth) 14 15 # this line is tricky,try to get it exactly right 16 print("If I add %d,%d,and %d I get %d."%(my_age,my_height,my_weight,my_age+my_height+my_weight))
 #ex6.py
1
x = "There are %d types of people."%10 2 binary = "binary" 3 do_not = "don't" 4 y = "Those who know %s and those who %s."%(binary,do_not) 5 6 print(x) 7 print(y) 8 9 print("I said:%r."%x) 10 print("I also said:'%s'."%y) 11 12 hilarious = False 13 joke_evaluation = "Isn't that joke so funny?!%r" 14 15 print(joke_evaluation % hilarious) 16 17 w = "This is the left side of..." 18 e = "a string with a right side." 19 20 print(w+e)
 #ex7.py
1
print("Mary had a little lamb.") 2 print("Its fleece was white as %s."%'snow') 3 print("And everywhere that Mary went.") 4 print("."*10) # what'd that do? 5 6 end1 = "C" 7 end2 = "h" 8 end3 = "e" 9 end4 = "e" 10 end5 = "s" 11 end6 = "e" 12 end7 = "B" 13 end8 = "u" 14 end9 = "r" 15 end10 = "g" 16 end11 = "e" 17 end12 = "r" 18 19 # watch that comma at the end. try removing it to see what happens 20 21 print(end1+end2+end3+end4+end5+end6,end=" ") 22 print(end7+end8+end9+end10+end11+end12)
 #ex8.py
1
formatter = "%r %r %r %r" 2 3 print(formatter %(1,2,3,4)) 4 print(formatter %("one","two","three","four")) 5 print(formatter %(True,False,False,True)) 6 print(formatter %(formatter,formatter,formatter,formatter)) 7 print(formatter %( 8 "I had this thing.", 9 "That you could type up right.", 10 "But it didn't sing.", 11 "So I said goodnight." 12 ))
 #ex9.py
1
# Here's some new strange stuff,remember type it exactly. 2 3 days = "Mon Tue Wed Thu Fri Sat Sun" 4 months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" 5 6 print("Here are the days: ",days) 7 print("Here are the months: ",months) 8 9 print(""" 10 There's something going on here. 11 With the three double-quotes. 12 We'll be able to type as much as we like. 13 Even 4 lines if we want,or 5,or 6. 14 """)
 #ex10.py
1
tabby_cat = "\tI'm tabbed in." 2 persian_cat = "I'm split\non a line." 3 backslash_cat = "I'm \\a\\ cat." 4 5 fat_cat = ''' 6 I'll do a list: 7 \t* Cat food 8 \t* Fishies 9 \t* Catnip\n\t* Grass 10 ''' 11 12 print(tabby_cat) 13 print(persian_cat) 14 print(backslash_cat) 15 print(fat_cat)

 

posted @ 2018-09-06 19:24  西柚汁  阅读(345)  评论(0编辑  收藏  举报