How to execute an If statement in Python

What is an if statement?

A computer program often requires a decision or a branch within a code to account for how the code operates under different inputs or conditions.

Within the Python programming language, the simplest and sometimes the most useful way of creating a branch within your program is through an if statement.

Syntax

The syntax for writing an if statement is as follows:

Code

Let’s take a look at an example implementing an if statement:

#creating variables "a" and "b"
a = 500
b = 200
#if condition to check whether "a" is greater than "b"
if(a > b):
print "a is greater than b"

The illustration below explains the code above.

Flowchart of if statement
1 of 4
New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved