W3 Python Lecture Slides
W3 Python Lecture Slides
call_me()
You had me at
turn_left()
"""
To run a race that is 9 avenues long, we need to move
forward or jump hurdles 8 times.
"""
def main():
for i in range(8): Decomposition principle:
if front_is_clear(): Consistent Each function should solve
move()
else: indentation one step of problem
jump_hurdle()
"""
Pre-condition: Facing East at bottom of hurdle
Post-condition: Facing East at bottom in next avenue after hurdle
"""
def jump_hurdle(): Descriptive names
ascend_hurdle()
move() Short functions (snake_case)
descend_hurdle() (usually 1-15 lines)
Piech and Sahami, CS106A, Stanford University
What’s Mozart Doing Now?
if mehran_teaching():
not_funny()
while mehran_teaching():
not_funny()
1. Introduction to Python
2. Understanding variables
def main():
print("hello, world!")
hey_that_looks_
like_what_I_
taught_them()
x 10
x 10
5
x 12
5
x 12
num_students 700
Python handles the
num_in_class = 550
baggage for you!
num_in_class 550
num_absent = num_students – num_in_class
num_absent 150
Piech and Sahami, CS106A, Stanford University
Types
• Each suitcase knows what type of information it carries
num_students = 700
num_students
int
700
float
num_students 700.0
string
num1 "9"
num1
int
9
10
3.5
x = 10
Piech and Sahami, CS106A, Stanford University
Multiple values in print
• You can also print multiple items separating them
with commas
– By default, a space is printed between each item
def main():
x = 4
y = 0.2
print(x, y)
print("x =", x, "and y =", y)
4 0.2
x = 4 and y = 0.2
1. Introduction to Python
2. Understanding variables