[go: up one dir, main page]

0% found this document useful (0 votes)
14 views1 page

2 - Aug - Condition Statement

The document outlines three types of conditional statements in Python: if-else, if-elif-else, and nested if-else. It provides examples and explanations for each type, demonstrating how they evaluate conditions and execute corresponding code blocks. Additionally, it includes various programming exercises to apply these concepts in practical scenarios.

Uploaded by

trisha12062002
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)
14 views1 page

2 - Aug - Condition Statement

The document outlines three types of conditional statements in Python: if-else, if-elif-else, and nested if-else. It provides examples and explanations for each type, demonstrating how they evaluate conditions and execute corresponding code blocks. Additionally, it includes various programming exercises to apply these concepts in practical scenarios.

Uploaded by

trisha12062002
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/ 1

There are three types of conditional statements.

if-else

if-elif-else

nested if-else
In [ ]:

In control statements, The if statement is the simplest form. It takes a condition and evaluates to either True or False.

If the condition is True, then the True block of code will be executed, and if the condition is False, then the block of code is skipped, and The controller moves to the next line

In [ ]:

In [1]: number = 6
if number > 5: # condition
print(number * number) #statement
print('Next lines of code')

36
Next lines of code

In [ ]:

In [ ]:

if else
The if-else statement checks the condition and executes the if block of code when the condition is True, and if the condition is False, it will execute the else block of code.
In [4]: password = input('Enter password ')

if password == "python":
print("Correct password")
else:
print("Incorrect Password")
print('Next lines of code')

Enter password python


Correct password
Next lines of code

In [ ]:

In [ ]:

In [5]: number = 6
if number > 5: # condition
print(number * number) # statement
else:
print("nothing to print")
print('Next lines of code')

36
Next lines of code

In [ ]:

In [ ]:

if elif else
In Python, the if-elif-else condition statement has an elif blocks to chain multiple conditions one after another. This is useful when you need to check multiple conditions.

With the help of if-elif-else we can make a tricky decision. The elif statement checks multiple conditions one by one and if the condition fulfills, then executes that code.

In [ ]: choice = int(input("enter your choice = "))


if choice == 1:
print("python")
elif choice == 2:
print("c++")
elif choice == 3:
print("java")
elif choice == 4:
print("dotnet")
else:
print("no programming language")
print("next line of code")

In [ ]:

In [6]: choice = int(input("enter your choice = "))


if choice == 1:
print("python")
if choice == 2:
print("c++")
if choice == 3:
print("java")
if choice == 4:
print("dotnet")
else:
print("no programming language")
print("next line of code")

enter your choice = 1


python
no programming language
next line of code

In [ ]:

In [ ]:

Nested if-else statement


In Python, the nested if-else statement is an if statement inside another if-else statement. It is allowed in Python to put any number of if statements in another if statement. Indentation is the only way to
differentiate the level of nesting. The nested if-else is useful when we want to make a series of decisions.
In [ ]: a=10
b=20
if(a == 10):
if(b == 20):
print("Inside B")
else:
print("Outside B")
else:
print("Outside A")
print("next line of code")

In [ ]:

In [ ]: a = 9
b = 8
c = 3
d = 2
if a>b:
print("a is smaller than b")
if c>d:
print("c is greater than d")
else:
print("c is smaller than d")
else:
print("a is greater than b")
print("Rest of the code")

In [ ]:

In [ ]:

In [ ]: number = 5

# outer if statement
if (number >= 0):
# inner if statement
if number == 0:
print('Number is 0')

# inner else statement


else:
print('Number is positive')

# outer else statement


else:
print('Number is negative')

# Output: Number is positive

In [ ]:

Q1. write a programme to check whether a person is eligible for voting or not.
In [ ]:

Q2. write a programme to check whether a person is senior citizen or not.


In [ ]:

Q3. write a programme to check whether a number entered by user is even or odd.
In [ ]:

Q4. Seation plan according to roll no

roll no 1 to 30 -------- > A section

if rollno is greater than 30 ----- > B section

if rollno is greater than 60 ----- > C section

roll no greater than 90 to upto 120 ----- > D section

Remaining student -----------> E section


In [ ]:

In [ ]: roll_no = int(input("enter your rollno = "))


if roll_no>0 and roll_no<=30:
print("A section ")
elif roll_no>30 and roll_no<=60:
print("B section")
elif roll_no>60 and roll_no<=90:
print("c section")
elif roll_no>90 and roll_no <=120:
print("d secton")
else:
print("e section")
print("next line of code")

In [ ]:

Q.5 A company decided to give bonus of 5% to employee if his/her year of service is


more than 5 years. Ask user for their salary and year of service and print the net bonus
amount.
In [ ]:

Question 6:

per is greater or equal 70 = first division

per is greater or equal 50 = second division

per is greater or equal 35 = third division

else = fail
In [ ]: A = int(input("enter the percentage : "))
if A>=70:
print('first division')
elif A>=50 and A<=70:
print('second divison')
elif A>=35 and A<=50:
print("third divison")
else:
print("fail")

In [ ]:

In [16]: per = int(input("enter the per = "))


if per>=70 and per<=100:
print("first division")
elif per>=50 and per<70:
print("second division")
elif per>=35 and per<50:
print("third division")
else:
print("fail")

enter the per = 35


third division

In [ ]:

Question 7 :

ticket price is 1000

age 6 to 14 = free

15 to 25 = 50% dis

26 to 36 = 20% dis

else = 10% dis

print the net ticket price


In [8]: age=int(input("enetr your age to check for discount:-"))
if (age>=6 and age<=14):
print("your ticket is free....")
elif (age>=15 and age<=25):
print("you have 50 percent discount on your ticket the net ticket price is")
print(1000/2)
elif (age>=26 and age<=36):
print("you got 20 percent discount on your ticket and net ticket price is ")
print((20/100)*10000)
else:
print("you got 10 percent discount on your ticket and the net ticket price is")
print((10/100)*1000)

enetr your age to check for discount:-22


you have 50 percent discount on your ticket the net ticket price is
500.0

In [ ]:

In [7]: a=int(input(" enter your age:"))


price=1000
print("ticket price is:")
if(a>=6 and a<=14):
price=0
print(price)
elif(a>=15 and a<=25):
price=price-(price*.5)
print(price)
elif( a>=26 and a<=36):
price=price-(price*.2)
print(price)
else:
price=price-(price*.1)
print(price)

enter your age:44


ticket price is:
900.0

In [ ]:

In [6]: ticket = 1000


age = int(input("enter your age = "))
if age>5 and age<15:
print("free")
elif age>14 and age<26:
net_ticket_price = (ticket-(ticket*0.5))
print("net ticket price is ", net_ticket_price)
elif age>25 and age<37:
net_ticket_price = (ticket-(ticket*0.2))
print("net ticket price is ", net_ticket_price)
else:
net_ticket_price = (ticket-(ticket*0.1))
print("net ticket price is ", net_ticket_price)

enter your age = 33


net ticket price is 800.0

In [ ]:

Question 8 :

Design a simple calculator using conditional statement

take two integer input from the user and one operation sign

"+" = a+b

"-" = a-b

"" = ab

"/" = a/b

else = invalid operator


In [ ]:

In [ ]:

9.Take values of length and breadth of a rectangle from user and check if it is square or
not.
In [5]: leng = int(input("enter length = "))
bre = int(input("enter breadth = "))
if leng==bre:
print("square")
else:
print("rectangle")

enter length = 5
enter breadth = 5
square

In [ ]:

10 Take two int values from user and print greatest among them.
In [4]: num1=int(input("enter first number:"))
num2=int(input("enter second number:"))
if(num1>num2):
print("FIRST NUMBER IS GREATER")
elif(num1<num2):
print("SECOND NUMBER IS GREATER")
else:
print("THEY ARE EQUAL")

enter first number:6


enter second number:6
THEY ARE EQUAL

In [ ]:

In [3]: num1 = int(input("Enter first number: "))


num2 = int(input("Enter second number: "))
#Compare both the number
if num1>num2:
print("num1 is greatest")
elif num1<num2:
print("num2 is greatest")
else:
print("both equal")

Enter first number: 5


Enter second number: 6
num2 is greatest

In [ ]:

11. 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.


In [2]: marks=int(input("enter your marks to check grade:-"))
if (marks>80):
print("your grade is A")
elif(marks>60 and marks<=80):
print("your grade is B")
elif(marks>50 and marks<=60):
print("your grade is C")
elif(marks>45 and marks<=50):
print("your grade is D")
elif(marks>25 and marks<=45):
print("your grade is E")
else:
print("your grade is F")

enter your marks to check grade:-66


your grade is B

In [ ]:

12. Take input of age of 3 people by user and determine youngest among them.
In [ ]: a=int(input("enter the age of first person:-"))
b=int(input("enter the age of second person:-"))
c=int(input("enter the age of third person:-"))
if(a<b and a<c):
print("person 1 is youngest")
elif(b<c and b<a):
print("person 2 is youngest")
else:
print("person c is youngest")

In [ ]:

In [22]: a=int(input("enter the age of first person:-"))


b=int(input("enter the age of second person:-"))
c=int(input("enter the age of third person:-"))
if(a<b and a<c):
print("person 1 is youngest")
elif(b<c and b<a):
print("person 2 is youngest")
elif(c<a and c<b):
print("person 3 is youngest")
elif(a==b or b==c or a==c):
if(a==b and a!=c):
print("person 1 and 2 have same age")
elif(a==c and a!=b):
print("person 1 and 3 have same age")
elif(b==c and b!=c):
print("person 2 and 3 have same age")
else:
print("all have same age")
else:
print("all have same age ")

enter the age of first person:-4


enter the age of second person:-4
enter the age of third person:-4
all have same age

In [ ]:

In [1]: age1=int(input('enter age1: '))


age2=int(input('enter age2: '))
age3=int(input('enter age3: '))
if age1<age2 and age1<age3:
print('age1 is youngest')
elif age2<age1 and age2<age3:
print('age2 is youngest')
elif age3<age1 and age3<age2:
print('age3 is youngest')
else:
print("all are equal")

enter age1: 6
enter age2: 5
enter age3: 5
all are equal

In [ ]:

You might also like