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

Readings

Motivation
Looping
While loop
For loops
Mechanics
Conclusion
Writing loops
Working with strings

Loops
Basic algorithms

Michael Burrell

September 19, 2023

Michael Burrell Loops


Readings
Motivation
Looping
While loop
For loops
Mechanics
Conclusion
Writing loops
Working with strings

Readings

Chapter 2 — 2.6
Chapter 3 — 3.4, 3.5

Michael Burrell Loops


Readings
Motivation
Looping
While loop
For loops
Mechanics
Conclusion
Writing loops
Working with strings

Motivation for loops

We have variables, if-statements, basic logic. There are still


some things we don’t know how to do yet:

Michael Burrell Loops


Readings
Motivation
Looping
While loop
For loops
Mechanics
Conclusion
Writing loops
Working with strings

Motivation for loops

We have variables, if-statements, basic logic. There are still


some things we don’t know how to do yet:
Reverse a string (e.g., turn hello into olleh)
Capitalize alternating letters in a string (e.g., turn hello
into HeLlO)
Compute a summation (e.g., 1 + 3 + 5 + 7 + . . . + n)
Print something out n times

Michael Burrell Loops


Readings
Motivation
Looping
While loop
For loops
Mechanics
Conclusion
Writing loops
Working with strings

Looping

Most useful code involves repetition of some sort


We don’t want to do exactly the same thing through each
repetition, but following some pattern
This repetition should be controlled in some way (and
stop repeating at some point)

Michael Burrell Loops


Readings
Motivation
Looping
While loop
For loops
Mechanics
Conclusion
Writing loops
Working with strings

While loop

while condition:
statements

Michael Burrell Loops


Readings
Motivation
Looping
While loop
For loops
Mechanics
Conclusion
Writing loops
Working with strings

While loop

while condition:
statements
We repeatedly execute the statements in the body of the
loop as long as the condition is true
If the condition is always true, it’s an infinite loop
If the condition is initially false, we do not enter the loop
at all

Michael Burrell Loops


Readings
Motivation
Looping
While loop
For loops
Mechanics
Conclusion
Writing loops
Working with strings

Mechanics of looping

Example loop
The best way to understand how while loops work is by
tracing through an example.
1 n = int(input(’Enter a number: ’))
2 i = 1
3 while i <= n:
4 print(i)
5 i += 1

Michael Burrell Loops


Readings
Motivation
Looping
While loop
For loops
Mechanics
Conclusion
Writing loops
Working with strings

Writing loops

Learning to write code with loops takes practice


Beginner students often ask me for a step-by-step guide
on how to write code
Such a step-by-step guide does not (and cannot exist)
But I have come up with a guide which sort of works for
rank beginners

Michael Burrell Loops


Readings
Motivation
Looping
While loop
For loops
Mechanics
Conclusion
Writing loops
Working with strings

Step 1: Identify the


repeated operations
Summing integers In this case,
Problem: Read in a positive integer n addition (the +
and sum up all the integers up to n. sign) is repeated.
E.g., if the user enters 4, calculate The bigger the
1 + 2 + 3 + 4 = 10. input, the more
additions we will
have to perform.

Michael Burrell Loops


Readings
Motivation
Looping
While loop
For loops
Mechanics
Conclusion
Writing loops
Working with strings

Step 2: Make a loop


with a looping
variable
Summing integers
After you’ve
Problem: Read in a positive integer n identified a repeated
and sum up all the integers up to n. operation, put that
1 total = 0 operating in a loop
2 i = ....
3 while i .... :
Identify the parts
4 total += ..... that will change
5 i += .... each time in the
loop
Some details can be
left out
Michael Burrell Loops
Readings
Motivation
Looping
While loop
For loops
Mechanics
Conclusion
Writing loops
Working with strings

Step 3: How does i


Summing integers relate to the repeated
Problem: Read in a positive integer n operation?
and sum up all the integers up to n. The repeated
1 total = 0 operation usually
2 i = .... depends on i
3 while i .... :
somehow
4 total += i
5 i += .... In this case, we are
just adding i itself

Michael Burrell Loops


Readings
Motivation
Looping
While loop
For loops
Mechanics
Conclusion
Writing loops
Working with strings

Summing integers Step 4: Finish details


for looping variable
Problem: Read in a positive integer n
and sum up all the integers up to n. 1 Initial value for i
1 total = 0
2 How does i change
2 i = 1 each time through
3 while i <= n: the loop?
4 total += i 3 When we we
5 i += 1
continue looping?

Michael Burrell Loops


Readings
Motivation
Looping
While loop
For loops
Mechanics
Conclusion
Writing loops
Working with strings

Working with strings

To access a particular character from a string, we can use


square-bracket notation in Python
E.g., if a is the string hello, then a[2] is the character l
When we use square-bracket notation, we start counting
at 0 (i.e., a[0] is h)
We can use the len function to find the length of a string

Michael Burrell Loops


Readings
Motivation
Looping
While loop
For loops
Mechanics
Conclusion
Writing loops
Working with strings

Exercises
Let’s write up some Python code which will:
1 Print exponentially increasing numbers
(1, 2, 4, 8, 16, 32, 64, . . . )
2 Determine if a number is a perfect square or not (not
using sqrt)
3 Print every positive integer less than n which is divisible
by 2 or 3
4 Print every other character from a string
5 Reduce a fraction. E.g., 24 60
becomes 25
6 Prints out all integers between an integer n and the
nearest multiple of 10. E.g., 57 will print out 57 58 59
60. E.g., 83 will print out 83 82 81 80
Michael Burrell Loops
Looping
For loops Counting loops
Conclusion

Counting loops

1 for i in range(10):
2 print(i)

This loop will print out the numbers 0 to 9


By default, we start counting at 0
We count up to but not including the number given

Michael Burrell Loops


Looping
For loops Counting loops
Conclusion

Counting loops

1 for i in range(5, 10):


2 print(i)

This loop will print out the numbers 5 to 9


You can change the number that you start counting at

Michael Burrell Loops


Looping
For loops Counting loops
Conclusion

Definite loops

Your textbook calls the use of for+range as a “definite


loop”
It is a common pattern in programming when an action
needs to be repeated a certain number of times
And you know before the loop starts how many times it
will need to be repeated

Michael Burrell Loops


Looping
For loops Counting loops
Conclusion

Exercises
Let’s write up some Python code which will:
1 Determine whether a number is prime
2 Print out all primes up to 1000
3 Draw a rectangle on the screen using |, - and +
characters

Michael Burrell Loops


Looping
For loops
Conclusion

Conclusion

We’ve seen two loops: while and for (only used with
range for now)
These allow us to repeat statements
We will require a lot of practice to get familiar with
common patterns with these

Michael Burrell Loops

You might also like