Download Session 9 - Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Science of
Graphics and Games
MONT 105S, Spring 2009
Lecture 9
For Loops
1
Recall Lists and Strings
A string is a sequential collection of characters
"house"
"Monty Python"
"The rain in spain"
A list is a sequential collection of python objects.
[1, 5, 7]
["dog", "cat", "bird"] [3.7, "spam", 46]
[]
We can access an element of a string or a list by its position:
Example:
>>>inventory = ["Axe", "gold coins", "torch"]
>>>inventory[2]
"torch"
>>>myWord = "house"
>>>myWord[4]
"e"
2
Accessing elements of a list or
string with a while loop
#Write a program to print out each character of a string:
answer = raw_input("Please enter a word or phrase: ")
count = 0
while count < len(answer):
print answer[count]
count = count + 1
print "Done!"
#What is the output if the user enters "apple"?
3
for loops
A for loop allows us to perform an operation on each item in
a list (or for each character in a string).
Example:
myString = "apple"
for i in myString:
print i
print "done"
Output:
a
p
p
l
e
done
4
General form of a for loop
The general for of a for loop in python is:
for myVar in myList:
Python code
Execution of a for loop:
1) Assign the first item of the list (index 0) to myVar
2) Execute the body of the loop
3) Assign the second item of the list (index 1) to myVar
4) Execute the body of the loop
5) Repeat for each consecutive item in the list (in order).
5
Another for loop example
# Use a for loop to add the numbers in a list:
total = 0
prices = [2.75, 1.22, 4.16]
for x in prices:
total = total + x
print total
# What is the output?
# We will work through the execution in class.
6
Generating a list of numbers
•We often want to perform an operation some number of times, or use
sequential numbers in our loop.
•To do this, we can use a for loop with a list of integers.
•We can generate the list of integers using the range( ) function.
range(N)
range(start, stop)
range(start, stop, step)
Examples:
>>> range(8)
[0, 1, 2, 3, 4, 5, 6, 7]
>>> range(4, 10)
[4, 5, 6, 7, 8, 9]
>>> range(3, 15, 2)
[3, 5, 7, 9, 11, 13]
# Generates a list from 0 to N-1
#Generates a list from start to stop - 1
#Generates a list from start to stop - 1
#with step size given by step.
7
Examples
Example 1:
sum = 0
for i in range(1, 5):
sum = sum + i
print sum
# What is the output?
Example 2:
for i in range(5):
print "Hurray!"
8
Writing a while loop as a for
loop
A while loop that is controlled by a counter can be written as
a for loop.
Example:
count = 0
while count < 5:
print "Count #", count
count = count + 1
Equivalent for loop:
for count in range(5):
print "Count #", count
9
Changing Values in a List
Suppose we want to step through a list of numeric values
and add one to each number in the list.
Wrong way:
myList = [14, 7, 6, 9]
for x in myList:
x=x+1
print myList
#What went wrong?
10
Changing Values in a List
Suppose we want to step through a list of numeric values
and add one to each number in the list.
Right way:
myList = [14, 7, 6, 9]
for x in range(4):
myList[x] = myList[x] + 1
print myList
#We will analyze this loop in class.
11