標籤:
ex1.py
1 print "Hello World!"2 print "Hello Qiwei"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.‘
ex2.py Comments and Pound Characters
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 ignored5 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 Numbers and Math
1 print "I will now count my chickens:" 2 3 print "Hens", 25 + 30 /6 4 print "Roosters", 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 - 713 14 print "What is 3 + 2?", 3 + 215 print "What is 5 - 7?", 5 - 716 17 print "Oh, that‘s why it‘s False."18 19 print "How about some more."20 21 print "Is it greater?", 5 > -222 print "Is it greater or equal", 5 >= -223 print "Is it less or equal?", 5 <= -2
ex4.py Variables And Names
1 cars = 100 2 space_in_a_car = 4.0 3 drivers = 30 4 passengers = 90 5 cars_not_driven = cars - drivers 6 cars_driven = drivers 7 carpool_capacity = cars_driven * space_in_a_car 8 average_passengers_per_car = passengers / cars_driven 9 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 transprot", 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."
Learn Python the Hard Way