## Extract from Chapter 3 Hello, Little Turtles! about for loops

• The for Loop
• iteration is one of the basic control structures
• control structures determine which statements are executed and when
• the default structure is the sequence (steps in order, all executed)
• the loop changes that
• syntax: for loopvariablename in list :
• followed by indented statements which will be repeated = body
• Flow of Execution of the for Loop (the semantics of the statement)
• see flowchart in textbook
• while the list given is not empty
• assign loopvariablename to next thing in the list
• do all the steps in the body with that value
• go back to the top of the loop and get the next item from the list
• when list becomes empty, get out of the loop and continue with following statement
• Note that this says that a loop may NOT be executed at ALL, if the list that controls it is empty!
• Iteration Simplifies our graphics Program
```from graphics import *

def main():
wn = GraphWin()
x = 30
y = 40
for i in ['green','red','blue','white','black']:
x = x + 10
y = y + 20
tt = Rectangle(Point(x,y),Point(x+10,y+10))
tt.setFill(i)
tt.draw(wn)
wn.getMouse()
wn.getMouse()
wn.close()

main()
```
• The range Function
• a list of integers in order is so common and so useful, range function creates exactly that
• has 3 forms, all useful
• first syntax: range(integer)
• semantics: generates a list starting at 0, going up to but NOT including the ending integer given
• second syntax: range(integer, integer)
• semantics: generates a list starting at first integer, going up to but NOT including the second integer given
• third syntax: range(integer, integer, integer)
• semantics: starts list at first integer, goes up to but NOT including the second integer, using third integer as the step size between values
• Examples of for loops
• printing a column of stars
```               for i in range(5):
print("*")  # each print gives a newline character at the end
```
• repeating interactions
```		for i in range(3):
n = int(input("Enter a number "))
print (n*3,"is three times as much as",n)
```
• Uses for loops
• used very often with numbers
• count entries or accumulate some numbers
• counters
• must be initialized outside of the loop
• must be changed inside the loop
• statement looks like "ctr = ctr + 1" or "ctr += 1"
• can be change by any amount, any mathematical operator
• example
```			ctr = 3
for i in range(5):
ctr = ctr + 3
```
• results in ctr being 18
• accumulators - used to find totals
• must be initialized outside of the loop
• must be changed inside the loop
• statement looks like "total = total + num" or "total += num"
• can be change by any amount, any mathematical operator
```			tot = 0
for i in range(5):
tot = tot + i
```
• results in tot being 10
• Factorial (written as an exclamation point !)
• this is a mathematical idea which involves a product (multiplcation)
• 1! = 0, 0! = 0
• n! is 1 * 2 * 3 * ... * n
• 5! = 1 * 2 * 3 * 4 * 5 = 120
• ```			fct = 1
n = int(input("Factorial of what number? "))
for i in range(2,n+1):
fct = fct * i
```
• fct is the factorial
• note that the loop will not run at all if n is 1 or 0
• A PROBLEM with Objects
• to make an object, you should use one of the constructor methods, like "Line()"
• if you just assign a name to an object, you do not create a new one
• example:
```		tom = Line()
joe = tom

```

This only creates ONE object, which has TWO names! the problem is called "aliasing". The same object has two names. Changing joe changes tom too!
• You must use the constructor twice to have two objects
• example:
```		tom = Line()
joe = Line()
```
• or use the clone method