Python if condition is one of the widely used control flow statement. It is used to execute specific set of code based on some condition. The basic syntax of Python if is like below.
if "Expression" : "Statement block"
First it evaluates the expression and if it is true, it executes the statements inside the if block. It continues the regular flow without entering the if block if the condition is evaluated as false.
If block is used along with an else block. This is used to control the program flow in one direction if the condition is true and to another direction if the condition is false.
x = 3 if x == 2: print("Value of x is 2") else: print("I can't find the value of x")
Elif is a combination of if and else. Lets say we have multiple conditions to validate, Elif is the right option to use. Take a look at the below example that uses if, elif and else. Here the variable “x” takes a value and we are printing what it is using if elif and else conditions.
x = 3 if x == 2: print("Value of x is 2") elif x == 3: print("Value of x is 3") else: print("I can't find the value of x")