EXPERIMENT NO 5
AIM: Develop program using simple and nested loops in python
OBJECTIVE: The objective of this experiment is:
1. Getting familiar with the loops in Python.
OUTCOMES: After completing the Experiment#5 students will be able to:
1. Use simple and nested loops to device solutions for given problem.
2. Write a python code using the loops to solve a programming problem.
PRE-REQUISITES:
1 . Basic Python code
THEORY:
Simple and nested loops in Python
Python Loops:
Python has two primitive loop commands:
1. while loops 2. for loops
For Loops in Python
Python For loop is used for sequential traversal i.e. it is used for iterating over an iterable like String, Tuple,
List, Set, or Dictionary.
Note: In Python, for loops only implement the collection-based iteration.
For Loops Syntax:
for var in iterable:
# statements
For Loop flowchart
Here the iterable is a collection of objects like lists, and tuples. The indented statements inside the for loops
Course Coordinator: Ms. Lalita Randive 1 Approved By:HEAD BSH
are executed once for each item in an iterable. The variable var takes the value of the next item of the iterable
each time through the loop.
Python For Loop inside a For Loop
This code uses nested for loops to iterate over two ranges of numbers (1 to 3 inclusive) and prints the value of i
and j for each combination of the two loops. The inner loop is executed for each value of i in the outer loop.
The output of this code will print the numbers from 1 to 3 three times, as each value of i is combined with each
value of j.
Output :
for i in range(1, 4): 11
for j in range(1, 4):
print(i, j) 12
13
21
22
23
31
32
33
The break Statement
With the break statement we can stop the loop before it has looped through all the items:
Example
Exit the loop when x is "banana":
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
Output:
apple
Banana
The Continue Statement
With the continue statement we can stop the current iteration of the loop, and continue with the next:
Course Coordinator: Ms. Lalita Randive 2 Approved By:HEAD BSH
Example
Do not print banana:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
Else in For Loop
The else keyword in a for loop specifies a block of code to be executed when the loop is finished:
Example
Print all numbers from 0 to 5, and print a message when the loop has ended:
for x in range(6):
print(x)
else:
print("Finally finished!")
Output:
0
1
2
3
4
5
Finally finished!
Nested Loops:
A nested loop is a loop inside a loop.
The "inner loop" will be executed one time for each iteration of the "outer loop":
Example
Print each adjective for every fruit:
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
output:
red apple
Course Coordinator: Ms. Lalita Randive 3 Approved By:HEAD BSH
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry
The pass Statement
for loops cannot be empty, but if you for some reason have a for loop with no content, put in the pass statement to
avoid getting an error.
Example
for x in [0, 1, 2]:
pass
The while Loop:
With the while loop we can execute a set of statements as long as a condition is true.
The syntax of while loop is:
while condition:
# body of while loop
Flowchart of Python while Loop
Flowchart of while Loop
Example:Print i as long as i is less than 6:
i=1
while i < 6:
print(i)
i += 1
Course Coordinator: Ms. Lalita Randive 4 Approved By:HEAD BSH
output:
1
2
3
4
5
The break Statement:
With the break statement we can stop the loop even if the while condition is true:
Example
Exit the loop when i is 3:
i=1
while i < 6:
print(i)
if i == 3:
break
i += 1
output:
1
2
3
The continue Statement:
With the continue statement we can stop the current iteration, and continue with the next:
Example
Continue to the next iteration if i is 3:
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
Output:
1
2
4
5
Course Coordinator: Ms. Lalita Randive 5 Approved By:HEAD BSH
Problem Statement: Write a program to print 1 to 10 using while loop.
Algorithm:
Step 1: Start
Step 2: input i=1
Step 3: while i<=10
Step 4: Print i
Step 5: increment i
Step 6: Stop.
Flowchart:
Start
i=1
While
i<=10
Print i
i=i+1
STOP
Course Coordinator: Ms. Lalita Randive 6 Approved By:HEAD BSH
Code:
i=1
while (i<=10):
print(i)
i=i+1
Output: 1
2
3
4
5
6
7
8
9
10
ASSESSMENT QUESTIONS:
1. draw the pattern using for and while loop:
a) * b) 1 c) 5 4 3 2 1 d) *
** 22 54321 **
*** 333 543 ***
**** 4444 5 4 ****
***** 55555 5 *****
2. Factorial of a number.
3. Find A Fibonacci Sequence Upto nth Term Using The While Loop
4. Write a program to print 1 to 10 (use while loop)
5. Write a program to print all the numbers from 10 to 1
6. Write a program to print first 10 integers and their squares using while loop
7. Write a program to find whether number is even or odd
Course Coordinator: Ms. Lalita Randive 7 Approved By:HEAD BSH