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