ConditionalStatements Python
ConditionalStatements Python
Samir V
What are Conditional Statements?
Samir V
Samir V
If statements -
Syntax:
if ( condition):
Statement(s)
Eg –
If ( age>=18):
Print(“You are eligible for vote”)
If(age<0):
Print(“You entered Negative Number”)
Samir V
if - else statements
When condition becomes true then executes the block given below it.
If condition evaluates result as false, it will executes the block given below else.
Syntax:
if ( condition):
statement(s)
else:
statement(s)
if(n%2==0):
print(N,“ is Even Number”)
else:
print(N,“ is Odd Number”)
Samir V
(if-elif-else) statements -
Syntax:
if ( condition-1):
Statement(s)
elif (condition-2):
Statement(s)
elif (condition-3):
Statement(s)
else:
Statement(s)
Samir V
num=int(input(“Enter Number: “))
If ( num>0):
Print(“You entered positive number”)
elif ( num<0):
Print(“You entered Negative number”)
else:
Print(“You entered Zero ”)
Samir V
Python Nested if statements –
It is the construct where one if condition take part inside of other if condition.
Syntax –
if ( condition-1):
if (condition-2):
……………
……………
else:
……………
……………
else:
…………………..
…………………..
Samir V
num=int(input(“Enter Number: “))
If ( num<=0):
if ( num<0):
Print(“You entered Negative number”)
else:
Print(“You entered Zero ”)
else:
Print(“You entered Positive number”)
Samir V
Checking Multiple Conditions
Logical operators are used to combine multiple conditional are “and”, “or”, and
“not”.
Samir V
Using “and” to check multiple conditions
Logical operator “and” will return True as long as all the conditions is True.
Example:
#Check if triangle is equilateral triangle
Samir V
Using “or” to check multiple conditions
Logical operator “or” will return True as long as at least one of the conditions is
True.
Example:
#Check if triangle is Isosceles triangle
Samir V
The 'not' operator in Python is a logical operator that returns the inverse of the
value of the operand it precedes.
If the operand is True, 'not' will return False, and if the operand is False, 'not' will
return True.
a = 10
Samir V