0% found this document useful (0 votes)
36 views19 pages

Python Day 2

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

Python Day 2

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

PYTHON CONDITIONAL LOOPS

CONTENTS

 INTRODUCTION
 PYTHON IF STATEMENT, IF – ELSE STATEMENT, ELIF STATEMENT & NESTED IF STATEMENT
 PYTHON SWITCH CASE
 PYTHON FOR LOOP & WHILE LOOP
 ENUMERATE, BREAK & CONTINUE STATEMENT
INTRODUCTION
CONDITIONAL STATEMENT

 Conditional Statement in Python perform different computations or actions depending on whether a


specific Boolean constraint evaluates to true or false. Conditional statements are handled by IF statements
in Python
IF STATEMENT

 SYNTAX
if expression:
Statement

 EXAMPLE
x=2, y=8

 OUTPUT
x is less than y
IF – ELSE STATEMENT

 SYNTAX
if expression:
Statement
else:
Statement
 EXAMPLE

 OUTPUT
x is greater than y
ELIF STATEMENT
 SYNTAX
if expression:
Statement
elif expression:
Statement
else:
Statement
 EXAMPLE

print(st)
 OUTPUT
x is same as y
MINIMAL CODE

 SYNTAX
A if B else C
 EXAMPLE

 OUTPUT
x is greater than or equal to y
NESTED IF STATEMENT
 SYNTAX
if expression:
Statement
if expression:
Statement
else:
Statement
 EXAMPLE
x=2, y=4 & z=6
if (x < y):
if (x < z):
print(“ x is smaller than y & z ”)
else:
print(“ x is greater than y & z”)
 OUTPUT
x is smaller than y & z
PYTHON SWITCH CASE
 EXAMPLE
def SwitchExample(argument):
switcher = {
0: " This is Case Zero ",
1: " This is Case One ",
2: " This is Case Two ",
}
return switcher.get(argument, "nothing")
argument = 1
SwitchExample(argument)

 OUTPUT
This is Case One
LOOP

 Loops can execute a block of code number of times until a certain condition is met. Their usage is fairly common in
programming. Unlike other programming language that have For Loop, while loop, dowhile, etc.
WHILE
LOOP
 While Loop is used to repeat a block of code. Instead of running the code block once, It executes the code block multiple
times until a certain condition is met.

 SYNTAX
while expression:
Statement

 EXAMPLE
x=0
while (x < 4):
print(x)
x=x+1

 OUTPUT
0
1
2
3
FOR LOOP

 For loop is used to iterate over elements of a sequence. It is often used when you have a piece of code which you want to
repeat "n" number of time.

 EXAMPLE

x=0
for x in range(1,4):
print(x)

 OUTPUT
1
2
3
FOR LOOP USING STRING
 EXAMPLE
Months= [“Jan”, ”Feb”, ”Mar”, “April”]
for m in Months:
print(m)
 OUTPUT
Jan
Feb
Mar
April
BREAK STATEMENTS IN FOR LOOP
 EXAMPLE
for x in range (10,20):
if (x == 15): break
print (x)
 OUTPUT
10
11
12
13
14
CONTINUE STATEMENT IN FOR LOOP
 EXAMPLE
for x in range (10,20):
if (x % 5 == 0): continue
print (x)
 OUTPUT
11
12
13
14
15
16
17
18
19
ENUMERATE IN PYTHON
 EXAMPLE
Months = ["Jan","Feb","Mar","April","May","June"]
for i, m in enumerate (Months):
print (i,m)
 OUTPUT
0 Jan
1 Feb
2 Mar
3 April
4 May
5 June
Conditional loops assignment
1. Print First 10 natural numbers using while loop
2. Reverse the following list using for loop
list1 = [10, 20, 30, 40, 50]
3.Python program to display all the prime numbers within a range 25 and 50
4. Display Fibonacci series up to 10 terms
5. Use a loop to display elements from a given list which are present at even position
list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
6.Write a Python program that prints all the numbers from 0 to 6 except 3 and
Note : Use 'continue' statement.
7. Write a Python program to convert month name to a number of days.
Expected Output:
List of months: January, February, March, April, May, June, July, August, September, October, November,
December
Input the name of Month: February
No. of days: 28/29 days
8. Write a Python program to construct the following pattern, using a nested for loop.
*
**
***
****
*****
****
***
**
*
9.Write a Python program to construct the following pattern, using a nested loop number.
Expected Output:
1
22
333
4444
55555
666666
7777777
88888888
999999999
10.Write a Python program to find those numbers which are divisible by 7 and multiple of 5, between 1500 and 2700 (both
included).
THANK YOU

You might also like