Conditional Statements#
Conditional statements in Python are programming constructs that allow you to execute different blocks of code based on whether certain conditions are True or False. They are used to make decisions in code, allowing programs to choose between different actions based on certain conditions being met or not. In essence, conditional statements are used to control the flow of programs.
if Statements#
In Python, an if statement is a conditional statement that allows executing a code block only if a specified condition evaluates to True. It is the most basic form of conditional execution in Python.
The syntax of an if statement typically consists of the keyword if, followed by a condition, and a colon :. A code block of one or more statements is executed if the condition is True. The code block is indented underneath the if statement with four spaces.
if <condition>:
<statement>
<statement>
Here is a flow chart of an if statement:
TODO:IF STATEMENT FLOWCHART
Here are some examples of an if statement:
TODO:TRINKET BLOCKS
if…else Statements#
An if…else statement is a conditional statement that allows you to execute one block of code if a specified condition evaluates to True, and a different block of code if the condition evaluates to `False. It provides a way to create branches in the flow of execution based on the outcome of a condition.
The syntax is identical to an if statement Optionally, the “if” statement can be followed by an “else” statement, which contains another code block to be executed if the condition is false.
if <condition>:
<statement>
<statement>
elif:
<statement>
<statement>
Here is a flow chart of an if…else statement:
TODO:IF…ELSE STATEMENT FLOWCHART
Here are some examples of an if statement:
TODO:TRINKET BLOCKS
if…elif…else Statements#
if <condition>:
<statement>
<statement>
elif:
<statement>
<statement>
else:
<statement>
<statement>
Here is a flow chart of an if…else statement:
TODO:IF…ELIF…ELSE STATEMENT FLOWCHART
Here are some examples of an if statement:
TODO:TRINKET BLOCKS