Python LabManual Exp5
Python LabManual Exp5
PRE-REQUISITES:
1 . Basic Python code
THEORY:
Simple and nested loops in Python
Python Loops:
Python has two primitive loop commands:
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
Example
Exit the loop when x is "banana":
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:
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
while condition:
# body of while loop
Flowchart:
Start
i=1
While
i<=10
Print i
i=i+1
STOP
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