Curriculum
In this tutorial, you will learn about if statement and if-else statements in Python along with some examples demonstrating its usage.
Conditional statements in python perform operations and check the user-defined conditions constrained to Boolean values and return the resulting value(s) or statements. Python supports the following conditional statements:
The “If statement or simple” if in python is a conditional statement that checks a certain condition and if true it executes the statement inside it. If the condition is false, it jumps out of the loop.
Syntax:
if condition: Statements
Example program using if statements
Input:
a=57 b=57 if a==b: print(“ A and B has same values”)
Output:
A and B has same values
So, what if the ‘if’ condition is not met but you need to perform another operation? That’s where the if..else conditional statement comes to the rescue.
The ‘if…else statement’ is a conditional statement that checks a certain condition and if true it executes the statement inside it and if the condition is false, then the statement is else section is executed.
Syntax:
if condition: Statements else: Statements
Example problem using the if…else statements
a=57 b=75 if a==b: print("A and B has same values") else: print("A and B does not have the same values")
Output:
A and B does not have the same values