You are on page 1of 30

Introduction to Computers

Lecture

Python Programming 3

Dr. Mazen Nabil Elagamy


Objectives

• The student should learn to write more advanced Python


code.

• Learn to iterate over a sequence of elements using the


different variations of for loop.

• Learn to iterate over a sequence of elements using the


different variations of while loop in Python

Dr. Mazen Nabil Elagamy


Introduction to Repetition
Structures
• Often have to write code that performs the
same task multiple times
– Disadvantages to duplicating code
• Makes program large
• Time consuming
• May need to be corrected in many places
• Repetition structure: makes computer
repeat included code as necessary
– Includes condition-controlled loops and count-
controlled loops
Dr. Mazen Nabil Elagamy
Elements of a Program
For…. Loops

• The for statement


allows us to iterate
through a sequence of
values.
• for <var> in
<sequence>:
<body>

Dr. Mazen Nabil Elagamy


Elements of a Program
For…. Loops
The for loop in Python is used to iterate over
for i in range(5): a sequence.
print(i) Syntax of for loop
for val in sequence:
0 Body of for
1
2 Here, val is the variable that takes the value
3 of the item inside the sequence on each
4 iteration.
>>> Loop continues until we reach the last item in
the sequence
We can generate a sequence of numbers using
range() function. range(5) will generate numbers
from 0 to 4 (5 numbers).

Dr. Mazen Nabil Elagamy


Elements of a Program
For…. Loops

Dr. Mazen Nabil Elagamy


Elements of a Program
For…. Loops
count=1
for count in range(1,5):
print(count) We can also define the start,
stop and step size
1 as range(start,stop,step
2 size). step size defaults to 1
3 if not provided. Loop
4 continues until we reach the
>>> last item in the sequence.
Elements of a Program
For…. Loops
sum=0
count=1
for count in range(1,5):
sum=sum+count
print(count,sum)

1 1
2 3
3 6
4 10
>>>

Dr. Mazen Nabil Elagamy


Example
Average Payment Problem
• A small company wishes to determine the
average salary it pays to its employees for
a specific month. The number of
employees and individual salaries can
vary each month. Write a python program
that determines the average salary for the
month of Februaury.

Dr. Mazen Nabil Elagamy


Elements of a Program
For…. Loops

• Input the count of the employees, n


• Initialize sum to 0
• Loop n times
– Input a salary, x
– Add x to sum
• Output average as sum/n

Dr. Mazen Nabil Elagamy


Elements of a Program
For…. Loops
# average1.py
# A program to average a set of numbers
# Illustrates counted loop with accumulator

n = eval(input("How many employees do you have?


"))
sum = 0.0
for i in range(n):
x = eval(input("Enter a Salary >> "))
sum = sum + x
print("\nThe average salary for the month of February
is", sum / n)
Dr. Mazen Nabil Elagamy
Elements of a Program
For…. Loops

How many employees do you have? 5


Enter a Salary >> 320
Enter a Salary >> 450
Enter a Salary >> 340
Enter a Salary >> 760
Enter a Salary >> 450

The average salary for the month of February


is 464

Dr. Mazen Nabil Elagamy


Elements of a Program
Loops….Break

Dr. Mazen Nabil Elagamy


Elements of a Program
Loops….Break

for i in range(1,10):
if i==5:
break
print(i)

1
2
3
4
>>>

Dr. Mazen Nabil Elagamy


Elements of a Program
Loops….Continue

Dr. Mazen Nabil Elagamy


Loops….continue
for i in range(1,10): for val in "string":
if i==5: if val == "i":
continue continue
print(i) print(val)
print("The end")
1
2 s
3 t
4 r
6 n
7 g
8 The end
9 >>>
>>>
Dr. Mazen Nabil Elagamy
Elements of a Program
For….Else Loops
• In python, for loops also have
an else clause.
• The else clause executes after the loop
completes normally. This means that the
loop did not encounter a break statement.

Dr. Mazen Nabil Elagamy


Elements of a Program
The while Loop: a Condition-Controlled
Loop

• while loop: while condition is true, do


something
– Two parts:
• Condition tested for true or false value
• Statements repeated as long as condition
is true
– In flow chart, line goes back to previous part

Dr. Mazen Nabil Elagamy


Elements of a Program
The while Loop
• while <condition>:
<body>
• condition is a Boolean expression, just like in
if statements. The body is a sequence of one
or more statements.
• Semantically, the body of the loop executes
repeatedly as long as the condition remains true.
When the condition is false, the loop terminates.

Dr. Mazen Nabil Elagamy


Elements of a Program
The while Loop
• The condition is tested at
the top of the loop. This is
known as a pre-test loop. If
the condition is initially
false, the loop body will not
execute at all.
• In order for a loop to stop
executing, something has
to happen inside the loop to
make the condition false
• Iteration: one execution of
the body of a loop
Dr. Mazen Nabil Elagamy
Elements of a Program
The while Loop
Here’s an example of a while loop that counts from 0 to 10:
i = 0
while i <= 10:
print(i)
i = i + 1
The code has the same output as this for loop:
for i in range(11):
print(i)
0
1
2
3
4
5
6
7
8
9
10
Elements of a Program
While Loops….Continue

Dr. Mazen Nabil Elagamy


Elements of a Program
Interactive Loops
# average2.py
# A program to average a set of numbers
# Illustrates interactive loop with two
accumulators

moredata = "yes"
sum = 0.0
count = 0
while moredata == 'yes':
x = eval(input("Enter a number >> "))
sum = sum + x
count = count + 1
moredata = input("Do you have more numbers (yes or
no)? ")
print("\nThe average of the numbers is", sum / count)

Dr. Mazen Nabil Elagamy


Elements of a Program
Interactive Loops
Enter a number >> 32
Do you have more numbers (yes or no)? yes
Enter a number >> 45
Do you have more numbers (yes or no)? yes
Enter a number >> 34
Do you have more numbers (yes or no)? yes
Enter a number >> 76
Do you have more numbers (yes or no)? yes
Enter a number >> 45
Do you have more numbers (yes or no)? no

The average of the numbers is 46.4

Dr. Mazen Nabil Elagamy


Elements of a Program
Sentinels Loops
• Sentinel: special value that marks the end
of a sequence of items
– When program reaches a sentinel, it knows
that the end of the sequence of items was
reached, and the loop terminates.
– Must be distinctive enough so as not to be
mistaken for a regular value in the sequence
– Example: when reading an input file, empty
line can be used as a sentinel.

Dr. Mazen Nabil Elagamy


Elements of a Program
Sentinels Loops
# average3.py
# A program to average a set of numbers
# Illustrates sentinel loop using negative input as
sentinel

sum = 0.0
count = 0
x = eval(input("Enter a number (negative to quit) >> "))
while x >= 0:
sum = sum + x
count = count + 1
x = eval(input("Enter a number (negative to quit) >> "))
print("\nThe average of the numbers is", sum / count)

Dr. Mazen Nabil Elagamy


Elements of a Program
Sentinels Loops
Enter a number (negative to quit) >> 32
Enter a number (negative to quit) >> 45
Enter a number (negative to quit) >> 34
Enter a number (negative to quit) >> 76
Enter a number (negative to quit) >> 45
Enter a number (negative to quit) >> -1

The average of the numbers is 46.4

Dr. Mazen Nabil Elagamy


Example
Prime Number Check
• Write a python
program that check
whether a number is
prime or not.

Dr. Mazen Nabil Elagamy


Summary & Discussion

Dr. Mazen Nabil Elagamy

You might also like