Trusted answers to developer questions

How to execute an If statement in Python

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

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

RELATED TAGS

python
if condition
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?