0% found this document useful (0 votes)
4 views11 pages

Python Multiplication Table Loops

The document provides an overview of loops in Python, specifically focusing on how to print a multiplication table using both while and for loops. It includes examples of code for generating the multiplication table and calculating the sum of reciprocals of the first 100 natural numbers. Additionally, it discusses the structure of while and for loops, as well as the use of the 'continue' statement in loop control.

Uploaded by

eaabiolaadebayo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

Python Multiplication Table Loops

The document provides an overview of loops in Python, specifically focusing on how to print a multiplication table using both while and for loops. It includes examples of code for generating the multiplication table and calculating the sum of reciprocals of the first 100 natural numbers. Additionally, it discusses the structure of while and for loops, as well as the use of the 'continue' statement in loop control.

Uploaded by

eaabiolaadebayo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Loops

1
Printing Multiplication Table
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50

Aug 12, 2025 Python Programming 2


Program…
n = int(input('Enter a number:
Too much'))
print (n, 'X', 1, '=', n*1)repetition!
print (n, 'X', 2, '=', n*2)Can I avoid
print (n, 'X', 3, '=', n*3) it?
print (n, 'X', 4, '=', n*4)
print (n, 'X', 5, '=', n*5)
print (n, 'X', 6, '=', n*6)
….

Aug 12, 2025 Python Programming 3


Printing Multiplication Table

Input n Loop Entry


i=1

Loop Exit
i <=10
TRUE FALSE

Print n X i = n*i Stop


i = i+1

Loop
Aug 12, 2025 Python Programming 4
Printing Multiplication Table
Input n
i=1

TRUE
i <=10
FALSE n = int(input('n=? '))
i=1
Print n x i = ni Stop
i = i+1

while (i <= 10) :


print (n ,'X', i, '=', n*i)
i=i+1
print ('done‘)

Aug 12, 2025 Python Programming 5


While Statement
while (expression):
S1 expression
FALSE

S2
TRUE

S1 S2
1. Evaluate expression
2. If TRUE then
a) execute statement1
b) goto step 1.
3. If FALSE then execute statement2.
Aug 12, 2025 Python Programming 6
For Loop
• Print the sum of the reciprocals of the
first 100 natural numbers.

rsum=0.0# the reciprocal sum

# the for loop


for i in range(1,101):
rsum = rsum + 1.0/i
print ('sum is', rsum)
Aug 12, 2025 Python Programming 7
For loop in Python
• General form

for variable in sequence:


stmt

Aug 12, 2025 Python Programming 8


range
• range(s, e, d)
– generates the list:
[s, s+d, s+2*d, …, s+k*d]
where s+k*d < e <= s+(k+1)*d
• range(s, e) is equivalent to range(s, e, 1)
• range(e) is equivalent to range(0, e)
Exercise: What if d is negative? Use python
interpreter to find out.
Aug 12, 2025 Python Programming 9
Quiz
• What will be the output of the following
program

# print all odd numbers < 10


i = 1
while i <= 10:
if i%2==0: # even
continue
print (i, end=‘ ‘)
i = i+1
Aug 12, 2025 Python Programming 10
Continue and Update Expr
• Make sure continue does not bypass update-
expression for while loops

# print all odd numbers < 10


i = 1 i is not incremented
while i <= 10: when even number
if i%2==0: # even encountered.
continue Infinite loop!!
print (i, end=‘ ‘)
i = i+1
Aug 12, 2025 Python Programming 11

You might also like