Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

ENGR 101

Introduction to
Programming
Week 4
Simple Repetition

for i in range(4):
print ‘Hello’
• Use a for loop to draw the square.
for loop – syntax

for i in range(start=0, stop, step=1):


print i
for loop – different flavors
for i in range(start=0, stop, step=1):
# Three parameters
# One parameter
for i in range(4, 10, 2):
for i in range(3):
print i
print i
4
0
6
1
8
2
# Going backwards
# Two parameters
for i in range(0, -8, -2):
for my_i in range(3, 6):
print i
print my_i
0
3
-2
4
-4
5
-6
for loop – caveat

• For the range function:


• All parameters have to be integers.
• Negative numbers are ok.
• Loop variable naming  the same rules as in
regular variables
TurtleWorld
• With for loop

from swampy.TurtleWorld import *


world = TurtleWorld()
bob = Turtle()
print bob

for i in range(4):
fd(bob,100)
lt(bob)

wait_for_user()
Exercises

• Work on Exercises in Section 4.3 (All of them)


Ex 1
• put your square-drawing code into a function
definition.
• call the function, passing the turtle as a parameter.
# draw a square of size 100
def square(t):
for i in range(4):
fd(t, 100)
lt(t)

square(bob)
Encapsulation
• Encapsulation?
• Wrapping a piece of code up in a function
• Benefits?
• The same motivation to use any function!
• You re-use the code. It is more concise to call a
function twice than to copy and paste the body!
• Easy maintenance: Just need to modify the function
definition. Function calls will directly be affected by
the modification.
Encapsulation

# encapsulation demo
ray = Turtle()
square(ray)
Ex 2

• add a length parameter to square function.


# draw a square of a given length
def square(t, length):
for i in range(4):
fd(t, length)
lt(t)

square(bob, 100)
Generalization
• Generalization?
• Adding a new parameter to a function
• Benefits?
• It makes the function more general:
• in the previous version, the square is always
the same size;
• in this version, it can be any size.
Generalization
Next step: instead of drawing squares, draw regular
polygons with any number of sides.

# draw a polygon with n edges of a given size


def polygon(t, n, length):
angle = 360.0 / n
for i in range(n):
fd(t, length)
lt(t, angle)

# This draws a 7-sided polygon with side length 70.


polygon(bob, 7, 70)
Generalization

• If you have more than a few numeric arguments, it


is easy to forget what they are, or what order they
should be in.
• It is legal, and sometimes helpful, to include the
names of the parameters in the argument list:

polygon(bob, length=70, n=7)


Revisiting Boolean
Boolean Type - bool

• Has two possible values: True or False


>>> a = True
>>> print type(a)
<type, ‘bool’>
Boolean Expressions
• True and False are special values that belong to
the type bool; they are not strings:

>>> type(True)
<type 'bool'>

>>> type(False)
<type 'bool'>
Relational Operators
• Relational operators are:
x == y # x is equal to y
x != y # x is not equal to y
x > y # x is greater than y
x < y # x is less than y
x >= y # x is greater than or equal to y
x <= y # x is less than or equal to y

• A common error is to use a single equal sign (=)


instead of a double equal sign (==). Remember that
= is an assignment operator and == is a relational
operator. There is no such thing as =< or =>.
Logical Operators

• There are three logical operators: and, or, and not.


• The semantics (meaning) of these operators is
similar to their meaning in English.
Logical Operators
x > 0 and x < 10
# is true only if x is greater than 0
# and less than 10.
x > 0 or x < 10
# is true if either of the conditions
# is true, that is, if x is greater than 0
# or less than 10.
not (x > y)
# is true if x > y is false, that is,
# if x is less than or equal to y.
Iteration (Looping)
Another Way
while loop – syntax
# initialize a counter variable
counter_variable = 0
while boolean_expression:
#do something in the loop body
print i

#update the counter variable


counter_variable += 2

boolean_expression should involve the counter variable


The while statement
• More formally, here is the flow of execution for a
while statement:

1. Evaluate the condition, yielding True or False.

2. If the condition is false, exit the while statement


and continue execution at the next statement.

3. If the condition is true, execute the body and


then go back to step 1.
Looping another way - while
option #1 option #2
with for loop with while loop
i = 0
for i in range(4):
while i < 4:
fd(bob,100)
i = i + 1
lt(bob)
fd(bob,100)
lt(bob)
Looping another way - while
Example Example
with for loop with while loop

for i in range(4, 8, 2): i = 4


print i while i < 8:
print i
i = i + 2
The while statement

• Another example:
# implementation
def countdown(n):
while n > 0:
print n
n = n-1
print 'Blastoff'
The while statement

• This type of flow is called a loop because the last step


loops back around to the top.
• The body of the loop should change the value of one or
more variables so that eventually the condition becomes
false and the loop terminates.
• Otherwise the loop will repeat forever, which is called an
infinite loop.
Sum numbers

• Write a function that will take two integers a and b,


and sum the numbers between a and b (including
endpoints)!
• Try it with both for and while loops!

You might also like