Experiment-6 Python notes
Experiment-6 Python notes
1. if statement
It is used to decide whether a certain statement or block of statements will be executed or not.
Syntax:
if condition:
# Statements
Eg-
i = 10
if (i > 15):
print("10 is less than 15")
print("Hello")
Output:
Hello
2. if-else statement
if the condition is true ,then the if statement is executed. But if the condition is fales ,then the else
statement is executed.
Syntax:
if (condition):
# Executes this block if condition is true
else:
# Executes this block if condition is false
Eg-
Output:
i is greater than 15
3. nested-if statement
Syntax:
if (condition1):
# executes when condition is True
if (condition2):
# executes when condition is True
Eg- # Python program to show nested if statement
i = -15;
if i != 0: # condition 1
if i > 0: # condition 2
print("Positive")
if i < 0: # condition 3
print("Negative")
Output:
Negative
4. if-elif-else ladder
Syntax:
if (condition):
# statement
elif (condition):
# statement
.
.
else:
# statement
Eg-
i = 20
if (i = = 10):
print("i is 10")
elif (i = = 15):
print("i is 15")
elif (i = = 20):
print("i is 20")
else:
print("i is not present")
Output:
i is 20
(v) Switch statement: (using match keyword)
Python does not have a switch case statement but we can indirectly achieve it using match keyword.
Unlike C/C++/Java, In python there is no fall through concept of switch statement.
Note:- The match keyword related code runs only in python 3.10 or above versions.
Eg-
def switch(arg):
match arg:
case 1:
print("hello")
case 2:
print("world")
case 3:
print("Srinath")
case default:
print("University")
if __name__ = = "__main__":
arg = 3
switch(arg)
O/P:-
Srinath
a=0
while a<4:
print("Hello")
a+=1
Output:
Hello
Hello
Hello
Hello
2. For Loop(i.e for in loop) in Python
- Def.- A for loop can be used to iterate over a range using range() function.
Syntax:
for iterator_var in sequence:
statements(s)
Note:-
- range() function
(i)To loop through a set of code a specified number of times, we can use the range( ) function. The
range( ) function returns a sequence of numbers, starting from 0 (by default) to n-1 by incrementing 1
(by default).
Syntax:- range(staring value, ending value-1)
Eg1-
for x in range(6): #Its same as for x in range(0,6):
print(x)
O/P:-
1
2
3
4
5
Note: range(6) is not the values of 0 to 6, but the values 0 to 5.
(ii)The range( ) function defaults to 0 as a starting value, however it is possible to specify the starting
value by adding a parameter: range(2, 6), which means values from 2 to 5 (but not including 6):
Eg-2
for x in range(2,6):
print(x)
O/P:-
2
3
4
5
3. Do while loop
Do while loop is a type of control looping statement that can run any statement until the condition
statement becomes false specified in the loop. In do while loop the statement runs at least once no
matter whether the condition is false or true.
Syntax:
do{
// statement or
// set of statements
}
while(condition)
Note:-
Unlike C/C++/Java, Do while loop does not exist in Python But we can indirectly implement it using
while loop and if statement.
Eg-
i=0
while True:
print(i)
i=i+1
if i>5:
break
Output:-
0
1
2
3
4
5
Note: In C
int i = 0;
do {
printf("%d\n", i);
i++;
}
while (i >5);
Output:-
0
1
2
3
4
5