For Loop
Sometimes you need to do something
for a number of times.
Say when executing load and stress
testing of a software product.
Instead of actually loading values to the
system 1000 times, we can automate
those steps.
There are several ways to repeat things
in Python,
o the most common of which is the for
loop.
for Variable_name in range( numberOfTimesToRepeat ):
statements to be repeated
e.g.
The following program will print Hello 4
times:
The word for must be in lowercase,
First line must end with a colon:
Repeated statements must be indented.
Indentation is used to tell Python which
statements will be repeated.
Example
Program to ask the user to enter a
number,
Prints the square of that number.
It does this 3 times.
Then prints loop is done.
Since the second and third lines are
indented, Python knows that these are
the statements to be repeated.
The fourth line is not indented, so it is
not part of the loop and only gets
executed once, after the loop has
completed.
Challenge 1
Write a program that prints A followed by B
followed by C and D alternated 3 times
before printing E as follows:
Solution
Challenge 2
Write a program that prints the following
output.
Solution
The loop variable
The loop variable is a little bit tricky.
When the loop first starts, Python sets
the variable i to 0.
Each time we loop back up, Python
increases the value of i by 1.
The program loops 10 times, each time
increasing the value of i by 1, until we
have looped 10 times.
At this point the value of i is 9.
We can use the loop variable to keep
track of where we are. e.g.
The range() function
The value we put in the range function
determines how many times we will
loop.
The way range works is it produces a list
of numbers from zero to the value
minus one.
E.g. range(5) produces: 0, 1, 2, 3, and 4.
If we want the list of values to start at a
value other than 0, we can do that by
specifying the starting value.
E.g. range(1,5) will produce the list 1, 2,
3, 4.
If we wanted the list to contain the
numbers 1 through 5 (including 5), then
we would have to do range(1,6).
3rd Argument of the range() function
Values can go up by more than one at a
time.
To do this, we can specify a 3rd
argument.
range(1,10,2) 1 – 9, stepping by twos,
producing 1, 3, 5, 7, 9.
Backward stepping in range() function
To get values going backwards, we can use
a negative sign.
E.g. to get 5, 4, 3, 2, Use range(5,1,-1).
(NB the range function stops one short of
the ending value 1).
Example 2
Program prints from 5 down 0 + 1 i.e.
5 to 1.
The end=’’ function makes
arguments to be printed in the same
line.
Tasks
1. Write a program that uses a for loop to
print the numbers 8, 11, 14, 17, 20, . . .
, 83, 86, 89.
2. Write a program that uses a for loop to
print the numbers 100, 98, 96, . . . , 4,
2.
Modules
What is a module?
A module in python a separate file (.py).
It comprises of code that is executing a
particular task.
This file can be called/used through
“from module import Class or Method”
statement, into the core python main
file or main program.
The core Python language consists of
things like for loops, if statements,
operators, and some functions like
print(), input() etc.
Everything else is contained in modules
math module
Contains familiar math functions,
o Such as sin, cos, tan, exp, log, log10,
factorial, sqrt, floor, ceil etc.
Also contains constants pi and e.
Built-in Mathematics functions (Non math
module)
Two built in math functions, that are
available in python without importing
the math module;
o abs (absolute value) and
o round
The round function takes two
arguments:
i. number to be rounded,
ii. number of decimal places.
random module
Allows us to use random numbers in our
programs.
E.g.
randint(a,b) will return a random
integer between a and b including both
a and b. Unlike the range function.
Task
1. Write a program that takes radius of
a circle and calculates the area of that
circle.
2. Write a program that generates and
prints 50 random integers, each
between 3 and 6.
3. Write a program that generates a
random number, x, between 1 and 50, a
random number y between 2 and 5, and
computes x y.
4. Write a program that generates a
random number between 1 and 10 and
prints your name that many times.