[go: up one dir, main page]

0% found this document useful (0 votes)
12 views25 pages

Loops

The document explains loops in Python, detailing their structure, types (for and while loops), and control statements. It covers the use of the range function, examples of loop implementations, and the break and continue statements. Additionally, it outlines the use of else statements with loops and provides various programming exercises involving loops.

Uploaded by

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

Loops

The document explains loops in Python, detailing their structure, types (for and while loops), and control statements. It covers the use of the range function, examples of loop implementations, and the break and continue statements. Additionally, it outlines the use of else statements with loops and provides various programming exercises involving loops.

Uploaded by

addie20101979
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Loops in Python

What is a Loop
In computer Programming, a Loop is used to execute a group of
instructions or a block of code multiple times, without writing it
repeatedly. The block of code is executed based on a certain
condition. Loops are the control structures of a program.
What is a Loop

The structure of a Loop can be virtually divided into two parts,


namely:
The control statement: It comprises the conditions that have to
be met for the execution of the body of the Loop. For every
iteration of the Loop, the conditions in the control statement
have to be true.
The body: It comprises the block of code or the sequence of
logical statements that are to be executed multiple times.
What is a Loop
When you use a Loop in your program, you will not have to write
the block of code (written in the body of the Loop), over and over
again in it. The block of code will be executed as many times as the
control statement will hold true and the Loop will be terminated
when the conditions in the control statement become false.
If the conditions are not clearly defined in the control statement,
the Loop will keep on executing. Such Loops are termed as infinite
Loops. If no termination condition is provided in the control
statement of a Loop, then it automatically becomes an infinite
Loop.
What is a Loop
Types of Loops

There are basically two types of Loops in most computer


Programming languages, namely,
Entry Controlled Loop
In an entry controlled Loop, the control statement is written
right at the beginning of the Loop. This type of Loop is also
called a pre-checking Loop. The conditions in the control
statements are checked at first, and only if the conditions are
true, the body of the Loop is executed. If the condition turns out
to be false, the lines of code in the body of the Loop will not be
executed. Example: for loop & while loop.
Types of Loops

Exit Controlled Loop


In an exit controlled Loop, the control statement is written at the
end of the Loop structure. The lines of codes in the body of the
Loop are executed once before the condition is checked. Hence,
this type of Loop is also called a post-checking Loop. Example
do… while loop
Types of Loops

There are two types of Loops in Python, namely,


for Loop, and while Loop.
When a Loop is written within another Loop, the control
structure is termed as a nested Loop.
The range function
The range() function returns a sequence of numbers, starting from 0 by default,
and increments by 1 (by default), and stops before a specified number.
Syntax
range(start, stop, step)
Start-Optional. An integer number specifying at which position to start. Default
is 0
Stop-Required. An integer number specifying at which position to stop (not
included).
Step-Optional. An integer number specifying the incrementation. Default is 1
for loop
A for loop is used for iterating over a sequence (that is either a
list, a tuple, a dictionary, a set, or a string).
for loopvar in range(argument/aguments):
statements to be repeated

Examples:
for x in range(5): for x in range(3, 6): for x in range(3, 8, 2):
print(x, end=“ ”) print(x, end=“ ”) print(x, end=“ ”)

Output: Output: Output:


01234 345 357
for loop
More Examples with list & string:

fruits = ["apple", "banana", "cherry"] s = "Geeks"


for x in fruits: for i in s:
print(x) print(i)

Output: Output:
Apple G
Banana e
cherry e
k
s
for loop
WAP to display first ‘n’ numbers
n=int(input(“Enter upto which no:”))
for i in range(n):
print(i)
Output:
If n is 5, then
1
2
3
4
5
for loop
WAP to display all odd numbers below 20

for i in range(1,20,2):
print(i, end=“ ”)
Output:
1 3 5 7 9 11 13 15 17 19
for loop
WAP to display all even numbers below 20

for i in range(2,20,2):
print(i, end=“ ”)
Output:
2 4 6 8 10 12 14 16 18
for loop
WAP to display sum of first n numbers

n=int(input(“Enter a number”)
sum=0
for i in range(1,n):
sum=sum+i
print(sum)
for loop
WAP to display factorial of n (product of first n
numbers)

n=int(input(“Enter a number”)
product=1
for i in range(1,n):
product=product*i
print(product)
while loop
A while Loop is an entry controlled Loop. The condition
checking is done at the beginning of the Loop structure. The
general syntax of the while Loop is given below.
Initialize loop variable
while(condition):
Body of the Loop or statements to b e repeated
increment/decrement loop variable
while loop
The condition checking is done before the execution of the
body of the while Loop. The block of code in the body of the
While Loop is executed only if the condition is true. The body
of the Loop gets executed as many times as the condition is
true. After each iteration of the Loop, the control moves back to
the condition checking part at the beginning of the While Loop.
If the condition is not met, that is, if the boolean expression in
the braces (), turns out to be false, the while Loop is
terminated.
while loop
Example: In the given example, the variable ‘n’ is
n=5 initialized as an integer, and its value is
i=1
while(i<=n): assigned as 5 and loop variable i is
print (i) initialized as 1. Every time the condition
n+=1 ‘i<=n’ is met the While Loop will be
Output: executed, and the value of i will be
1 displayed on the screen. At every
2 iteration, the value of i will be increased
3 by 1. The Loop will be terminated when
the value of ‘i’ becomes greater than 5.
4
The above While Loop will display the
5 numbers from 1 to 5.
break & continue statements
break is used to exit a for loop continue is used to skip the
or a while loop. current block, and return to
the "for" or "while" statement.
Example:
count = 0 Example:
while True: for x in range(10):
print(count, end=“ ”) if x % 2 == 0:
count += 1 continue
if count >= 5: print(x, end=“ ”)
break
Output: Output:
01234 1,3,5,7,9
else statements

else with loop is used with both while and for loop. The else
block is executed at the end of loop means when the given loop
condition is false then the else block is executed. The else
keyword in a for loop/while loop specifies a block of code to be
executed when the loop is finished:
• Note: The else block will NOT be executed if the loop is
stopped by a break statement.
• So let’s see the example of while loop and for loop with else
below.
else statements-examples

for i in range(1, 4): for i in range(1, 4):


print(I, end=“ ”) print(i)
else: # Executed because no break in for break
print("No Break")
else: # Not executed as there is a break
Output: print("No Break")
1 2 3 No Break
Output :
1
Programs using loops
1. WAP to display first ‘n’ numbers.
2. WAP to display odd numbers below ‘n’.
3. WAP to display first ‘n’ odd numbers.
4. WAP to display even numbers below ‘n’.
5. WAP to display first ‘n’ even numbers.
6. WAP to display the sum of first ‘n’ numbers.
7. WAP to display the factorial of ‘n’.
8. WAP to display all numbers in a range.
Programs using loops
9. WAP to display numbers below ‘n’ in reverse order.
10. WAP to reverse a numbers.
11. WAP to find the sum of digits of a number.
12. WAP to display the multiplication table of ‘n’ for ‘m’ rows.
13. WAP to find whether the given number is a prime or not.
14. WAP to find whether the given number is a perfect number
or not.
15. WAP to find whether the given number is an amstrong
number or not.

You might also like