-
Notifications
You must be signed in to change notification settings - Fork 20
Control Flow
- Make sure you've done the Hello World Exercise
Control Flow is how you control the execution of your program through your code. Control flow includes
- Conditional (if, if-else, if-else-if) statements
- Loops (for, while)
- Functions
Make sure you're in the examples
directory.
Conditional statements allow us to do different actions, depending on the current state of the program (e.g. boolean condition, checking if a variable has a certain value, etc.)
Open up ifelse.py
to see how to write conditional statements:
# True and False must be capitalized
# To make variables, just declare it and
# assign it a value. The spaces around the "=" are optional.
b = True
c = False
d = 4
e = "apple"
# Example of an if-else-if statement.
# To check if two quantities are equal, use "=="
if d == 3:
print "d is 3"
elif d == 4:
food = d + 7
print "FOOD", food
elif d == 5:
print "d is 5"
else:
print "d is not 3 or 4 or 5"
# Use "==" for strings as well!
if e == "apple":
print e
else:
print "NOT APPLE"
# Use "and" and "or" to take the logical and/or of
# two boolean values.
# Note that an if statement does not need to have a
# matching "else".
if b and c:
print "b and c are both True"
if b or c:
print "at least one of b or c is True"
To run it, python ifelse.py
. You should see this output:
FOOD 11
apple
at least one of b or c is True
Loops allow us to repeat in action or to traverse a collection. Open up loops.py
to see examples of a while
and for
loop:
# Example of a while loop.
# This computes the sum of the integers 0 + 1 + ... + 99
# and stores the result in the variable `s`
n = 0
s = 0
while n < 100:
s += n
n += 1
print "s:", s
# This is an example of a for loop.
# This allows us to iterate over all the items in a collection.
# The variable `item` will take on all the values in `lst`, one per iteration.
lst = ["apple", "bar", "orange"]
for item in lst:
print item
# range() is a function that allows us to generate a sequence
# of integers in a certain range. range(100) will generate the sequence:
# 0, 1, 2, ... 99
#
# To see the documentation for range(), open up the python top-level and type:
#
# help(range)
s2 = 0
for n in range(100):
s2 += n
print "s2:", s
To run it, python loops.py
. You should see the following output:
s: 4950
apple
bar
orange
s2: 4950
Functions allow us to abstract away common pieces of code so that the code is written once, but is able to be used many times.
Open up functions.py
to see examples of how to declare and call functions.
# To declare a function, use the `def` keyword,
# followed by the function name, and then arguments
# in parentheses.
def add(x, y):
return x + y
print "add(5,4)", add(5,4)
print "add(3,6)", add(3,6)
# A function does not need to return anything. In this
# case, the special value None will be the return value.
#
# A function can also have default values for arguments
# if those values are not provided when the function
# is called.
def repeat(s, prefix="Hi", n=5):
for i in range(n):
print i, prefix, s
repeat("cat", "Hello", 3) # prefix == "Hello", n == 3
repeat("cat", prefix="Bye") # prefix == "Bye", n == 5
repeat("dog", prefix="Hi", n=3) # prefix == "Hi", n == 3
repeat("food") # prefix == "Hi", n == 5
def sum_range(low, high):
"""
This will sum up all the integers in the half-open
interval [low, high).
Triple quotes are special comments that you can place
inside a function. These comments serve as the
documentation string for this function (what you
see when type help(function_name) at the top level.
"""
# range(low, high) will generate a sequence of all the
# integers in the range [low, high)
#
# sum is a built-in function that will sum all the numbers
# in a given sequence.
return sum(range(low, high))
print "sum_range(10,20)", sum_range(10, 20)