How Many Types Of Control Statements In Python-
-
1) Condition –
if, if-else, if-elif-else, nested-if
-
2) Loop –
for, while, nested loops
-
3) Branching –
break, continue, return, pass
Qsn-
Do we not have do while in python?
And-
No
1) Condition-
1) if – It is used to execute a block of code only in case the condition is True.
if True:
print("Yes")
else:
print("No")
OR
if 4>3: # if (4>3):
print("Yes") --> This will print because 4>3 returns True
else:
print("No")
Note- “if 4>3:” is recommended to use instead of “if (4>3)”.
Ex-
Write A Program To Compare 2 Nos.-
def test(a, b): # Function defination
if a > b:
print("a is greater then b")
else:
print("b is greater then a")
a = input("Enter the 1st no ")
b = input("Enter the 2nd no ")
test(a, b) #Function calling
Output-
Enter the 1st no 7
Enter the 2nd no 3
a is greater then b
If-Elif-Else-
Ex-
Write A Program To Compare 3 Nos.-
def test(a, b, c):
if a > b and a > c:
print("a is biggest")
elif b > a and b > c:
print("b is biggest")
else:
print("c is biggest")
a = input("Enter the 1st no ")
b = input("Enter the 2nd no ")
c = input("Enter the 3rd no ")
test(a, b, c) # 10 , 20, 30
Output-
Enter the 1st no 10
Enter the 2nd no 20
Enter the 3rd no 30
c is biggest
Nested If-
– Defining if block inside another if or else block.
Ex-
if True:
print("1")
if False:
print("2")
if False:
print("3")
else:
print("4")
else:
print("5")
else:
print("5")
Output-
1
5
Logical Operators
- >>> True and True
- True
- >>> False and False
- False
- >>> True and not True
- False
- >>> not True and True
- False
- >>> False and not True
- False
- >>> not False or not True
- False
2) Loop –
- – It is used to execute the block of Control Statements In Python repidily as long as the given condition is true.
- – We have two types of loops in Python-
- 1) For
- 2) While
1) For Loop-
- range()- A predefined function, which is used along with the for loop.
- Range can be used to specify the values like start, stop and step.
- Sytanx- range([Start], stop, [step]) # step up(+1) and step down(-1).
- # step = default value is 1
- # start and step are optional
- # start defalut value is 0
Java-
for(int i=0; i<9; i++){
}
OR
for([start]; [stop]; [step]){
}
Python-
Ex-
for i in range(4):
print(i)
OR
for i in range(0,4,1):
print(i)
Output-
0
1
2
3
Qsn-How To Print The Tables Of A No. In Python?
def printTable(n):
for i in range(1, 11):
print(n, "X", i, "=", n*i)
n = int(input("Enter the no"))
printTable(n)
Output-
Enter the no-2
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
2) While-
– Another loop same like for mentioned above.
Ex-
def printNo(): x=1 while x<=5: print(x) x = x+1 #No support of X++ or ++X printNo() Output- 1 2 3 4 5
Note – For loop internally converted into while loop by PVM at the time of executing your program.
Nested Loop-
- – Defining a loop inside the another loop.
- – By using nested loop concept we can process 2dimensional(row and column wise) data.
3) Branching
– break, continue, return, pass
1) Break:
It is used to terminate the execution of program.
Ex-
def main():
for a in range (1,7):
if a==4: #When a==4 then it control goes inside the if statement and it will break the execution of for loop.
break;
print("a is-", a)
main()
Output-
a is- 1
a is- 2
a is- 3
2) Continue:
def main():
for a in range(1, 7):
if a == 3 or a == 5: #It will not print the values for a=3 and a=5
continue
print("a val is ", a)
main()
Output-
a val is 1
a val is 2
a val is 4
a val is 6
3) Return
Ex-
def test():
sum = 30+40
return sum
val = test()
print("Total sum is -", val)
Output-
Total sum is - 70
4) Pass
- – It is used to define empty block, methods and class.
- – We can not leave block, method and class with the body.
Ex-
def test():
pass
test()
print("Done")
Output-
Done
Notes-
In Python empty means pass

