Questions

How do you write an If Statement in Python?

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

Related Questions

Related Topics

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.