Python Conditional Statements

Python Conditional Statements, Decision making in Python programming, Python if statement, Python else statement, and Python elif statement.

Python Language Conditional Statements

Conditional Statements are part of control flow statements in computer programming,

We have three types of Control Flow Statements in Python:
1. Conditional Statements
2. Loop Statements
3. Branching Statements

In Python language also we can find these 3 types of statements…
Generally in any Programming language, conditional Statements are 2 types…
1. If Statement
2. Switch Statement
but in Python no Switch statement, only if statement

Usage of Conditional/Decision Making Statements

1. Execute a block of Statements when the condition is true.
2. Execute a block of Statements when a compound condition is true.
3. Execute a block of Statements when the condition is true otherwise execute another block of Statements
4. Decide among several alternates(elif)
5. Execute a block of Statements when more than one condition is true (Nested if)


Usage of Conditional Statements / Decision-Making Statements

1. Execute a block of Statements when the condition is true.

Syntax:

if (Condition/Expression):
Statement(s)

Example:

a, b = 100, 50
if (a>b)
print (“A is a Big Number”)

a, b = 100, 50
if (a>b):
print (“A is a Big Number”)
print (“Hello Python”)


2. Execute a block of Statements when a compound condition is true

Syntax:
if ((condition1/Expression1) and (Condition2/Expression2)) :
Statement(s)

Example:
a, b, c = 100, 50, 700
if ((a>b) or (a>c)):
print (“A is a Big Number”)


3. Execute a block of Statements when condition is true otherwise execute another block of Statements

Syntax:
if (Condition):
Statement(s)
else:
Statement(s)

Example:

a, b = 100, 200
if (a>b):
print (“A is a Big Number”)
else :
print (“B is a Big Number”)


4. Decide among several alternates(elif)

Syntax:

if (condition):
Statement(s)
elif (condition):
Statement(s)
elif (condition):
Statement(s)
else:
Statement((s)

Example:
Initialize an Integer Variable and verify the range

If the number is in between 1 and 100 then display “Number is a Small Number”
If the number is in between 101 and 1000 then display “Number is a Medium Number”
If the number is in between 1001 and 10000 then display “Number is a Big Number”
If the number is more than 10000 then display “Number is a High Number”
Otherwise, display “Number is a either Zero or Negative Number”
——————————-
Python Program:

a=0;
if ((a>=1) and (a<=100)):
print (“A is a Small Number”)
elif ((a>100) and (a<=1000)):
print (“A is a Medium Number”)
elif ((a>1000) and (a<=10000)):
print (“A is a Big Number”)
elif (a>10000):
print (“A is a High Number”)
else:
print (“A is either Zero or Negative Number”)


5. Execute a block of Statements when more than one condition is true (Nested if)

syntax:
if (condition){
if (condition){
if (condition){
Statements
————–
————–
}
}
}

Example:
Initialize a, b, c, and d variables (Integer variables), check if the a variable
is bigger than other three variables or not?

a, b, c, d = 100, 90, 70, 500;

if (a>b):
if (a>c):
if (a>d):
print (“A is a Big Number”)
else :
print (“A is Not a Big Number”)
——————————-
a, b, c, d = 100, 909, 70, 50;

if (a>b):
if (a>c):
if (a>d):
print (“A is a Big Number”)
else :
print (“A is Not a Big Number “+ “3rd Condition is not true”)
else :
print (“A is Not a Big Number “+ “2nd Condition is not true”)
else :
print (“A is Not a Big Number “+ “1st Condition is not true”)
——————————-
Using Compound Condition

a, b, c, d = 100, 90, 70, 50;

if ((a>b) and (a>c) and (a>d)):
print (“A is a Big Number”)
else:
print (“A is Not a Big Number”)
——————————-
Nested if Condition vs. Compound Condition

In Nested, if condition we can write multiple else parts, whereas in the compound condition we can write single else part only.


Problem: Find the biggest variable (Integer variables) among 4 variables

Use Compound Condition and else if…

a, b, c, d = 100, 90, 70, 50;

if ((a>b) and (a>c) and (a>d)):
print (“A is a Big Number”)
elif ((b>a) and (b>c) and (b>d)):
print (“B is a Big Number”)
elif ((c>a) and (c>b) and (c>d)):
print (“C is a Big Number”)
else:
print (“D is a Big Number”)

Python Conditional Statements


Python Syllabus

Python Step by Step Video Tutorial

Python Tutorial

Follow me on social media:

3 thoughts on “Python Conditional Statements”

Leave a Comment