The IF Statement
tags: #python/documentation/python_conditionals
What is a One-Way IF Statement?
A one-way if statement, also known as a simple or basic if statement, is a fundamental construct in programming that allows you to conditionally execute a block of code based on a specified condition. If the condition is:
True: The associated block of code is executed.False: The block is skipped, and the program moves onto the next statement
Basic Syntax
To create an IF conditional statement:
if condition:
# Code to be executed if the condition is True
The condition followed by the IF keyword is a boolean expression. Should the expression evaluate to True, the associated block of code is executed.
We need to end the IF statement with a : followed by indentation for the body of the conditional statement. The basic structure of an IF statement consists of:
- Header line that ends with a colon character.
- Indented block of code to be executed if condition is
True.
Python relies on indentation (whitespaces at the beginning of a line) to define a block of code and also for readability.
Python treats statements with the same indentation level as a single block of code.