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

PFSD Unit 2. (Autosaved)

Uploaded by

gtabackup005
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)
33 views25 pages

PFSD Unit 2. (Autosaved)

Uploaded by

gtabackup005
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/ 25

Python FullStack Devlopment

FLOW CONTROL
Conditional Statements
If

if condition : statement
OR
if condition :
statement-1
statement-2
statement-3
If condition is true then statements will be executed.
Eg:
1) name=input("Enter Name:")
2) if name==“kakashi" :
3) print("Hello kakashi Good Morning")
4) print("How are you!!!")
D:\Python_classes>py test.py
Enter Name:kakashi
Hello kakashi Good Morning
How are you!!!
if-else:
if condition:
Action-1
else:
Action-2
if condition is true then Action-1 will be executed otherwise Action-2 will be executed.
1) name=input("Enter Name:")
2) if name==“sakuna" :
3) print("Hello itadori Good Morning")
4) else:
5) print("Hello Guest Good Moring") ask students
if-elif-else:

if condition1:
Action-1
elif condition2:
Action-2
elif condition3:
Action-3
elif condition4:
Action-4
...
else:
Default Action
Based condition the corresponding action will be executed
1) brand=input("Enter Your Favourite Brand:")
2) if brand=="RC" :
3) print("It is childrens brand")
4) elif brand=="KF":
5) print("It is not that much kick")
6) elif brand=="FO":
7) print("Buy one get Free One")
8) else :
9) print("Other Brands are not recommended") ask student
Note:
1) else part is always optional. Hence the following are various possible
syntaxes.
1) If
2) if – else
3) if-elif-else
4) if-elif
2) There is no switch statement in Python
Nested if
if condition_1: The syntax of the nested if statements can vary, but the main
if condition_1_a: thing is an if-else block is nested inside another if-else block.
...
elif condition_1_b:
...
else:
...
elif condition_2:
if condition_2_a:
...
else:
...
elif condition_3:
if condition_3_a:
...
else:
...
# Python Program to check if the given year is a leap year or not

# Inputting the year


year = int(input("Enter any year: "))

if year % 4 == 0: # if the year is divisible by 4


if year % 100 == 0: # if the year is divisible by 100
if year % 400 == 0: # if the year is divisible by 400
print(f"{year} is a leap year.")
else: # if the year is divisible by 100 but not 400
print(f"{year} is not a leap year.")
else: # if the year is divisible by 4 but not 100
print(f"{year} is a leap year.")
else: # if the year is not divisible by 4
print(f"{year} is not a leap year.")

# nested if statement in python


# explain the nested if statement in python with example
Iterative Statements

֍ If we want to execute a group of statements multiple times then we should go


for Iterative statements.

֍ Python supports 2 types of iterative statements.


1) for loop
2) while loop
for loop:

If we want to execute some action for every element present in some sequence
(it may be string or collection) then we should go for for loop.
Syntax: for x in sequence:
Body
Where sequence can be string or any collection.
Body will be executed for every element present in the sequence.
Eg:
s = 'gojo will die' s=3
for i in range(s):
for i in (s):
print(i) print(i) 0
1
2
while loop:

If we want to execute a group of statements iteratively until some condition false ,


then we should go for while loop.

Syntax: while condition :


body
n=5
i= 1
While i<=n:
print(i)
i=i+1

1
2
3
4
5
Transfer Statements
break:
We can use break statement inside loops to break loop execution based on some
condition.
1) for i in range(10):
2) if i==7:
3) print("processing is enough..plz break")
4) break
5) print(i)
continue:

We can use continue statement to skip current iteration and continue next iteration.
Eg 1: To print odd numbers in the range 0 to 9
1) for i in range(10):
2) if i%2==0:
3) continue
4) print(i)
1
3
5
7
9
pass statement:

pass is a keyword in Python.


pass statement:
The pass statement is used as a placeholder for future code. When the pass
statement is executed, nothing happens, but you avoid getting an error when
empty code is not allowed. Empty code is not allowed in loops, function
definitions, class definitions, or in if statements.

It is an empty statement
It is null statement
It won't do anything
Q) What is the difference between for loop and while loop
in Python?
֍ We can use loops to repeat code execution
֍ Repeat code for every item in sequence - for loop
֍ Repeat code as long as condition is true - while loop
Q) How to exit from the loop? By using break statement
Q)How to skip some iterations inside loop? By using continue statement.
Q)When else part will be executed wrt loops? If loop executed without break

You might also like