The Python if statement is used to determine whether or not a specific statement or set of statements will be performed. There are various methods to write an if statement in a Python program. These are –
- Python if statement
- Python if…else statement
- Python if…elif…else statement
- Python nested if statement
- Python shorthand if statement
- Python shorthand if…else statement
The simplest of them is the if statement. Its syntax is as follows –
if condition:
statements(s)
Example
# Python program to check if 2 numbers are equal or not using if statement
a = 25
b = 25
if a == b:
print('Values are equal')
Output
Values are equal
Another most common if statement in Python is the if…else statement.
Example
# Python program to demonstrate if…else
ch = input('Enter any character: ')
vow = 'aeiouAEIOU'
if ch in vow:
print("Entered character is a vowel")
else:
print("Entered character is not a vowel")
Output
Enter any character: f
Entered character is not a vowel
Table of content