Python Practical Assignment
Python Practical Assignment
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 : if Percentage >=60 and Percentage <80
Grade C : if Percentage >=40 and Percentage <60
Grade D : if Percentage <=40
Solution:
a= float(input("enter marks for subj 1"))
b= float(input("enter marks for subj 2"))
c= float(input("enter marks for subj 3"))
if (a<0) or (b<0) or (c<0) or (a>100) or (b>100) or (c>100):
print("please enter the correct marks")
else:
sm= (a+b+c)/3
print(f"{sm:.2f}")
if sm>=80:
print("Congrats you have Grade A")
elif sm>=60 and sm<80:
print("Grade B")
elif sm<60 and sm>=40:
print("Grade C")
else:
print("Grade D")
Solution:
a= int(input("enter a number"))
for i in range (1,a+1):
if (a%i==0):
print(i)
4: Write a program to print the following conversion table (use looping constructs):
5.8ft 69.6inches
5.9ft 70.8inches
6.0ft 72inches
Solution:
a= float(input("enter first height in feet"))
b= float(input("enter second height in feet"))
while a<= b:
print(f"{a*12:.1f}" " inches")
a+=0.1
5: Write a program that takes a positive integer n and the produce n lines of output as shown
*
**
***
****
Solution:
n= int(input("number please"))
for i in range(1,n+1):
print("*"*i, end="")
print()
for i in range(1,n+1):
b*= i
return b
for i in range(1,n+1):
b*= i
return b
def series(k):
total=0
for i in range(1, k + 1):
total += 1 / abc(i)
return total
def actual():
j= int(input("enter the value you desire"))
print(f"the sum is {series(j): .4f}")
actual()
Solution:
A)
strg=input("string please")
lenstr=len(strg)
print(f"length of string is: {lenstr}")
B)
strng= input("enter a string")
numstr= input("enter letter whose frequency you want to find")
abc= strng.count(numstr)
print(f"Frequency of '{numstr}':'{abc}'")
C)
strng= input("enter a string")
for char in strng:
if char.isupper():
print(f"'{char}' is in uppercase")
elif char.islower():
print(f"'{char}' is in lowercase")
else:
print(f"its not a string")
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.
Solution:
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
even_numbers = [num for num in numbers if num % 2 == 0]
odd_numbers = [num for num in numbers if num % 2 != 0]
even_numbers.append(100)
print(f"Even numbers after appending 100: {even_numbers}")
even_numbers.remove(100)
print(f"Even numbers after removing 100: {even_numbers}")
even_numbers.sort()
print(f"Sorted even numbers: {even_numbers}")
odd_numbers.insert(0, -1)
print(f"Odd numbers after inserting -1: {odd_numbers}")
popped_value = odd_numbers.pop()
print(f"Odd numbers after popping {popped_value}: {odd_numbers}")
odd_numbers.reverse()
print(f"Reversed odd numbers: {odd_numbers}")
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.
Solution:
b= int(input("Enter a number"))
cubedict = {a: a**3 for a in range(1, b+1)}
print(cubedict)
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
Solution:
A)
t1= (1,2,5,7,2,4)
middle= len(t1)//2
print("the first half is:", t1[:middle])
print("the second half is:", t1[middle:])
B)
t1= (1,2,5,7,2,4)
t2= (10,11)
ct3= t1+t2
print("the concatenated tuple is:", ct3)
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-> [1000,8000,27000,64000]
i. 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]
Solution:
A)
datalist = [1452, 11.23, 1+2j, True, 'w3resource', (0, -1), [5, 12], {"class":'V',
"section":'A'}]
for item in datalist:
print(f"Item: {item}, item: {type(item)}")
B)
def is_perfect_number(n):
a=0
for i in range(1, n):
if n % i == 0:
a += i
return a == n
b = int(input("Enter a number: "))
if is_perfect_number(b):
print(f"{b} is a Perfect number.")
else:
print(f"{b} is not a Perfect number.")
C)
a = int(input("Enter a number: "))
b = [int(x) for x in str(a)]
c = len(b)
s = sum(x ** c for x in b)
if s == a:
print(f"{a} is an Armstrong number.")
else:
print(f"{a} is not an Armstrong number.")
D)
def fact(n):
if n == 0 or n == 1:
return 1
return n * fact(n - 1)
E)
def fib(n):
if n == 0:
return 0
if n == 1:
return 1
return fib(n - 1) + fib(n - 2)
F)
a = list(map(int, input("Enter numbers: ").split()))
a = list(set(a))
a.sort()
if len(a) > 1:
print(f"The 2nd largest number is {a[-2]}")
else:
print("Not enough different numbers")
G)
a = input("Enter a string: ").lower()
vowels = "aeiou"
vcount = 0
ccount = 0
for c in a:
if c.isalpha():
if c in vowels:
vcount += 1
else:
ccount += 1
H)
a = list(map(int, input("Enter a list of integers separated by spaces: ").split()))
b = [x ** (i + 1) for i, x in enumerate(a)]
print("Output List:", b)
I)
def listmaker(a):
return list(set(a))
…………………………………………………………………………………………………………………………………
…………………………………………………………………………………………