We'll cover a bit of the first two tonight, leaving data structures for next week.
.py) with variables, callable functions and other reusable code.import modules to (re)use the code in our own programs.Libraries (and modules) come in three flavors:
pip or conda to download and install themimport turtle # use the turtle library
wn = turtle.Screen() # creates a graphics window
alex = turtle.Turtle() # create a turtle named alex
alex.forward(150) # tell alex to go forward 150 units
alex.left(90) # tell alex to turn by 90 degrees
alex.forward(75) # continues on (upward) ...

A class is a data type that defines ...
Each data type in Python (even built-in ones like int or float) is actually a class.
An object is an instance of a class (data type)
Each data value in Python is actually an object.
To invoke (or call) a method of an object (literal value or variable reference), we tack on a dot (period) followed by the method call. The method calls always know what objects they are attached to.
<object>.<method call>
# Examples with string objects (instances)
"ZXYW".lower() # method call on a string literal
alpha="abcd" # set a string variable
alpha.upper() # method call on string variable
# Examples with string objects (instances)
print("ZXYW".lower()) # method call on a string literal
alpha="abcd" # set a string variable
print(alpha.upper()) # method call on string variable
zxyw ABCD
import turtle
wn = turtle.Screen()
alex = turtle.Turtle()
alex.forward(150)
alex.left(90)
alex.forward(75)
Functions are also objects. You can think of a function like an object with a single method that is executed every time the function is called. (There's more to it than that, of course ...)
# the following does not run without some work (not shown)
# but illustrates the "everything is an object" principle
add(1,2) # function call to add two numbers
1.add(2) # method call to add one number to another
# note that the number 1 is an object in Python
A module is a .py file that contains a number of reusable Python definitions and statements
We use import statements to integrate modules into our code. We then use dot notation to refer to any classes or functions defined by the module.
A function definition encapsulates (Google that!) a block of code into a form that can be called by other code
def <name>(<parameters>):
<statements>
def indicates the start of a function definition <name> is the name of the function<parameters> is a comma-delimited list of input variables <statements> comprise the body of the functionTake note: punctuation like indentation, spaces,:, ( and ) matter here. Don't ignore them!
def add_two_numbers(x,y):
z=x+y
return z
add_two_numbers(1,2)
add_two_numbersx and y act like variables within the bodyz is undefined outside of the function bodyreturn statement completes the function call and (optionally) provides a valuedef add_two_numbers(x,y):
z=x+y
return z
add_two_numbers(1,2)
3
A block of code is just a series of statements that are run in the order given.
gashouse_gang = ["Evers","Tinker","Chance"] # A block
for player in gashouse_gang: # with two statements
print(player) # A new block nested within
# the for statement
Evers Tinker Chance
x=1 # three statements
y=2 # in the same
print(x+y) # block
print(x+y) # another block
File "<ipython-input-3-91f4d40a6bfe>", line 4 print(x+y) # another block ^ IndentationError: unexpected indent
Why do you suppose we get this error?
Because indentation is part of the logic. It's illogical to start a new block for no reason. It needs to nest logically inside another statement.
Conditional execution allows a block to run only when given conditions are met.
Typical form is binary selection
if <boolean expression>:
<True block>
else:
<False block>
x=1
y=2
if x>y:
print("x is bigger than y")
else:
print("x is not bigger than y")
x=1
y=2
if x>y:
print("x is bigger than y")
else:
print("x is not bigger than y")
x is not bigger than y
So what counts as a boolean expression?
x == y (is x equal to y?)x > y (is x greater than y?)x >= y (is x greater than or equal to y?)and, or, not, and () operators):((x == 1) or ((x > 10) and not (x == 15))x = 8
y = (x == 1) or ((x > 10) and not (x == 15))
print(y)
x = 8
y = (x == 1) or ((x > 10) and not (x == 15))
print(y)
False
Try other values of x. What values cause the expression to evaluate to True?
A loop repeats a block called the body over and over again until a termination condition is met.
for loop is great for iterating over a set of itemswhile loop Write a for loop that tests each value of x between 0 and 16. For each value tested, print the value and whether (x == 1) or ((x > 10) and not (x == 15)) is true or false.
for x in range(17):
y = (x == 1) or ((x > 10) and not (x == 15))
print(x, y)
0 False 1 True 2 False 3 False 4 False 5 False 6 False 7 False 8 False 9 False 10 False 11 True 12 True 13 True 14 True 15 False 16 True
while Loops¶while <boolean>:
<statements>
<boolean> is a condition to check at the start of each pass through the loop<statements> in the body are only executed when the condition is true; if the condition is false then execution skips to the statement immediately after the loop body

Anything you can do with a for loop, you can also do with a while loop (though in a slightly more verbose way) using an accumulator variable.
# The example for loop (again)
# for x in range(17):
# y = (x == 1) or ((x > 10) and not (x == 15))
# print(x, y)
# --------------------------------------------------------
# The equivalent while loop
#
x = 0 # initialize our accumulator x
while x < 17:
y = (x == 1) or ((x > 10) and not (x == 15))
print(x, y)
x = x + 1 # increment the accumulator each
# pass through the loop
0 False 1 True 2 False 3 False 4 False 5 False 6 False 7 False 8 False 9 False 10 False 11 True 12 True 13 True 14 True 15 False 16 True
Use a while loop to find and print out the values of x (up to 16) such that (x == 1) or ((x > 10) and not (x == 15)) is true. Do not print out the values where it is false.
x=0
while x <= 16:
if (x == 1) or ((x > 10) and not (x == 15)):
print(x)
x = x + 1
1 11 12 13 14 16
We forgot to modify the accumulator variable x with every pass through the loop!
The test x<10 never returns False, so the loop never ends. This can actually crash your computer if the loop is part of your operating system code.
The following is due before class next week:
Please email chuntley@fairfield.edu if you have any problems or questions.