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

Conditional Statement

The document discusses conditional statements and decision making in Python. It explains if, if-else and if-elif-else statements, providing syntax and examples. Key decisions like checking number sign, leap years and vote eligibility are demonstrated. Logical operators and indentation importance are also covered.

Uploaded by

Manushree Nayak
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)
44 views

Conditional Statement

The document discusses conditional statements and decision making in Python. It explains if, if-else and if-elif-else statements, providing syntax and examples. Key decisions like checking number sign, leap years and vote eligibility are demonstrated. Logical operators and indentation importance are also covered.

Uploaded by

Manushree Nayak
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/ 25

Conditional statement

How do you make decisions?


The basis of decision making depends upon
the availability of information and how we
experience and understand it.
‘information’ includes our past experience,
intuition, knowledge, and self-awareness.
We can’t make “good” decisions without
information because then we have to deal with
unknown factors and face uncertainty, which
leads us to make wild guesses, flipping coins, or
rolling a dice.
Having knowledge, experience, or insights given a
certain situation, helps us visualize what the
outcomes could be. and how we can achieve/avoid
those outcomes.
Scenario 1
You are locked inside a room with 3
doors to move out of the locked room and you
need to find a safe door to get your way out.
Behind the 1st door is a lake with a shark. The
2nd door has a mad psychopath ready to kill
with a weapon and the third one has a lion that
has not eaten since the last 2 months.
The answer is door number 3.
The reason being that since the lion has not
eaten for 2 months, he wouldn't have survived
till now and would already be dead .
This makes going out from gate 3 the correct
option.
If else condition
Why we need “if” condition?

Decision making is required when we want to


execute a code only if a certain condition is
satisfied.
The if…elif…else statement is used in Python for
decision making.
Python if Statement
Syntax:
if test expression:
statement(s)
Here, the program evaluates the test expression and will execute
statement(s) only if the test expression is True.

If the test expression is False, the statement(s) is not executed.

In Python, the body of the if statement is indicated by the


indentation. The body starts with an indentation and the first
unindented line marks the end.
Python interprets non-zero values as True. None and 0 are
interpreted as False.
Python if Statement Flowchart
Python supports the usual logical
conditions from mathematics:
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Example: Python if Statement

a = 33
b = 200
if b > a:
print("b is greater than a")
Indentation
Python relies on indentation (whitespace at the
beginning of a line) to define scope in the code. Other
programming languages often use curly-brackets for
this purpose.

Example
If statement, without indentation (will raise an error):
a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error
Questions

a) To check if a year is leap year


b) To check if a number is even or odd
c) To check if a number is divisible by 7
d) To check if a person is eligible for voting
Example: Python if Statement

# If the number is positive, we print an appropriate message


num = 3
if num > 0:
print(num, "is a positive number.")
print("This is always printed.")
num = -1
if num > 0:
print(num, "is a positive number.")
print("This is also always printed.")
Python if...else Statement
Syntax of if...else
if test expression:
Body of if
else:
Body of else

The if..else statement evaluates test


expression and will execute the body of if only
when the test condition is True.
If the condition is False, the body of else is
executed. Indentation is used to separate the
blocks.
Python if..else Flowchart
Example of if...else
# Program checks if the number is positive or
negative
# And displays an appropriate message
num = 3
# Try these two variations as well.
# num = -5
# num = 0
if num >= 0:
print("Positive or Zero")
else:
print("Negative number“)
1. Program to check whether the given number is odd
or even
2. Write a program to accept two integers and check
whether they are equal or not.
3. Write a program to read the age of a candidate and
determine whether it is eligible for casting his/her
own vote
1. Program to print the day name of the week using if-elif
condition.
2. Write a program to accept a number from the user and
check whether the number is divisible by 5 and 11.
3. Write a python program to take values of length and breadth
of a rectangle from user and check whether it is square or
not.
Python if...elif...else Statement
Syntax of if...elif...else
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
• The elif is short for else if. It allows us to check for
multiple expressions.
• If the condition for if is False, it checks the condition of
the next elif block and so on.
• If all the conditions are False, the body of else is
executed.
The if block can have only one else block. But it can have
multiple elif blocks.
#Check whether the number is positive, negative
number or zero

n=int(input("Enter a number:"))
if(n>0):
print(n,"is positive number")
elif(n==0):
print(n,"is equal to zero")
else:
print(n,"is a negative number")
#Program to reads two numbers and an arithmetic operator and
displays the computed result.

n1=int(input("Enter the first number:"))


n2=int(input("Enter the second number:"))
ch=input("Choose any one operator(+,-,/,*):")
if(ch=='+'):
print("Sum=",n1+n2)
elif(ch=='-'):
print("Division=",n1-n2)
elif(ch=='*'):
print("Multiplication=",n1*n2)
elif(ch=='/'):
print("Division=",n1/n2)
else:
print("Invalid")
#Python program to find the largest number among
the three input numbers
#take three numbers from user
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
if (num1 > num2) and (num1 > num3):
print("largest number is",num1)
elif (num2 > num1) and (num2 > num3):
print("largest number is",num2)
else:
print("The largest number is",num3)
'''A school has following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.'''

marks=float(input("Enter marks:"))
if marks<25:
print("F")
elif marks>=25 and marks<45:
print("E")
elif marks>=45 and marks<50:
print("D")
elif marks>=50 and marks<60:
print("C")
elif marks>=60 and marks<80:
print("B")
else:
print("A")
#Program to print the day name of the week using if-elif condition
day=int(input("enter the number between 1-7:"))
if day == 1:
print("Monday")
elif day == 2:
print("Tuesday")
elif day == 3:
print("Wednesday")
elif day == 4:
print("Thursday")
elif day == 5:
print("Friday")
elif day == 6:
print("Saturday")
elif day == 7:
print("Sunday")
else:
print("invalid")

You might also like