0% found this document useful (0 votes)
13 views21 pages

Lect-01 While For

Uploaded by

YASH WANKHEDE
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)
13 views21 pages

Lect-01 While For

Uploaded by

YASH WANKHEDE
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/ 21

Control Statements

Dr. Harish D. Gadade


Control Statements
● while Loop
● for Loop
● Break and Continue Statements

2
While Loop
● While Loops is used to execute a
block of statements repeatedly until
a given condition is satisfied.

● Syntax_1:

while expression:
Statement_1
Statement_2

● Syntax_2:
while condition:
Statement block
else:
Statement Block 3
While Loop - Hands-on!!
1. Write a program to print the numbers from one to five using while loop

4
While Loop - Hands-on!!
1. Write a program to print the numbers from one to five using while loop
2. WAP to add 10 consecutive numbers starting from 1 using while loop

5
While Loop - Hands-on!!
1. Write a program to print the numbers from one to five using while loop
2. WAP to add 10 consecutive numbers starting from 1 using while loop
3. Write a program to find the sum of the digits of a given number using
while loop

6
While Loop - Hands-on!!
1. Write a program to print the numbers from one to five using while loop
2. WAP to add 10 consecutive numbers starting from 1 using while loop
3. Write a program to find the sum of the digits of a given number using
while loop
4. Write a program to display the reverse of the entered number using while
loop

7
While Loop - Hands-on!!
1. Write a program to print the numbers from one to five using while loop
2. WAP to add 10 consecutive numbers starting from 1 using while loop
3. Write a program to find the sum of the digits of a given number using
while loop
4. Write a program to display the reverse of the entered number using while
loop
5. WAP to print the sum of the numbers from 1 to 30 (1 and 30 are included)
that are divisible by 5 using while loop

8
While Loop - Hands-on!!
1. Write a program to print the numbers from one to five using while loop
2. WAP to add 10 consecutive numbers starting from 1 using while loop
3. Write a program to find the sum of the digits of a given number using
while loop
4. Write a program to display the reverse of the entered number using while
loop
5. WAP to print the sum of the numbers from 1 to 30 (1 and 30 are included)
that are divisible by 5 using while loop
6. WAP to calculate factorial of a given number using while loop

9
Range() Function
● Inbuilt Function
● Syntax:
range(begin,end,step)
Where,
Begin : First number
End : is last number or a limit
Step : difference between each number in the sequence
● Example:
>>>list(range(1,6))
[1,2,3,4,5] 10
Range() Function
What is the range of the following
● range(5)
● range(1,5)
● range(0,10,2)
● range(5,0,-1)
● range(5,0,-2)
● range(-4,4)
● range(0,1)
● range(1,1)
● range(0) 11
Control Statements
● while Loop
● for Loop
● Break and Continue Statements

12
for - Loop
● for loop in python programming a bit different than other programming
languages.

● for loop is an iterator based loop. It goes through the elements in


any ordered sequence list i.e.list, tuples and dictionary.

● Syntax:

for i in X:
Block of Statements
else:
Block of Statements

13
for Loop - Hands-on!!
1. WAP to add 10 consecutive numbers starting from 1 using for loop
2. WAP to print the sum of the numbers from 1 to 30 (1 and 30 are included)
that are divisible by 5 using for loop
3. WAP to calculate factorial of a given number using for loop
4. WAP to print fibonacci series up to limit 8
5. Write a program to print even number from 0 to 10 and find their sum
6. Write a program to print and calculate the sum of numbers from 1 to 20
which are not divisible by 2,3 or 5

14
Nested for Loop - Hands-on!!
Examples:
● Loop within loop or when one 2. 1
loop is inserted within another 2 2
loop is called nested loop. Examples: 3 3 3
4 4 4 4
1. 0 0 0 0
Examples: 1 1 1 1 3. 1
2 2 2 2 1 2
1. * 3 3 3 3 1 2 3
* *
1 2 3 4
* * *
4. 1
2 1
3 2 1
4 3 2 1
15
Control Statements
● while Loop
● for Loop
● Break and Continue Statements

for i in range(5):
if(i==3):
break
print(“Hello”,i)

16
Control Statements
● while Loop
● for Loop
● Break and Continue Statements

for i in range(5):
if(i==3):
continue
print(“Hello”,i)

17
Homework
1. Write a program to find the square root of a number

2. Write a program to find area of a rectangle

3. Write a program to print Fibonacci series up to 8

4. Write a program to swap the values of two variables

5. Write a program to convert kilogram into pound

6. Write a program to find whether a number is even or odd

7. Write a program to check the largest among the given three numbers

18
Lab Assignment
1. Write a program to check whether the entered number is an Armstrong number or not
a. E.g. 153 = 1^3+5^3+3^3 = 153
2. Write a program to display the pattern of numbers as follows
a. 1
b. 21
c. 321
d. 4321
3. Write a program to print fibonacci sequence for a given limit.
a. 0 1 2 3 5 8 13 21 . . .

19
Lab Assignment
Write a program to accept a number and reverse that number using for/while loop

Write a program to sum of the following series

x+x^2/2! +x^3/3! + . . . . +n
Write a program to reverse a list of elements using indexing and using your own logic

Write a Program to find Min, Max and second Max element from given list using range() function

Write a program to read the password from a user. If the user types the correct password, then display the message, " Welcome".

Note: Only three attempts are allowed to enter the right password.

Write a Program for the following

a) Finding Max and Second Max Element from a List 20


Lab Assignment
Write a Program for the following

a) Finding Max and Second Max Element from a List

b) Generating Prime Numbers for a given range of the list

c) Compute X Power Y

d) Finding Kth smallest and biggest Element of List/Array

Write a Python program to accept 'n' elements from the user and ask to search a key 'k' from given 'n' elements using Linear
Search.

Write a program for binary search using function

Write programs of your choice which has following concepts


21
1. Positional Arguments

You might also like