Python: Conditional statements

Python support singular IF,  If-ELSE, IF-ELIF-ELIF-ELSE statements. Sample formats are as below.

if b > a:
    print("b is greater than a")

if b > a:
    print("b is greater than a")
    print("A is greater than b")

if A == 10:
    print("A is 10")
elif A == 20:
    print("A is 20")
    print("A is neither 10 nor 20")

Conditional statements can be used as nested statements.

Boolean Expressions

Boolean expression is an expression that is either True or False. Python supports usual Boolean expressions. examples as below

Test Symbol Example
Equality == a == b
Inequality != a != b
Less than < a < b
Less than or equal <= a <= b
Greater than > a >b
Greater than or equa >= a >= b

Logical Operatiors

Python supports usual logical operators. examples as below. Symantic of these operator is similar to their English language meaning.


Operator Symbol Example
and and (a and b)
or or (a or b)

Try and Except

Since python is runtime programming language, many times errors are detected while running the job and whole program might fail due to any error. In such cases, you can use try..except.

Try .. except statement is like insurance in python


input_age =input(" Enter your age : ")

# days=int(input_age)*365

days= int(input_age)*365
print("days inside try and except :" , days)
except Exception as e:
print(" Error :", e)

Leave a Reply
