Python IF ELSE is a Decision Condition where a particular condition is true, then that block of code is executed, otherwise, it breaks or different block of code is executed. The condition can be made using Operators such as Assignment, Comparison, Logical, Membership, Identity Operators
IF ELSE Syntax
If ( Condition ) : Statement (s)
If statement can be written using
if keyword. For example –
a = 33 b = 200 if b > a: print("b is greater than a") # Output: b is greater than a
IF ELSE Indentation
Python relies on indentation, using whitespace, to define the scope in the code. Other programming languages usually use curly-brackets for this purpose.
a = 33 b = 200 if b > a: print("b is greater than a") # Output: Indentation Error Please put Indentation
elif the keyword is pythons way of saying “if the previous conditions were not true, then try this condition”.
x = 100 if (x==10): print("X is equal to 10") elif (x==100): print("X is equal to 100")
else keyword catches anything which isn’t caught by the preceding conditions.
x = 100 if (x==10): print("X is equal to 10") elif (x==100): print("X is equal to 100") else: print("X is not equal 10")
Nested If Else Statement
If Else means Condition inside Condition. For Example:
y = "Hello"
print("X = 100 & y = Hello")
Whats Next – Python While Loop