0% found this document useful (0 votes)
19 views12 pages

Class 05 Conditinal Statements (If - Elif - Else)

The document discusses control structures in Python, focusing on conditional statements that allow programs to make decisions based on specified conditions. It explains the syntax and execution of 'if', 'elif', and 'else' statements, as well as nested conditions and the 'match-case' structure for pattern matching. Additionally, it highlights the use of wildcards in match-case statements for handling unmatched patterns.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views12 pages

Class 05 Conditinal Statements (If - Elif - Else)

The document discusses control structures in Python, focusing on conditional statements that allow programs to make decisions based on specified conditions. It explains the syntax and execution of 'if', 'elif', and 'else' statements, as well as nested conditions and the 'match-case' structure for pattern matching. Additionally, it highlights the use of wildcards in match-case statements for handling unmatched patterns.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Programming Using

PYTHON 5
Control Structures
Control Structures
Yeasir Arafat Ratul

Control Structures can be considered


as the building blocks of computer
programs. They are commands that
enable a program to “take decisions”.

CONTROL
What is conditional statement?
Yeasir Arafat Ratul

Decision-making is as important in any programming


language as it is in real life. Decision-making in a
programming language is automated using conditional
statements, in which Python evaluates the code to see if
it meets the specified conditions.

যদি – if

অথবা যদি – else if - elif

তা না হলে - else
When a condition is executed?
Yeasir Arafat Ratul

A condition is executed when it is ‘TRUE’ if False:


Then Not Executed

elif False:
Then Executed

elif True:
Then Not Executed

else (All False)


Then Executed
How conditions are written and executed?
Yeasir Arafat Ratul

if (condition): a>b
if False:
statements Then Not Executed
Or
elif(condition): elif False:
statements a == b
Then Executed
elif(condition): Or
elif True:
statements Then Not Executed
a > b and b > c
else: else (All False)
statements Or
Then Executed
a > b or b > c
How to use a condition
Yeasir Arafat Ratul

• if

• if - else

• if – elif – else

• Nested – if - else
Compound Condition
Yeasir Arafat Ratul

if condition_1 and/or condition_2 :


statement
Nested If - Else
Yeasir Arafat Ratul

if (condition):
statements

if(condition):
statements
elif(condition):
statements
else:
statements
else:
statements
MATCH - CASE

A match statement will compare a given variable’s


value to different shapes, also referred to as the match variable_name:
pattern. The main idea is to keep on comparing the case ‘pattern1’ :
variable with all the present patterns until it matches //statement1
into one. case ‘pattern2’ :
//statement2

case ‘patternN’:
//statement n
case other:
// statement
MATCH – CASE EXAMPLE

data = int(input("Enter a number: "))

match data:
case 1:
print("You Are First")
case 2:
print("You Are Second")
case 3:
print("You Are Third")

# default case
case other:
print("Not in Top Three")
MATCH – CASE Wildcard

A single underscore is used as the wildcard. This means if none


of the above patterns matches, the wildcard action is
performed. This is optional, so you can also omit it, and then
match variable_name:
nothing at all happens if no match is found.
case ‘pattern1’ :
//statement1
case ‘pattern2’ :
//statement2

case _:
// statement
Yeasir Arafat Ratul

THANK YOU

You might also like