What is an ' if ' Statement ?
In Python, an if statement is used to execute a block of code only when a condition is true.
It allows decision-making in programs.
Syntax :
Python
if condition: statement(s) How it Works ?
The if statement evaluates a condition (an expression that results in True or False). If the condition is true, the code block inside the if statement is executed. If the condition is false, the code block is skipped.
Indentation :
Indentation defines the block of code.
Python
num = 10 if num > 0: print("Positive number") # inside if block print("End") # outside if block Example :
Python
is_logged_in = True if is_logged_in: print("Welcome user") Python
marks = 75 if marks >= 50: print("You passed")