Learn python the hard way. python test program 2016.04.27
1 # this will not be printed in python !
2 print "I could have code like this." # and the comment after is ignored
3
4 # You can also use a comment to "disable" or comment out a piece of code:
5 # print "This won't run. "
6
7 print "This won't run. "
8
9 print "I will now count my chickens:"
10
11 print "Hens", 25 + 30 / 6
12 print "Roosters", 100 - 25 * 3 % 4
13
14 print "Now I will count the eggs:"
15
16 print 3+2+1-5+4%2-1/4+6
17 print "Is it true that 3 + 2 < 5 - 7 ?"
18
19 print "What is 3+2 ?", 3+2
20 print "What is 5-7 ?", 5-7
21
22 print "Oh, that's why it's False."
23
24 print "How about Some more."
25
26 print "Is it greater ?", 5 > -2
27 print "Is it greater or equal ?", 5 >= -2
28 print "Is it less or equal ?", 5 <= -2
29
30
31
32 # Exercise 5: More Variable and Printing.
33 my_name = 'Zed A. Shaw'
34 my_age = 35 # not a lie
35 my_height = 74 # inches
36 my_weight = 180 # lbs
37 my_eyes = 'Blue'
38 my_teeth = 'White'
39 my_hair = 'Brown'
40
41 print "Let's talk about %s." % my_name
42 print "He's %d inches tall." % my_height
43 print "He's %d pounds heavy." % my_weight
44 print "Actually that's not too heavy."
45 print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
46 print "His teeth are usually %s depending on the coffee." % my_teeth
47
48 # this line is ticky, try to get it exactly right
49 print "If I add %d, %d, and %d I get %d." % (
50 my_age, my_height, my_weight, my_age + my_height + my_weight)
51
52
53
54 # Exercise 4: Variables and Names
55 cars = 100
56 space_in_a_car = 4.0
57 drivers = 30
58 passengers = 90
59 cars_not_driven = cars -drivers
60 cars_driven = drivers
61 carpool_capacity = cars_driven * space_in_a_car
62 average_passengers_per_car = passengers / cars_driven
63
64 print "There are", cars, "cars available."
65 print "There are only", drivers, "drivers available"
66 print "There will be", cars_not_driven, "empty cars today."
67 print "We can transport", carpool_capacity, "people today."
68 print "We have", passengers, "to carpool today."
69 print "We need to put about", average_passengers_per_car, "in each car."
70
71
72
73 x = "They are %d types of people." % 10
74 binary = "binary"
75 do_not = "don't"
76 y = "Those who know %s and those who %s." % (binary, do_not)
77
78 print x
79 print y
80
81 print "I said: %r." % x
82 print "I also said: '%s'." % y
83
84 hilarious = True
85 joke_evaluation = "Isn't that joke so funny ?! %r"
86
87 print joke_evaluation % hilarious
88
89 w = "This is the left side of ..."
90 e = "a string with a right side."
91
92 print w+e
93
94
95
96 print "Mary had a little lamb."
97 print "Its fleece was white as %s." % 'snow'
98 print "And everywhere that Mary went."
99 print "." * 10 # what'd that do ?
100
101 end1 = "C"
102 end2 = "h"
103 end3 = "e"
104 end4 = "e"
105 end5 = "s"
106 end6 = "e"
107 end7 = "B"
108 end8 = "u"
109 end9 = "r"
110 end10 = "g"
111 end11 = "e"
112 end12 = "r"
113
114 # watch that comma at the end. try removing it to see what happens
115 print end1 + end2 + end3 + end4 + end5 + end6 , # use the , it output: Cheese Burger
116 print end7 + end8 + end9 + end10 + end11 + end12 # remove the , it output: Cheese \\ Burger;
class Song(object):
def __init__(self, lyrics):
self.lyrics = lyrics
def sing_me_s_song(self):
for line in self.lyrics:
print line
happy_bday = Song(["Happy birthday to you",
"I don't want to get sued",
"So, I will stop right there"])
bulls_on_parade = Song(["There rally around the family",
"With pockets full of shells"])
happy_bday.sing_me_s_song()
bulls_on_parade.sing_me_s_song()
Stay Hungry,Stay Foolish ...