if

In Python, the if keyword starts a conditional statement that allows you to execute a code block only if a specified Boolean condition is true. This keyword is fundamental for controlling the flow of your programs.

Python if Keyword Examples

Here’s a quick example of how to use the if keyword:

Python
>>> x = 10

>>> if x > 5:
...     print("x is greater than 5")
...
x is greater than 5

In this example, the condition x > 5 is evaluated. Because it’s true, the indented code block under the if statement runs, and you get the output "x is greater than 5".

Python if Keyword Use Cases

  • Making decisions in your code based on conditions
  • Executing different blocks of code depending on varying conditions
  • Handling input validation and error checking

Tutorial

Conditional Statements in Python

In this step-by-step tutorial you'll learn how to work with conditional ("if") statements in Python. Master if-statements and see how to write complex decision making code in your programs.

basics python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Jan. 6, 2025 • Reviewed by Dan Bader