This document discusses loops in Python. It begins by explaining why loops are useful, such as for payroll processing to calculate salaries and bonuses for multiple employees. It then defines loops as allowing code to execute repeatedly based on conditional statements. The document outlines the main types of loops in Python - while loops, for loops, and nested loops. While loops iterate until a condition is false, for loops iterate a specified number of times, and nested loops involve loops within other loops. Examples are provided for each loop type, such as a guessing game to demonstrate a while loop and calculating factorials with a for loop.
4. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Why To Use Loops
If a software developer develops a software module for payroll processing
that needs to compute the salaries and the bonus of all the employees.
Software module
for payroll
processing
Salary
Bonus
Total
Employee - 1
Employee - 2
Employee - 3
5. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Why To Use Loops
Manual Loops
Employee-1:
Salary
Bonus
Total
Employee-2:
Salary
Bonus
Total
Employee-3:
Salary
Bonus
Total
Start
Logic to calculate
the total salary
Employee-1:
Salary
Bonus
Total
Start
Logic to calculate
salary of
Employee-1
Employee-2:
Salary
Bonus
Total
Start
Logic to calculate
salary of
Employee-2
Employee-3:
Salary
Bonus
Total
Start
Logic to calculate
salary of
Employee-3
7. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
What Are Loops
Loops allows the execution of a statement or a group of statement multiple times.
In order to enter the loop there are certain conditions defined in the beginning.
Once the condition becomes false the loop stops and the control moves out of the loop.
Start
Conditional Code
Condition
If condition
is false
If condition
is true
Exit
Loops FiniteInfinite
9. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
While Loop
While loops are known as indefinite or conditional loops. They will keep iterating until certain
conditions are met. There is no guarantee ahead of time regarding how many times the loop will
iterate.
Syntax:
Start
While
Condition?
Body of the loop
True
False
Exit
While
For
Nested
10. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
While Loop Example
A little guessing game
Let the correct answer be 13
Input Number: 10 Number is too small
Input Number: 15 Number is too large
Input Number: 13 Exit: Congratulations. You made it!
While
For
Nested
11. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
For Loop
For loop is a Python loop which repeats a group of statements a specified number of times. The
for loop provides a syntax where the following information is provided:
Boolean condition
The initial value of the counting variable
Incrementation of counting variable
Start
Execute Statement (s)
End
Next item from
sequence
If no more items in the
sequence
Item from
sequence
While
For
Nested
12. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
For Loop Example
Lets find factorial
Notice we know the number of iterations so for loop is a better option
3! = 3(2)(1)
4! = 4(3)(2)(1)
5! = 5(4)(3)(2)(1)
Example
Factorial = n(n-1)(n-2)…..1
n n-1 n-2 1
While
For
Nested
13. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Nested Loops
Python programming language allows use of loop inside another
loop. This is called Nested Loop. below is the syntax for the same:
Syntax: Syntax:
While
For
Nested
14. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Nested Loop Example
Lets code a program in Python that effectively simulates a bank ATM.
Enter the 4-digit pin
Make a withdrawal
Pay in
Check balance
Return card
While
For
Nested
15. www.edureka.co/pythonEDUREKA PYTHON CERTIFICATION TRAINING
Nested Loop Example
Lets write a code to print the Pythagorean numbers.
Three integers satisfying a2+b2=c2 are called Pythagorean numbers
c2a2
b2
3 4 5
5 12 13
6 8 10
8 15 17
9 12 15
12 16 20
Enter the maximum number:
20
Pythagorean numbers
between 1-20
While
For
Nested