Practical Assignment
Programming Using Python Sem I General Elective
(By Aarushi Handa 24BC484)
1. Write a program to calculate total marks, percentage and grade of a
student. Marks obtained in each of three subjects are to be input by
the user. Assign grades according to the following criteria:
Grade A : if Percentage >=80
Grade B : 1f Percentage >=60 and Percentage <80
Grade C : if Percentage >=40 and Percentage <60
Grade D : if Percentage <=40
s1= int(input("Enter your marks in the first subject: "))
s2= int(input("Enter your marks in the second subject: "))
s3= int(input("Enter your marks in the third subject: "))
ps=(s1+s2+s2)/3
if ps>=80:
g='A'
elif 60<ps<80:
g='B'
elif 40<ps<60:
g='C'
else:
g='D'
print('Your Total Marks are: ', s1+s2+s3)
print('Your Total Percentage is: ', ps,'%')
print('Your Grade is: ', g)
2. Write a program to print factors of a given number.
x=int(input('Enter a number between 1 to 100: '))
y=[]
for i in range(1,x+1):
if x%i==0:
y.append(i)
print(y)
3. Write a program to add N natural numbers and display their sum.
n=int(input("Enter the number upto which you want the sum: "))
for i in range(1,n+1):
s=i*(i+1)/2
print("The sum upto the number equals to: ",s)
4. Write a program to print the following conversion table (use looping
constructs):
Height(in Feet) Height(in inches)
5.0ft
5.8ft 60 inches
69.6 inches
5.1ft
5.9ft 61.2 inches
70.8 inches
6.0ft 72 inches
def Table():
print("Height(in feet)","\t","Height(in inches)")
f=5.0
while f<=6.0:
i=f*12
print(round(f,1),"\t","\t","\t",round(i,1))
f+=0.1
Table()
5. Write a program that takes a positive integer n and the produce n
lines of output as shown:
*
**
***
****
(for n =4)
n=int(input("Enter any number: "))
for i in range(1,n+1):
for j in range(1,i+1):
print("*", end=" ")
print()
6. Write a function that calculates factorial of a number n.
f=int(input("Enter any number: "))
x=1
for i in range(1,f+1):
x=x*i
print(x)
7. Write a program to print the series and its sum: (use functions)
1/1! + 1/2! + 1/3!.......I/n!
z=[]
def get_sum(n):
x=1
y=0
for i in range(1,n+1):
global z
z.append(1/i)
x=x*i
y=y+(1/x)
return y
print(get_sum(int(input("Enter any number: "))))
print(z)
8. Write a program to perform the following operations on an input
string
a. Print length of the string
b. Find frequency of a character in the string
c. Print whether characters are in uppercase or lowercase
s=input("Enter the string: ")
print("the length of the string is ",len(s),"characters")
freq={c: s.count(c) for c in s}
print("The frequency of each character in the string is: ",freq)
if s.upper()==s:
print("The string is in upper case")
elif s.lower()==s:
print("The string is in lower case")
else:
print("The string is neither completely in uppercase nor completely in lower
case")
9. Write a program to create two lists: one of even numbers and
another of odd numbers. The program should demonstrate the
various operations and methods on lists.
n=int(input("Enter the number upto which you want the program to go to: "))
even=[]
odd=[]
for i in range(1,n+1):
if i%2==0:
even.append(i)
else:
odd.append(i)
print(even)
print(odd)
print(even[2])
print(odd[-2])
print(odd[2:4])
even[0]=0
print(even)
print(odd.index(3))
print(even.count(2))
even.sort()
print(even)
even.sort(reverse=True)
print(even)
even.append(2)
print(even)
extend=[8]
even.extend(extend)
print(even)
odd.extend([9])
print(odd)
even.remove(0)
print(even)
print(odd.pop())
print(odd)
print(odd.pop(3))
print(odd)
print(even+odd)
odd.insert(3,7)
print(odd)
odd.remove(3)
print(odd)
del even[1]
print(even)
del odd[1:3]
print(odd)
del even[:]
print(even)
10. Write a program to create a dictionary where keys are
numbers between 1 and 5 and the values are the cubes of the keys.
Dict={1:1**3,2:2**3,3:3**3,4:4**3,5:5**3}
print(Dict)
11. Write a program to create a tuple tl = (1,2,5,7,2,4). The
program should perform the following:
a. Print tuple in two lines, line 1 containing the first half of tuple
and second line having the second half.
b. Concatenate tuple t2 = (10,11) with tl
tl=(1,2,5,7,2,4)
print(tl[0:3],tl[3:])
t2=(10,11)
concat=tl+t2
print(concat)
12. Attempt the following questions:
a. Print each item and its corresponding type from the following list.
Sample List : datalist = [1452, 11.23, 1+2j, True, 'w3resource',
(0, -1), [5, 12], {"class":'V', "section":'A'}]
b. Write a Python Program to check whether a number input by user
is a Perfect number.
c. Write a Python Program to check whether a number input by user
is an Armstrong number.
d. Write a Python Program to print factorial of a number using
recursive function.
e. Write a Python Program to print the 1st n Fibonacci series using
recursive function.
f. Write a Python program to find the 2nd largest number in a list.
g. Write a Python program to count vowels and consonants in a
string.
h. Write a Python program to take a list of integers as input from the
user and prints a
list containing the power of numbers in the list raised to their index.
Sample Input:
List 1-> [10,20,30,40]
Output:
List 2-> [1,20,900,6400]
a. datalist = [1452, 11.23, 1+2j, True, 'w3resource', (0, -1), [5, 12], {"class":'V',
"section":'A'}]
for i in datalist:
print("The datatype of ",i," is ",type(i))
b. n=int(input("Enter the number you want to check for perfect number: "))
def perf(p):
f=[]
s=0
for i in range(1,n):
if n%i==0:
f.append(i)
for j in f:
s+=j
if s==n:
print("It is a perfect number.")
else:
print("It is not a perfect number.")
perf(n)
c. n=int(input("Enter any number you want to check for armstrong number: "))
s=0
a=str(n)
for i in a:
b=int(i)
s+=b**3
if s==n:
print("Yes, it is an armstrong number")
else:
print("No, it is not an armstrong number")
d. n=int(input("Enter any number: "))
def fact(f):
if f==0:
return 0
elif f==1:
return 1
else:
return(f*fact(f-1))
fact(n)
e. n=int(input("Enter the number of terms: "))
def fibonacci(f):
if f==0:
return 0
elif f==1:
return f
else:
return(fibonacci(f-1)+fibonacci(f-2))
print(fibonacci(n))
f. l=[50,70,60,89,76,0,109]
l.sort(reverse=True)
print(l[1])
g. str=input("Enter: ")
vowels=["a","e","i","o","u"]
v=0
c=0
for i in str:
if i in vowels:
v+=1
elif i==" ":
continue
else:
c+=1
print("The number of vowels in the provided string are ",v)
print("The number of consonants in the provided string are ",c)
h. t=int(input("Enter number of terms to add into list: "))
num=[]
for i in range(1,t+1):
n=int(input("Enter any number: "))
num.append(n)
print("The given list is: ",num)
pow=[]
for a in num:
x=num.index(a)
pow.append(a**x)
print("The desired list is: ",pow)
13. Write a Python function that takes a list as an argument and
returns a new list with
distinct elements from the first list.
Sample List: [1,2,3,3,3,3,4,5]
Unique List: [1, 2, 3, 4, 5]
def unique(l):
x=[]
for i in l:
if i not in x:
x.append(i)
return x
print(unique([1,2,4,5,8,4,2,3]))