Conditions in Python

Python : Conditions

Conditions are used to check if a particular statement is true or false. The most common way of using conditions in Python is through the if statement.

If statement in Python

if condition:
    statement(s)
x = 5
if x > 0:
    print("x is positive")

if-else statement in Python

if condition:
    statement(s) if condition is true
else:
    statement(s) if condition is false

elif statement in python

if condition1:
    statement(s) if condition1 is true
elif condition2:
    statement(s) if condition2 is true
...
else:
    statement(s) if all conditions are false

Example-

x = 5
if x < 0:
    print("x is negative")
elif x == 0:
    print("x is zero")
else:
    print("x is positive")

Overall, conditions are an important part of Python programming as they allow us to make decisions and execute different blocks of code based on those decisions.

Note: We can't use turnary operator directly in Python similar to other languages like javascript etc.

Introduction to Loops in Python