0% found this document useful (0 votes)
4 views

ConditionalStatements Python

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

ConditionalStatements Python

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Conditional statements

Samir V
What are Conditional Statements?

Conditional Statements are statements in Python that provide a choice for


the control flow based on a condition. It means that the control flow of the
Python program will be decided based on the outcome of the condition.

Now let us see how Conditional Statements are implemented in Python.

Samir V
Samir V
If statements -

This construct of python program consist of one if condition. When condition


becomes true then executes the block given below it.

Syntax:

if ( condition):
Statement(s)

Eg –

Age=int(input(“Enter Age: “))

If ( age>=18):
Print(“You are eligible for vote”)

If(age<0):
Print(“You entered Negative Number”)

Samir V
if - else statements

This construct of python program consist of one if condition with two


blocks.

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)

N=int(input(“Enter Number: “))

if(n%2==0):
print(N,“ is Even Number”)
else:
print(N,“ is Odd Number”)
Samir V
(if-elif-else) statements -

This construct of python program consist of more than one if condition.


When first condition evaluates result as true then executes the block
given below it.
If condition evaluates result as false, it transfer the control at else part to test
another condition. So, it is multi-decision making construct.

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.

This construct consist of more than one if condition. It is also multi-decision


making construct.

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

As we sometimes we need to check multiple conditions to come to conclusion.


Writing multiple if conditions one after another make program complex.
Alternatively we can combine multiple conditions on one if construct with help of
logical operators.

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

a= int(input("Enter angle of a triangle"))


b= int(input("Enter angle of a triangle"))
c= int(input("Enter angle of a triangle"))

if(a==60and b== 60 and c==60):


print("Its an equilateral triangle")
else:
print("Its not an 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

a= int(input("Enter length of a side of triangle"))


b= int(input("Enter length of a side of triangle"))
c= int(input("Enter length of a side of triangle"))

if(a==b or b==c or a==c):


print("Its an Isosceles triangle ")
else:
print("Its not an 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

if not ( a%5 == 0):


print("10 is divisible by 5")
else:
print("10 is not divisible by 5")

Samir V

You might also like