# 1 What suffix do Python source files have? .py Name the Python source file ex1 ex1.py How would one execute ex1.py? python ex1.py Is this correct? print "Hello World!" yes Is this correct? print "Hello World!' no Is this correct? print 'Hello World!' yes Is this correct? print "I'd much rather you 'not'." yes Is this correct? print 'I'd much rather you "not".' no Is this correct? print 'I "said" do not touch this.' yes Is this correct? print 'I\'d much rather you "not".' yes Is this correct? print 'I\'d much rather you \"not\".' yes Is this correct? print 'I'd much rather you \"not\".' no Evaluate: print "Hello World!" Hello World! Evaluate: print "Hello Again" Hello Again Evaluate: print "I like typing this." I like typing this. Evaluate: print "This is fun." This is fun. Evaluate: print 'Yay! Printing.' Yay! Printing Evaluate: print "I'd much rather you 'not'." I'd much rather you 'not'. Evaluate: print 'I "said" do not touch this.' I "said" do not touch this. # 2 Python comments are...? anything following a '#' Evaluate: print "I could have code like this." # comment # print "This won't run." I could have code like this. Evaluate: # print "I could have code like this." # comment # print "This will run." (nothing) # 3 Evaluate: print "I will now count my chickens:" I will now count my chickens: Evaluate: print "Hens", 25 + 30 / 6 Hens 30 Correct? print "Hens" 25 + 30 / 6 no; SyntaxError: invalid syntax Correct? print "Hens", 25 + 30 / 6, yes Evaluate: print "Hens", 25 + 30 / 6, Hens 30 Correct? print "Hens", (25 + 30 / 6) yes Evaluate: print "Roosters", 100 - 25 * 3 % 4 Roosters 97 Evaluate: print "Roosters", 100 - 25 * 3 % 4 Roosters 97 Evaluate: print "Roosters", 100 - 25 * (3 % 4) Roosters 25 Evaluate: print "Roosters", 100 - (25 * 3 % 4) Roosters 97 Evaluate: print "Roosters", 100 - (25 * 3) % 4 Roosters 97 Evaluate: print "Now I will count the eggs:" Now I will count the eggs: Evaluate: print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 7 Evaluate: print "Is it true that 3 + 2 < 5 - 7?" Is it true that 3 + 2 < 5 - 7? Evaluate: print 3 + 2 < 5 - 7 False Evaluate: print "What is 3 + 2?", 3 + 2 What is 3 + 2? 5 Evaluate: print "What is 5 - 7?", 5 - 7 What is 5 - 7? -2 Evaluate: print "Oh, that's why it's False." Oh, that's why it's False. Evaluate: print "How about some more." How about some more. Evaluate: print "Is it greater?", 5 > -2 Is it greater? True Evaluate: print "Is it greater or equal?", 5 >= -2 Is it greater or equal? True Evaluate: print "Is it less or equal?", 5 <= -2 Is it less or equal? False Evaluate: 25 + 30 / 6 30 Evaluate: 25 + (30 / 6) 30 Evaluate: 100 - 25 * 3 % 4 97 Evaluate: 100 - 25 * (3 % 4) 25 Evaluate: 100 - 25.0 * (3 % 4) 25.0 Evaluate: 100 - 25.0 * 3 % 4 97.0 Evaluate: 100 - 25 * 3 % 4.0 97.0 Evaluate: 30 / 7 4 Evaluate: 30.0 / 7 4.2857142857142856 Evaluate: 30 / 7.0 4.2857142857142856 Correct? true & true no Correct? true & True no Correct? True & True yes Evaluate: true error: NameError: name 'true' is not defined Evaluate: True True Evaluate: False False Evaluate: false error: NameError: name 'false' is not defined # 4 Correct? cars_driven = 30 space_in_a_car = 4.0 # comment carpool_capacity = cars_driven * space_in_a_car print "We can transport", carpool_capacity, "people today." yes Evaluate: cars_driven = 30 space_in_a_car = 4.0 # comment carpool_capacity = cars_driven * space_in_a_car print "We can transport", carpool_capacity, "people today." We can transport 120.0 people today. Evaluate: var = 2 var = 4 print var 4 Evaluate: cars_driven = 30 space_in_a_car = 4 # comment carpool_capacity = cars_driven * space_in_a_car print "We can transport", carpool_capacity, "people today." We can transport 120 people today. Evaluate: cars_driven = 30 space_in_a_car = 4 # comment carpool_capacity = cars_driven * space_in_a_car print "We can transport" carpool_capacity "people today." error; SyntaxError: invalid syntax Correct? cars = 100 print "There are", cars, "cars available." yes Correct? val cars = 100 print "There are", cars, "cars available." no Correct? int cars = 100 print "There are", cars, "cars available." no Correct? int cars = 100; print "There are", cars, "cars available."; no Correct? cars = 100 print "There are", cars, "cars available."; yes Correct? cars = 100; print "There are", cars, "cars available."; yes Evaluate: space_in_a_car = 4.0 # why 4.0? drivers = 10 carpool_capacity = drivers * space_in_a_car print "We can transport", car_pool_capacity, "people today." error; NameError: name 'car_pool_capacity' is not defined # 5 Evaluate: my_name = 'Zed A. Shaw' print "Let's talk about %s." % my_name Let's talk about Zed A. Shaw. Evaluate: my_name='Zed A. Shaw' print "Let's talk about %s." % my_name Let's talk about Zed A. Shaw. Evaluate: my_name="Zed A. Shaw" print "Let's talk about %s." % myname Traceback (most recent call last): File "", line 1, in NameError: name 'myname' is not defined Evaluate: my_name = 'Zed A. Shaw' print "Let's talk about %my_name." Let's talk about %my_name. Evaluate: my_weight = 180 print "He's %s pounds heavy" % my_weight He's 180 pounds heavy Evaluate: my_weight = 180 print "He's %d pounds heavy" % my_weight He's 180 pounds heavy Evaluate: my_eyes = 'Blue' my_hair = 'Brown' print "he's got %s eyes and %s hair" % (my_eyes, my_hair) he's got Blue eyes and Brown hair Evaluate: my_eyes = 'Blue' my_hair = 'Brown' print "he's got %s eyes and %s hair" % {my_eyes, my_hair} Traceback (most recent call last): File "", line 1, in TypeError: not enough arguments for format string Evaluate: my_eyes = 'Blue'; my_hair = 'Brown' print "he's got %s eyes and %s hair" % (%my_eyes, %my_hair) File "", line 1 print "he's got %s eyes and %s hair" % (%my_eyes, %my_hair) ^ SyntaxError: invalid syntax Evaluate: my_age=35; my_weight=180; my_height=74 print "If I add %d, %d, and %d I get %d" % (my_age, my_height, my_weight, my_age + my_height + my_weight) If I add 35, 74, and 180 I get 289 Evaluate: my_age=35; my_weight=180; my_height=74 print "If I add %d, %d, and %d I get %d" % (my_age, my_height, my_weight, (my_age + my_height + my_weight)) If I add 35, 74, and 180 I get 289 Evaluate: my_age=35; my_weight=180; my_height=74 print "If I add %a, %b, and %c, I get %d" % (my_age, my_height, my_weight, (my_age + my_height + my_weight)) Traceback (most recent call last): File "", line 1, in ValueError: unsupported format character 'a' (0x61) at index 10 Evaluate: my_height = 180 print "He's %d% inches tall." % my_height error Evaluate: my_height = 180 print "He's %d% inches tall." % (my_height) error Evaluate: my_weight = 180 print "He's %d pounds heavy." % my_weight He's 180 pounds heavy. Evaluate: my_eyes = 'Blue'; my_hair = "Brown" print "He's got %s eyes and %s hair." % (my_eyes, my_hair) He's got Blue eyes and Brown hair. Evaluate: my_teeth='White'; print "His teeth are usually %s depending on the coffee." % my_teeth His teeth are usually White depending on the coffee. Evaluate: my_age = 35 my_height = 74 my_weight = 180 print "If I add %d, %d, and %d I get %d." % (my_age, my_height, my_weight, my_age + my_height + my_weight) If I add 35, 74, and 180 I get 289. Evaluate: print "he's %d feet tall" % 10.0 he's 10 feet tall Evaluate: print "he's %r feet tall" % 10.0 he's 10.0 feet tall Evaluate: print "he's %s feet tall" % 10.0 he's 10.0 feet tall # 6 Evaluate: w = "This is the left side of..." e = "a string with a right side." print w + e This is the left side of...a string with a right side. Evaluate: w = "This is the left side of..." e = "a string with a right side." print w ++ e Traceback (most recent call last): File "", line 1, in TypeError: bad operand type for unary +: 'str' Evaluate: print "This is the left side of..." + "a string with a right side." This is the left side of...a string with a right side. Evaluate: print ("This is the left side of..." + "a string with a right side.") This is the left side of...a string with a right side. Evaluate: x = "There are %d types of people." % 10 print "I said: %r." % x I said: 'There are 10 types of people.'. Evaluate: x = "There are %d types of people." % 10 print "I said: '%r'." % x I said: ''There are 10 types of people.''. Evaluate: binary = "binary" do_not = "don't" y = "Those who know %s and those who %s." % (binary, do_not) print "I also said: '%s'." % y I also said: 'Those who know binary and those who don't.'. Evaluate: hilarious = False joke_evaluation = "Isn't that joke so funny?! %r" print joke_evaluation % hilarious Isn't that joke so funny?! False # 7 Nothing new. # 8 Evaluate: formatter = "%r %r %r %r" print formatter % (1, 2, 3, 4) 1 2 3 4 Evaluate: formatter = "%r %r %r %r" print formatter % ("one", "two", "three", "four") 'one' 'two' 'three' 'four' Evaluate: formatter = "%r %r %r %r" print formatter % (True, False, False, True) True False False True Evaluate: formatter = "%r %r %r %r" print formatter % (formatter, formatter, formatter, formatter) '%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r' Evaluate: formatter = "%r %r %r %r" print formatter % ( "I had this thing.", "That you could type up right.", "But it didn't sing.", "So I said goodnight." ) 'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.' # 9