Introduction To Python: Chris Piech and Mehran Sahami CS106A, Stanford University
Introduction To Python: Chris Piech and Mehran Sahami CS106A, Stanford University
"""
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!")
This is on a PC.
On Macs: python3 helloworld.py
Our First Python Program
You’re now all Python
programmers!
hey_that_looks_
like_what_I_
taught_them()
x 10
x 10
5
x 12
5
x 12
num_students 700
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
string
num1 "9"
num1
int
9
10
3.5
x = 10
Piech and Sahami, CS106A, Stanford University
You just wrote your first
Python program and learned
about variables!
1. Introduction to Python
2. Understanding variables