Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views4 pages

If Else Assignment

Uploaded by

sudhir
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)
13 views4 pages

If Else Assignment

Uploaded by

sudhir
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/ 4

8/30/22, 10:35 AM Assignment 3 (if-else) - Jupyter Notebook

In [1]: 1 #1. Take two int values from user and print greatest among them
2 num1 = eval(input("enter a number: "))
3 num2 = eval(input("enter a number: "))
4 if num1>num2:
5 print("num1 is greatest number")
6 else:
7 print("num2 is greatest number")

enter a number: 5
enter a number: 42
num2 is greatest number

In [8]: 1 '''2. A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years.
2 Ask user for their salary and year of service and print the net bonus amount.'''
3 salary = eval(input("enter the employee salary: "))
4 service = eval(input("enter the employee service in years: "))
5 if service>=5:
6 print("Eligible for bonus: total salary after bonus", salary+(salary*0.05))
7 else:
8 print("Not Eligible for bonus")
9

enter the employee salary: 75000


enter the employee service in years: 5.5
Eligible for bonus: total salary after bonus 78750.0

In [20]: 1 # 3. A shop will give discount of 10% if the cost of purchased quantity is more than 1000.
2 #Ask user for quantity
3 # Suppose, one unit will cost 100.
4 #Judge and print total cost for user.
5 ​
6 quantity = eval(input("number of products purchased: "))
7 cost = eval(input(" cost per quantity: "))
8 if quantity>1000:
9 print("total cost on purchase after adding discount is: ", ((cost*quantity)-(cost*quantity)*0.01))
10 else:
11 print("total cost on purchase after adding discount is: ", cost*quantity)
12
13
14 ​
15 ​

number of products purchased: 1002


cost per quantity: 100
total cost on purchase after adding discount is: 99198.0

In [38]: 1 #4. A school has following rules for grading system:


2 '''a. Below 25 - F
3 b. 25 to 45 - E
4 c. 45 to 50 - D
5 d. 50 to 60 - C
6 e. 60 to 80 - B
7 f. Above 80 - A
8 '''
9 m = eval(input("Marks of students: "))
10 ​
11 if m<25:
12 print("Grade is : F")
13 elif(m>=25 and m<45):
14 print("Grade is : E")
15 elif(m>=45 and m<50):
16 print("Grade is : D")
17 elif(m>=50 and m<60):
18 print("Grade is : C")
19 elif(m>=60 and m<80):
20 print("Grade is : B")
21 elif(m>80):
22 print("Grade is : A")
23 ​
24

Marks of students: 83
Grade is : A

localhost:8888/notebooks/Assignment 3 (if-else).ipynb 1/3


8/30/22, 10:35 AM Assignment 3 (if-else) - Jupyter Notebook

In [47]: 1 #5. Take input of age of 3 people by user and determine oldest and youngest among them.
2 p1=eval(input("Enter the age of person1: "))
3 p2=eval(input("Enter the age of person2: "))
4 p3=eval(input("Enter the age of person3: "))
5 if (p1>p2 and p1>p3):
6 print("person1 is oldest")
7 if p1<p2:
8 print("person1 is youngest")
9 else:
10 print("person2 is youngest")
11 elif (p2>p1 and p2>p3):
12 print("person2 is oldest")
13 if p2<p3:
14 print("person2 is youngest")
15 else:
16 print("person3 is youngest")
17 elif (p3>p1 and p3>p2):
18 print("person3 is oldest")
19 if p3<p1 or p2<p3:
20 print("person3 is youngest")
21 else:
22 print("person1 is youngest")
23

Enter the age of person1: 40


Enter the age of person2: 20
Enter the age of person3: 50
person3 is oldest
person3 is youngest

In [3]: 1 #6. Write a program to print absolute vlaue of a number entered by user.
2 num = eval(input("ENter a num :"))
3 if num == abs(num):
4 print("absolute value of num is =",num)

ENter a num :-5

In [13]: 1 7.# A student will not be allowed to sit in exam if his/her attendence is less than 75%.
2 '''Take following input from user
3 Number of classes held
4 Number of classes attended.
5 And print
6 percentage of class attended
7 '''
8 classes = eval(input("Number of classes held : "))
9 attendence =eval(input("enter the percentage of attendence : "))
10 if attendence<75:
11 print("He/she is not allowed to write exam")
12 else:
13 print("He/she is allowed to write exam")
14 print("% of classes attended :", (classes-attendence)*100/attendence)

Number of classes held : 100


enter the percentage of attendence : 76
He/she is allowed to write exam
% of classes attended : 31.57894736842105

localhost:8888/notebooks/Assignment 3 (if-else).ipynb 2/3


8/30/22, 10:35 AM Assignment 3 (if-else) - Jupyter Notebook

In [6]: 1 """15.Write a program to accept a number from 1 to 7 and display the name of the
2 day like 1 for Sunday , 2 for Monday and so on."""
3 num=eval(input("Enter the number"))
4 if num==1:
5 print("Sunday")
6 elif num==2:
7 print("monday")
8 elif num==3:
9 print("tuesday")
10 elif num==4:
11 print("wednesday")
12 elif num==5:
13 print("thursday")
14 elif num==6:
15 print("friday")
16 elif num==7:
17 print("saturday")

Enter the number7


saturday

In [4]: 1 '''16.Accept any city from the user and display monument of that city.
2 City Monument
3 Delhi Red Fort
4 Agra Taj Mahal
5 Jaipur Jal Mahal'''
6 ​
7 city=input("Enter the city name=")
8 if city == 'delhi':
9 print(f"monument of the {city} is Red fort")
10 elif city=='agra':
11 print(f"monument of the {city} is Tajmahal")
12 elif city=='jaipur':
13 print(f"monument of the {city} is Jal Mahal")
14 ​
15 ​

Enter the city name=agra


monument of the agra is Tajmahal

In [8]: 1 #Write a program to check whether a number entered is three digit number or not.
2 num=input("Enter the number")
3 if len(num)==3:
4 print("Entered num is three digit num")
5 else:
6 print("Entered num is not three digit num")

Enter the number842


Entered num is three digit num

localhost:8888/notebooks/Assignment 3 (if-else).ipynb 3/3


8/23/22, 6:31 AM Assignment 2 - Jupyter Notebook

In [9]: #⦁ How would you check if each word in a string begins with a capital letter?
#it possible to convert first letter of each word in a string can be conv
#example
str1 = "data science and python"
str1.title()

Out[9]: 'Data Science And Python'

In [13]: #⦁ Check if a string contains a specific substring


str1 = "data SCIENCE and python"
if "SCIENCE" in str1:
print("String SCIENCE exist")
else :
print("String SCIENCE doesnot exist")

String SCIENCE exist

In [28]: #⦁ Find the index of the first occurrence of a substring in a string


str1 = "data SCIENCE and python data SCIENCE and python"
if "SCIENCE" in str1:
print("String SCIENCE exist")
else :
print("String SCIENCE doesnot exist")

str1.find("SCIENCE",0,50)

String SCIENCE exist

Out[28]: 5

In [32]: #Count the number of a specific character in a string


str1 = "data SCIENCE and python data SCIENCE and python data SCIENCE and python d
str1.count("a")

Out[32]: 12

In [35]: #Capitalize the first character of a string


str1 = "data SCIENCE and python"
str1.title()

Out[35]: 'Data Science And Python'

localhost:8888/notebooks/Assignment 2.ipynb 1/1

You might also like