0% found this document useful (0 votes)
3 views

Experiment-6 Python notes

The document explains conditional statements and loops in Python, detailing various types of conditional statements including if, if-else, nested-if, if-elif-else ladder, and the switch statement using the match keyword. It also covers looping constructs such as while loops and for loops, including the use of the range() function, and mentions the absence of a do-while loop in Python while providing a workaround. Examples are provided for each construct to illustrate their usage.

Uploaded by

singhrohit200315
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Experiment-6 Python notes

The document explains conditional statements and loops in Python, detailing various types of conditional statements including if, if-else, nested-if, if-elif-else ladder, and the switch statement using the match keyword. It also covers looping constructs such as while loops and for loops, including the use of the range() function, and mentions the absence of a do-while loop in Python while providing a workaround. Examples are provided for each construct to illustrate their usage.

Uploaded by

singhrohit200315
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment 6: Conditional Statements and Loops

 Conditional Statements in Python :-


1. The if statement
2. The if-else statement
3. The nested-if statement
4. The if-elif-else ladder
5. Switch statement

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-

# python program to implement If statement

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-

# python program to show If else statement


i = 20
if (i < 15):
print("i is smaller than 15")
else:
print("i is greater than 15")

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-

# Python program to implement if-elif-else ladder

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

 Looping statements in Python:-


1. While loop
2. For loop
Note:- Unlike C/C++/Java, Do while loop does not exist in Python But we can indirectly
implement it.

1. While Loop in Python


In python, a while loop is used to execute a block of statements repeatedly until a given condition is
satisfied. And when the condition becomes false, the line immediately after the loop in the program is
executed.
Syntax:
while condition :
# statement(s)
Eg-

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

You might also like