PYTHON Unit 2
PYTHON Unit 2
____________________________________________________
Decision making structure
There are three types of decision making statements in python language, those are
1. If statements
2. If else statement
3. Nested if else statement
4. If elif else statement
5. Switch statement
1. If Statement
_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 1
UNIT 2: CONTROL FLOW STRUCTURE
____________________________________________________
If statements allow for conditions to be set within code so that it behaves
differently depending on whether or not certain conditions are true.
Syntax
Example
Output
2. If else statement
From the name itself, we get the clue that the if-else statement checks the
expression and executes the if block when the expression is True otherwise it
will execute the else block of code.
The else block should be right after if block and it is executed when the
expression is False.
Syntax
_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 2
UNIT 2: CONTROL FLOW STRUCTURE
____________________________________________________
Example
Output
Syntax
Example
_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 3
UNIT 2: CONTROL FLOW STRUCTURE
____________________________________________________
Output
Syntax
Example
Follow nested if example
5. Switch Statement
Until version 3.10, Python never had a feature that implemented what the
switch statement does in other programming languages.
So, if you wanted to execute multiple conditional statements, you
would've had to use the elif keyword.
_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 4
UNIT 2: CONTROL FLOW STRUCTURE
____________________________________________________
From version 3.10 upwards, Python has implemented a switch case feature
called “structural pattern matching”.
You can implement this feature with the match and case keywords.
Syntax
Example
_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 5
UNIT 2: CONTROL FLOW STRUCTURE
____________________________________________________
Loops in Python
o While loop
o For loop
1. While Loop
Syntax& Example
count = 0
count = count + 1
print("Hello Geek")
2. For Loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).
_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 6
UNIT 2: CONTROL FLOW STRUCTURE
____________________________________________________
This is less like the for keyword in other programming languages, and works
more like an iterator method as found in other object-orientated
programming languages.
With the for loop we can execute a set of statements, once for each item in a
list, tuple, set etc.
Syntax
Example
# Python program to illustrate
# Iterating over range 0 to n-1
n =4
for i in range(0, n):
print(i)
Output
0
_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 7
UNIT 2: CONTROL FLOW STRUCTURE
____________________________________________________
Break statement
Continue statement
Range statement
for x in range(6):
print(x)
#0 1 2 3 4 5 6
print(x)
#2345
print(x)
#258
_____________________________________________________________________________________
Prepared By: Pro. Devdattt Chavda PG. 8