0% found this document useful (0 votes)
0 views15 pages

Computer Science Project

The document provides a series of programming exercises covering various topics such as input/output, data handling, flow control, string manipulation, list manipulation, tuples, and dictionaries. Each question is followed by a sample answer in Python code, demonstrating how to perform specific tasks like calculating sums, generating random numbers, and manipulating strings and lists. The exercises aim to enhance programming skills and understanding of Python fundamentals.

Uploaded by

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

Computer Science Project

The document provides a series of programming exercises covering various topics such as input/output, data handling, flow control, string manipulation, list manipulation, tuples, and dictionaries. Each question is followed by a sample answer in Python code, demonstrating how to perform specific tasks like calculating sums, generating random numbers, and manipulating strings and lists. The exercises aim to enhance programming skills and understanding of Python fundamentals.

Uploaded by

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

Q1) Write a program to input a welcome message and print it.

Ans) message = input(“enter welcome message:”)


Print(“Hello,”,message)
Q2) WAP to obtain three numbers and print their sum. Ans)
num1=int(input(“Enter number 1:”))
num2=int(input(“Enter number 2:”))
num3=int(input(“Enter number 3:”))
sum=num1+num2+num3 print(“three
numbers are :”,num1,num2,num3)
print(“Sum is :”,Sum)
Q3) Write a program to obtain length sand breath of a rectangle and
calculate its area.
length =float(input(“Enter length of the rectangle:”))
Breath=float(input(“Enter breath of the rectangle:”))
Area=length*breadth
print(“rectangle specifications”)
print(“length=”,length,end=’ ‘)
print(“breadth=”,breadth)
print(“area=”,area)
Q4) Write a program to calculateBMI of a person.
Ans) weight_in_kg=float(input(“Enter weight in kg:”))
Height_in_meter=float(input(“Enter height in meters:”))
Bmi=weight_in_kg/(height_in_meter*height_in_meter)
Print(“Bmi is :”, Bmi)
Q5) Write a program to input a value in tonnes and convert it into
quintals and kilograms.
Ans) tonnes= float(input(“Enter tonnes:”))
Quintals=tonnes*10
Kgs=quintals*100
print(“tonnes:”,tonnes)
print(“Quintals:”,Quintals)
print(“Kgs:”,Kgs)
Q6) Write a program to input two numbers and swap them.
Ans) n1=int(input(“enter number1:”))
N2=int(input(“enter number2:”))
print(“original number:”,n1,n2)
n1,n2=n2,n1 print(“after
swapping:”,n1,n2)
b) Data Handling :-
Q1) Write a program to compute simple interest and compound interest.
Ans)P=float(input("Enter the Principal Amount : "))
R=float(input("Enter the Interest Rate : "))
T=float(input("Enter the time period(in years): "))
S_I=(P*R*T)/100
Amount1=P+S_I
print('Simple Interest =',S_I)
print("Amount(S.I.) =",Amount1)
import math
Amount2=P*math.pow((1+R/100),T) C_I=Amount2-
P
print('Compound Interest =',round(C_I,2))
print("Amount(C.I.) =",round(Amount2,2))
Q2) In a School fest, three randomly chosen students out of 100
students(having roll numbers 1-100) have to present bouquets to the
guests. Help the school authorities choose three students randomly.
Ans) import random student1=random.randint(1,100)
student2=random.randint(1,100)

student3=random.randint(1,100) print("3 choosen students


are :",student1, student2, student3)
Q3) Write a program to generate 3 random integers between 100 and
999 which is divisible by 5. Ans) import random
def int():
print(random.randrange(100,999,5))
int()
int()
int()
Q4) Write a program to calculate the radius of a sphere whose area
(4πr^2) is given. Ans) import math
area = float(input("Enter area of sphere: ")) r
= math.sqrt(area / (4 * math.pi))
print("Radius of sphere =", r)
Q5) Given a list containing these values [22, 13, 28, 13, 22, 25, 7, 13,
251]. Write code to calculate mean, median and mode of this list.
Ans) import statistics as stat list1 = [ 22, 13, 28, 13, 22, 25, 7, 13, 25]
list_mean = stat.mean(list1)

list_median = stat.median(list1) list_mode


= stat.mode(list1)
print("Given list:", list1)
print("Mean:", list_mean) print("Median:",
list_median) print("Mode:", list_mode)
Q6) A triangle has three sides a, b, c as 17, 23, 30. Calculate and
display its area using Heron's formula as s=(a+b+c)/2 , Area =
√𝒔(𝒔 − 𝒂)(𝒔 − 𝒃)(𝒔 − 𝒄) Ans) import math a,b,c=17,23,30
s=(a+b+c)/2
area = math.sqrt(s* (s-a) * (s-b)* (s-c))
print("Sides of triangle:", a, b, c)
print("Area:", area, "units square")

c) Flow of Control :-
Q1) Write a program to print absolute value of a number(using Loops):
Ans) num = int(input("Enter the Number : "))
if num>=0:
print("Absolute value : ",num)
else:
num*=-1
print("Absolute value :
",num)
Q2) Program to calculate the factorial of a number
Ans) n = int(input("Enter the number : ")) fact = 1 a
= 1 while a<= n: fact*=a a+=1 print("The
factorial of",n,'is',fact)
Q3) Write a table of any number given by the user.(till 10)
Ans) n = int(input("Enter the number : ")) for i in
range(11):
print(n,"X",i,'=',n*i)
Q3) Write a program that takes the name and age of the user as input
and displays a message whether the user is eligible to apply for a
driving liceanse or not.(eligible age is 18 years)
ANS) name =input('Enter your name : ')
age = int(input("Enter your age if
age >=18:
print("You are eligible for driving liceanse.")
else:
print("You are not eligible for driving liceanse.")
Q4) Write a program that prints minimum and maximum of five
numbers entered by the user.
Ans) n = int(input("Enter the number : "))
min,max=n,n
for i in range(4):
n = int(input("Enter the number : "))
if n>max: max = n if n<min:
min=n
print("Maximum number :",max) print("Minimum
number :",min)

Q5) Write a Program to print the following.


12345
1234
123
12
1
Ans) n = 5
s= 0 for i in
range(n,0,-1): for j
in range(1,s+1):
print(end=' ')
for j in range(1,1+i):
print(j,end='
') s+=2 print()
Q6) Make a 'guess the number' game in which the player is given 5
chances to guess the number between 10 and 50. Ans) import
random number = random.randint(10,50) chance = 0 while
chance<5:
guess=int(input("Enter any number between 10 and 50 : "))
if guess == number:
print('You found the number \nYou won !!!')
break
else:
chance += 1
if chance>5:
print("Hard luck :(\nBetter luck next time !!!")

d) String Manipulation :-
Q1) Write a program which reverses a string and stores the reversed
string in a new string.
Ans) str = input("Enter the string: ")
newStr = ""
for ch in str :
newStr = ch + newStr
print(newStr)
Q2) Program to read a string and display it in reverse order one
character per line.
Ans) String1 = input(“Enter a String: ”)
print(“The”,string1,”in reverse order is:”) length
= len(string1)
for a in range(-1, (-length-1), -1):
print(string[a] )
Q3) Write a program that inputs a string that contains a decimal number
and prints out the decimal part of the number. For instance, if 515.8059
is given, the program should print out 8059. Ans) s = input(‘Enter a
String(a decimal number): ’)
t = s.partition(‘.’) print(“Given
string:”,s) print(“part after
decimal”, t[2] )
Q4) Write a program to input two strings. If string1 is contained in
string2, then create a third string with first four characters of string2'
added with word 'Restore'.
Ans) s1 = input(“Enter string 1 :”)
s2 = input(“Enter string 2 :”)
print(“Original strings:”,s1,s3)
if s1 in s2:
s3 = s2[0:4]+ “Restore”
print(“Final string:” ,s1,s3)
Q5) Write a program to input a string and check if it’s a palindrome
string using a string slice.
Ans) s = input(“Enter a string:”)
If (s==s[::-1]): print(s,”is
a Palindrome.”) else:
print(s,”isNOT a Palindrome.”)
Q6) Write a program to input an integer and check if it contains any 0 in
it.
Ans) n = int(input(“Enter a number:”))
s = str(n)
if ‘0’ is s:
print(“There’s a 0 in”,n)
else:
print(“No 0 in”,n)

e) List Manipulation :-
Q1) Program to print elements of a list [ 'q', 'w', 'e', 'r', 't', 'y' ] in separate
lines along with element's both indexes (positive and negative).
Ans) L = [‘q’, ‘w’, ‘e’, ‘r’, ‘t’, ‘y’]
length = len(L) for a
in range(length):
print(“At indexes”,a, “and”, (a – length), “element:”, L[a])
Q2) Write a program display the lists. to create a copy of a list. In the
list's copy, add 10 to its first and last elements. Then display the lists.
Ans) L1 = [17, 24 ,15, 30, 34, 27]
L2 = L1.copy() print(“Original
list:”,L1) print(“Created copy of
the list:”,L2)
L2[0] += 10 L2[-1] += 10 print(“Copy of
the list after changes :”,L2)
print(“Original list :”, L1)
Q3) Create a list named friends and include elements like name, date of
birth, lucky number, favorite colour, weight, and height. Ans) name =
input('Enter Name:') lucky_no = int(input('Enter Lucky Number:'))
fav_col = input('Enter favorite color:') w = int(input('Enter weight:')) h
=int(input('Enter height:')) friends=[]
friends.append([name,lucky_no,fav_col,w,h])
print(friends)
Q4) Create a list of the following numbers and print them with their
index. [22,45,67,89,6,1,34,56,89,10] Ans) l =
[22,45,67,89,6,1,34,56,89,10]
print('Index – [0]: ',l[0])
print('Index – [1]: ',l[1])
print('Index – [2]: ',l[2])
print('Index – [3]: ',l[3])
print('Index – [4]: ',l[4])
print('Index – [5]: ',l[5])
print('Index – [6]: ',l[6])
print('Index – [7]: ',l[7])
print('Index – [8]: ',l[8])
print('Index – [9]: ',l[9])
Q5) Create a list of your friends’ names and print them.
Ans) list_frnds=
['Anurag','Bhargav','Chandrkant','Harshad','Jayesh','Mithun']
for i in range(0,len(list_frnds)):
print('Freind[',i,']:',list_frnds[i])
Q6) Write some lines about “Bharat is best” and make a list of words
used in the input.
Ans) w = 'Bharat is Best among all the countries'
l= w.split()
print(l)

f) Tuples:-
Q1) Write a program to create a tuple with a single input value.
Ans) t1 = eval(input(“enter input for tuple: ”)) print(“Created
tuple is:”, t1)
Q2) Write a program to print a tuple's first three and last three
elements in the following manner: 1st element, last element
2nd element, 2nd last element
3rd element, 3rd last element
Ans) t1 = eval(input("Enter input for tuple:
")) print(t1[0], t1[-1]) print(t1[1], t1[-2])
print(t1[2], t1[-3])
Q3) Program to print elements of a tuple ('Hello', "Isn't", "Python', 'fun',
'?') in separate lines along with element's both indexes (positive and
negative).
Ans) T = ('Hello', "Isn't", "Python', 'fun', '?')
length = len(T)
for a in range(length):
print(‘At indexes’,a, ‘and’, (a-lenth), ‘element:’,t[a])
Q4) Write a program to check if all the elements of a tuple are in
descending order or not.
Ans) tup=eval(input('Enter a tuple:')) if
sorted(tup, reverse=True)==list(tup):
print('Tuple is sorted in descending order') else:
print('Tuple is not sorted in descending order:')
Q5) Write a program to create a nested tuple to store roll number,
name and marks of students. Ans) tup= () while True :
roll = int(input("Enter a roll number : ")) name
= input("Enter name :") mark = input("Enter
marks :") tup += ( (roll,name,mark ),) user =
input("Do you want to quit enter yes/no =")
if user == "yes":
print(tup) break

Q6) Write a program to print the index of the minimum element in a


tuple.
Ans) tup = eval(input(“Enter a number:”)) mn = min(tup)
print(‘Minimum element’, mn,\ ‘is at index’, tup.index.mn))
g) Dictionaries :-
Q1) Write a program to read roll numbers and marks of four student
and create a dictionary from it having roll numbers as keys. Ans) rno =
[] mks = [] for a in range(4): r,m = eval(input(“Enter Roll
No.,Marks:”)) rno.append(r) mks.append(m)
d={ rno[0]:mks[0], rno[1]:mks[1], rno[2],mks2[], rno[3]:mks[3]}
print(“Created Dictionary”) print(d)
Q2) Write a program to create a phone dictionary for all your friends
and print each key value pair in separate lines.
Ans) PhoneDict = {“Madhav”:1234567, “Steven”:7654321,
“Dilpreet”:
6734521, “Rabiya”:4563217, “Murughan”: 3241567,
“Sampree”:4673215 }
For name in PhoneDict:
print(name, “:”, PhoneDict[name])
Q3) Write a program to create a dictionary M which stores the marks of
the students of class with roll numbers as the keys and marks as the
values. Get the number of students as input. Ans) M = {} n =
int(input(“How many students?”))
for a in range(n):
r,m = eval(input(“Enter Roll No.,Marks :”))
M[r] = m
print(“Created dictionary”) print(M)
Q4) Write a program to create a dictionary containing names of
competition winner students as keys and number of their wins as
values.
Ans) n=int(input("How many students?")) Compwinners
= {}
for a in range(n):

key = input("Name of the student:") value = int


(input("Number of competitions won :"))
CompWinners[key] = value
print ("The dictionary now is:")
print (CompWinners)
Q5) Write a program to add new students' roll numbers and marks in
the dictionary numbers as the keys and marks as the values. Ans) M =
{} n = int(input("How many students?"))
for a in range(n):
r, m = eval(input("Enter Roll No., Marks:"))
M[r] = m
print("Created dictionary") print(M)
ans = 'y' while
ans == 'y':
print("Enter details of new student")
r, m = eval(input("Roll No., Marks:"))
M[r] = m
ans = input("More students? (y/n):")
print("Dictionary after adding new students") print(M)
Q6) Writ a program to create a dictionary with 10 keys 0..9.
Ans)new_dict = {}
for i in range(10):
new_dict.setdefault(i)
print(“Dictionary is”) print(new_dict)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy