PFSD Unit 2. (Autosaved)
PFSD Unit 2. (Autosaved)
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
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:
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:
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