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 :
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.
num = 10 if num > 0: print("Positive number") # inside if block print("End") # outside if block Example :
is_logged_in = True if is_logged_in: print("Welcome user") marks = 75 if marks >= 50: print("You passed")