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

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved