9:30 - 10:00
10:00 - 10:45
10:45 - 11:00
11:00 - 12:15
cd..
versus cd ..
cd
is a "verb" that says "change directories" and ..
is a specific location, i.e. "one level up from here"file_names_like_this.txt
my_dictionary["firstname"]
versus my_dictionary ["firstname"]
"
) or single quotes ('
) as you like1: Has anyone seen Fred's Computer?
2: "These quotation marks are driving me crazy!" said Fred.
Post it to the etherpad -- you have two minutes!
# Solution
print("Has anyone seen Fred's Computer?")
print('"These quotation marks are driving me crazy!" said Fred.')
Trial, error, and good record-keeping. * stackoverflow.com * Code snippets tool * Make your own "retained wisdom" documents
Python is a very forgiving language. Except when it isn't.
example Java variable declaration:
int my_height_in_inches = 73;
Python is much more friendly. It simply tries to guess what variable type you want from the context.
my_height_in_inches = 73 # python will guess that you want an integer my_height_in_inches = 73.1 # python will guess you want a floating point number
type | decimal value | binary representation -----|---------------|---------------------- int | 73 | 1001001 (7 bits) float | 73.1 | 01000000 01010010 00000110 01100110 01100110 01100110 01100110 01100110 (64 bits)
A person can convert 73 to 73.1 by writing ".1" on the end of the number, but for the computer this is a whole different ballgame
x = 4/3
print("The value of x is:", x)
print("The type of x is:", type(x))
x_coerced_to_float = 4/3.0
x_coerced_to_float = float(4) / 3
print("The value of x_coerced_to_float is:", x_coerced_to_float)
print("The type of x_coerced_to_float is:", type(x_coerced_to_float))
# Quick Exercise: what is the result of this? Why?
y = float( 4 / 3)
print(y)
True
False
None
Spelled and capitalized as indicated
Some good places to practice:
# for loop
for i in range(10):
print i,
# while loop
i = 0
while (i < 10):
print i,
i = i + 1
There are other types of loops, but for
and while
loops can handle nearly every circumstance where you need to operate of a sequence of numbers.
for
loop versus while
loop?2 minutes to think about it - then discuss and/or ask questions
Write a for
loop that prints the even numbers up to and including 24
This is a common type of problem. The game plan:
div_by_3_list = []
counter = 0
desired_quantity_of_multiples = 20
while(len(div_by_3_list) < desired_quantity_of_multiples):
counter += 1 # this is the same as saying counter = counter + 1
if counter % 3 == 0:
# we have found a number divisible by three
div_by_3_list.append(counter)
print div_by_3_list
One way to start this off could be:
for t in div_by_3_list:
...do something...
10 minutes: work as a group
for t in div_by_3_list:
if t % 9 == 0:
print t, " is divisible by nine."
things_I_like = ['Sushi', 'Risotto', 'Grapefruit', 'Pez']
things_I_do_not_like = ['Marshmallow Peeps', 'Avocado', 'Tripe', 'Haggis']
things_on_the_menu = ['Sushi', 'Beef Wellington', 'Mascarpone', 'Tripe', 'Pez']
5 minutes, work with yor table
for thing in things_on_the_menu:
print "Would you like some", thing, "? ",
if thing in things_I_like:
print "Yes indeed."
elif thing in things_I_do_not_like:
print "No thanks."
else:
print "Hmm, let me think about that"
things_I_like = ['Sushi', 'Risotto', 'Grapefruit', 'Pez']
favorite_food_score = [8, 10, 7, 11]
# dict(zip(list_of_keys, list_of_values)) makes a dictionary of key:value pairs
favorites_dict = dict(zip(things_I_like, favorite_food_score))
favorites_dict
favorites_dict.keys()
favorites_dict.values()
for k in favorites_dict.keys():
print "My score for", k, "is:", favorites_dict[k]
##... about that get() method
what_about = "Pop-Tarts"
favorites_dict[what_about]
favorites_dict.get(what_about)
some_dictionary[some_key]
will throw a KeyError if some_key is not in that dictionary's keyssome_dictionary.get(some_key)
will return None
instead# This works:
if favorites_dict.get("Tilapia") is None:
print "Have you ever tried Tilapia?"
# This does not work:
if favorites_dict["Tilapia"] is None:
print "Have you ever tried Tilapia?"
\[ f(x) = x^2\]
\(f\) is the function name
\(x\) is the argument
\(x^2\) is the value that is returned by the function
def f(x):
"""
Calculate the square of the argument and return that value.
"""
calculated_value = x * x
return calculated_value
f(3)
def
statement says that you are about to define a new functionf
is the function name
calc_square
x
is the argument
"""Calculate the square of the argument and return that value."""
is the docstring for the function.
calculated_value
is a variable that exists only in the context of the function
calculated_value
is the function, fprint(calculated_vaulue)
def my_product(x, y):
"""
Return the product of two numbers
"""
return x + y
Is
my_product
going to work?
my_product(2, 2)
my_product(0, 0)
my_product(3, 3)
assert my_product(3, 3) == 9, "Three times Three is Nine."
assert
something that is either True or False, "Message to print if False"
assert
statements to test the functionassert
statements first!
unittest
moduleassert my_func(0) == 0
assert my_func(1) == 0
assert my_func(2) == 3
assert my_func(3) == 5
assert my_func(4) == 7
# This solution works
def my_func(y):
"""Return 2y + 1 if y > 1, otherwise return 0."""
ret_val = 0
if y > 1:
ret_val = 2 * y - 1
return ret_val
assert my_func(0) == 0
assert my_func(1) == 0
assert my_func(2) == 3
assert my_func(3) == 5
assert my_func(4) == 7