Python Exercise 1 & 2 PDF
Python Exercise 1 & 2 PDF
1.Create a program that asks the user to enter their name and their age.
Print out a message addressed to them that tells them the year that they
will turn 100 years old.
Program:
name=input("Enter Your Name: ")
year=2020-age+100
Output:
2.Write a Python program which accepts the radius of a circle from the user
and compute the area.
Program:
from math import pi
radius=float(input("Enter The Radius of Circle: "))
area=pi*radius**2
print("The Area of Circle is: ",area)
Output:
3.Write a Python program which accepts the user's first and last name and
print them in reverse order with a space between them.
Program:
def reverse(s):
str=" "
for i in s:
str=i+str
return str
fname=input("Enter Your First Name: ")
lname=input("Enter Your Last Name: ")
print(reverse(fname)+""+reverse(lname))
Output:
Enter Your First Name: Vishal
lahsiV hkeraP
VISHAL PAREKH-RA1811003010517
Program:
from math import pi
radius=float(input("Enter The Radius of Sphere: "))
vol=(4/3)*pi*radius**3
print("The Volume of Sphere is: ",vol)
Output:
Enter The Radius of Sphere: 4
5.Write a program that prompts the user to enter an integer for today’s day
of the week (Sunday is 0, Monday is 1, ..., and Saturday is 6). Also prompt
the user to enter the number of days after today for a future day and
display the future day of the week.
Write a program that prompts the user to enter three integers and displays
them in increasing order. (using if..elif structure).
Program:
n=int(input("Enter Today's Day of Week: "))
num=int(input("Enter Number of Days After Today: "))
n=n+num
n=n%7
print("The Day Wil Be: ")
if(n==0):
print("Sunday")
elif(n==1):
print("Monday")
elif(n==2):
print("Tuesday")
elif(n==3):
print("Wednesday")
elif(n==4):
print("Thursday")
elif(n==5):
print("Friday")
else:
print("Saturday")
Output:
Enter Today's Day of Week: 4
6. Write a program that prompts the user to enter three integers and
displays them in increasing order. (using if..elif structure).
Program:
a=int(input("Enter First Number: "))
b=int(input("Enter Second Number: "))
c=int(input("Enter Third Number: "))
print("Numbers in Increasing Order are: ")
if(a<b and a<c):
if(b<c):
print(a,b,c)
else:
print(a,c,b)
elif(b<a and b<c):
if(a<c):
print(b,a,c)
else:
print(b,c,a)
else:
if(a<b):
print(c,a,b)
else:
print(c,b,a)
Output:
Enter First Number: 3
Program:
for i in range(1,6):
for j in range(1,i+1):
print(i,end="")
print("\r")
Output:
1
22
333
4444
55555
VISHAL PAREKH-RA1811003010517
8. Reverse a given number and return true if it is the same as the original
number.
Program:
n=int(input("Enter a Number: "))
m=n
x=0
while m>0:
x=x*10+(m%10)
m=m//10
if x==n:
print("True")
else:
print("False")
Output:
Enter a Number: 55
True
VISHAL PAREKH-RA1811003010517
9. Given a two list of ints create a third list such that should contain
only odd numbers from the first list and even numbers from the second list.
Program:
lst=input("Enter Numbers in List 1: ").split()
lst2=input("Enter Numbers in List 2: ").split()
lst3=[]
i=0
for i in lst:
if int(i)%2!=0:
lst3.append(i)
for i in lst2:
if int(i)%2==0:
lst3.append(i)
for i in lst3:
print(i)
Output:
Enter Numbers in List 1: 1 4 8
2
VISHAL PAREKH-RA1811003010517
10.Return the number of times that the string “Emma” appears anywhere in
the given string.
Program:
list1=input("Enter a String: ")
count=0
name="Emma"
count=list1.count(name)
print(count)
Output:
Enter a String: Emma and Emma's friend are going to the park
2
VISHAL PAREKH-RA1811003010517
Program:
fee=10000
for i in range(10):
fee=fee+fee*(5/100)
y=fee
total=fee
for i in range(4):
y+=y*(5/100)
total+=y
print("Ten Years Cost: "+str(fee))
print("Four Years Cost: ",total)
Ouput:
Ten Years Cost: 16288.946267774414
12.(Find the highest score) Write a program that prompts the user to enter
the number of students and each student’s score, and displays the highest
score. Assume that the input is stored in a file named score.txt, and the
program obtains the input from the file.
Program:
num=int(input("Enter No of Students: "))
list=[]
for i in range(num):
y=input("Enter Marks of Student: ")
list.append(y)
list.sort()
print("The Highest Mark is:", list[-1])
Output:
Enter No of Students: 5
13.(Find the two highest scores) Write a program that prompts the user to
enter the number of students and each student’s score, and displays the
highest and second highest scores.
Program:
num=int(input("Enter No of Students: "))
list=[]
for i in range(num):
y=input("Enter Marks of Student: ")
list.append(y)
list.sort()
print("The Highest Mark is:", list[-1])
print("The Second Highest Mark is:", list[-2])
Output:
Enter No of Students: 5
Program:
count=0
print("Numbers Between 100 and 1000 Which are Divisible by 5 and 6 are: ")
for i in range(100,1000):
if i%5==0 and i%6==0:
count+=1
print(i,end=" ")
if count==10:
count=0
print(" ")
Output:
Numbers Between 100 and 1000 Which are Divisible by 5 and 6 are:
120 150 180 210 240 270 300 330 360 390
420 450 480 510 540 570 600 630 660 690
720 750 780 810 840 870 900 930 960 990
VISHAL PAREKH-RA1811003010517
15.Write a program that reads an integer and displays all its smallest
factors, also known as prime factors. For example, if the input integer is
120, the output should be as follows: 2, 2, 2, 3, 5.
Program:
n=int(input("Enter Any Number: "))
i=2
while(n!=0):
if(n%i==0):
print(i,end=" ")
n=n/i
else:
i=i+1
Output:
Enter Any Number: 100
2 2 5 5
VISHAL PAREKH-RA1811003010517
16.Consider you are automate the Gratuity calculation using the following
a program that reads the subtotal and the gratuity rate and computes the
gratuity and total. For example, if the user enters 10 for the subtotal and
15% for the gratuity rate, the program displays 1.5 as the gratuity and
11.5 as the total.
Program:
a=float(input("Enter Subtotal: "))
b=float(input("Enter Gratuity Rate:"))
c=float((b*a)/100)
print("Gratuity is",c)
print("Total is",a+c)
Output:
Enter Subtotal: 10
Gratuity is 1.5
Total is 11.5
VISHAL PAREKH-RA1811003010517
17.Write a program that reads an integer between 0 and 1000 and adds all
the digits in the integer. Ensure the Entered number is between the limit
if not prompt the user to Re-enter the number.
Program:
print("Enter Number between 0 & 1000: ")
a=int(input())
sum=0
if a>0 and a<1000:
while a>0 and a<1000:
n=a%10
sum=sum+n
a=a//10
else:
print("Reenter Number:")
a=int(input())
Output:
Enter Number between 0 & 1000:
555
Program:
import random
x=random.randint(100,1000)
y=input("Enter a Number Between 100 & 1000: ")
z=str(x)
if x==int(y):
print("You won $10000")
elif sorted(z)==x:
print("You won $3000")
elif ((y[0] in z or y[1] in z or y[2] in z)):
print("You won $1000")
else:
print("You won nothing")
print("Random Number was: ",x)
Output:
Enter a Number Between 100 & 1000: 438
19. Create a Scissor and Rock simulator that plays the popular scissor-
rock-paper game. (A scissor can cut a paper, a rock can knock a scissor,
and a paper can wrap a rock.) The program randomly generates a number 0, 1,
or 2 representing scissor, rock, and paper. The program prompts the user to
enter a number 0, 1, or 2 and displays a message indicating whether the
user or the computer wins, loses, or draws.
Program:
import random
x=random.randint(0,3)
print("Rock:0, Paper:1, Scissors:2")
print("Enter a Choice: ")
y=int(input())
if y==0:
if x==0:
print("It's a Draw")
elif x==1:
print("You Loose")
else:
print("You Win")
elif y==1:
if x==1:
print("It's a Draw")
elif x==2:
print("You Loose")
else:
print("You Win")
elif y==2:
if x==2:
print("It's a Draw")
elif x==0:
print("You Loose")
else:
print("You Win")
Output:
Rock:0, Paper:1, Scissors:2
Enter a Choice:
You Win
VISHAL PAREKH-RA1811003010517
20. Write a program that reads integers, finds the largest of them, and
counts its occurrences. Assume that the input ends with number 0. Suppose
that you entered 3 5 2 5 5 5 0; the program finds that the largest number
is 5 and the occurrence count for 5 is 4. (Hint: Maintain two variables,
max and count. The variable max stores the current maximum number, and
count stores its occurrences. Initially, assign the first number to max and
1 to count. Compare each subsequent number with max. If the number is
greater than max, assign it to max and reset count to 1. If the number is
equal to max, increment count by 1.)
Program:
lst = []
num = int(input('How many numbers: '))
for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Maximum element in the list is:", max(lst))
def most_frequent(lst):
counter=lst.count(max(lst))
print("Maximum element occurrence in list is: ",counter)
most_frequent(lst)
Output:
How many numbers: 5
Enter number 1
Enter number 2
Enter number 2
Enter number 5
Enter number 2
Program:
print("The Perfect Numbers are: ")
for i in range(2,10000):
sum=0
for j in range(1,i):
if(i%j==0):
sum+=j
if(sum>i):
break
if(sum==i):
print(i)
Output:
The Perfect Numbers are:
28
496
VISHAL PAREKH-RA1811003010517
22.(Twin primes) Twin primes are a pair of prime numbers that differ by 2.
For example, 3 and 5, 5 and 7, and 11 and 13 are twin primes. Write a
program to find all twin primes less than 1,000.
Program:
def checkprime(a):
if(a==2):
return 1
for i in range (2,(a//2)+1):
if(a%i==0):
return 0
return 1
print("The Twin Prime Numbers are: ")
num=2
for i in range (3,1000):
if checkprime(i):
if i-num==2:
print("{} {}".format(num,i))
else:
num=i
Output:
The Twin Prime Numbers are:
3 5
11 13
17 19
29 31
41 43
59 61
71 73
101 103
107 109
137 139
149 151
179 181
191 193
197 199
227 229
239 241
269 271
281 283
311 313
347 349
419 421
431 433
461 463
521 523
569 571
599 601
617 619
641 643
659 661
809 811
821 823
827 829
857 859
881 883
VISHAL PAREKH-RA1811003010517
Program:
def checkprime(a):
if(a==2):
return 1
for i in range (2,(a//2)+1):
if(a%i==0):
return 0
return 1
n=int(input("Enter Number: "))
i=2
x=3
print("The Mersenne Prime Numbers are: ")
while x<=n:
if checkprime(x):
print(x)
i+=1
x=(2**i)-1
Output:
Enter Number: 5
3
VISHAL PAREKH-RA1811003010517
Program:
fee=10000
for i in range(0,10):
fee+=5*fee/100
print("Fee After 10 Years: ${0:.2F}".format(fee))
total=fee
for i in range(0,4):
fee+=5*fee/100
total+=fee
print("Fee For 4 Years of College After 10 Years: ${0:.2F}".format(total))
Output:
Fee After 10 Years: $16288.95
25.(Find the two highest scores) Write a program that prompts the user to
enter the number of students and each student’s score, and displays the
highest and second highest scores.
Program:
n=int(input("Enter Number Of Students: "))
li=[]
for i in range(0,n):
x=int(input("Enter Student {}'s mark: ".format(i+1)))
li.append(x)
li.sort()
print("Highest Score: {}\nSecond Highest Score: {}".format(li[-1],li[-2]))
Output:
Enter Number of Students: 5