OneBite.Dev - Coding blog in a bite size

if else conditional statement in python

Code snippet on how to if else conditional statement in python

if condition:  
    statement1
else:  
    statement2

This code contains an if-else statement that checks a condition and executes either one of the two statements. The code starts with the if keyword followed by a condition. Then in the same line, the code is closed with a colon (:). This is followed by an indented block of code that runs when the condition is true. After that, the else keyword is used followed by a colon. This is followed by another block of code that is executed when the condition is false. The code ends after the else block.

Basically, the if-else statement is used to make a decision based on the result of the evaluation of the condition. The condition is normally a comparison expression that can be either true or false. Depending on the evaluation result, the code block associated with the if statement is executed or the code block that follows the else keyword is executed.

python