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 the statement

Usage of Decision making/Conditional Statements

1. Run a block of statements when a condition is true
2. Run a block of statements when a condition is true, otherwise, run another block of statements
3. Run a block of statements when a compound condition is true
4. Run a block of statements when a compound condition is true, otherwise, run another block of statements
5. Decide among several alternates (elif)
6. Run a block of statements when more than one condition is true (nested if)

1. Run a block of statements when a condition is true

Syntax:

if (condition):
Statement/s
.
.
xxxxxxx

Example:

a=100
b=900

if (a>b):
print(“A is Big Number”)

print(“Hello Python”)

2. Run a block of statements when a condition is true, otherwise, run another block of statements

Syntax:

if (condition):
Statement/s
.
.
else:
Statement/s
.
.
————————-
Example:

a=100
b=900

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

print (“Hello Python”)
————————–
a=10.345
b=10.344

if (a>b):
print(“A is Big Number”)
else:
print(“B is Big Number”)
————————
10 > 20
10.234 > 10345
“India” > “China”

ANSI
A to Z (65 to 90)
a to z (97 to 122)
0 to 9 (48 to 57)
—————–
100
“100”
10.23
“10.23”
———————
a=”India”
b=”china”

if (a>b):
print(“India is Big”)
else:
print(“China is Big”)
————————-
a=False
b=True

if (a>b):
print(“A is Big”)
else:
print(“B is Big”)
—————————-
a=100
b=100

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

3. Run a block of statements when a compound condition is true

Syntax:

if ((condition1) and “Or” or (condition2)):
Statement/s
.
.
Example:

a=100
b=90
c=80

if ((a>b) and (a>c)):
print (“A is Big Number”)
————————————–
a, b, c =100, 90, 80

if ((a>b) and (a>c)):
print (“A is Big Number”)
———————————–
a, b, c =100, 90, 800

if ((a>b) or (a>c)):
print (“A is Big Number”)

4. Run a block of statements when a compound condition is true, otherwise run another block of statements

Syntax:

if ((condition1) and “Or” or (condition2) and “Or” or (condition3)):
Statements
.
else:
Statement/s
.
——————–
Example:

a=100
b=90
c=800
d=70

if ((a>b) and (a>c) and (a>d)):
print (“A is Big Number”)
else:
print (“A is Not Big Number”)
————————-
a=100
b=90
c=800
d=70

if ((a>b) or (a>c) or (a>d)):
print (“A is Big Number”)
else:
print (“A is Not Big Number”)


5. 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: