0% found this document useful (0 votes)
4 views

Python Practical Assignment

The document contains a series of Python programming assignments aimed at calculating student grades, generating factors, summing natural numbers, and performing various string and list operations. It includes solutions for each task, demonstrating the use of functions, loops, and data structures like lists, dictionaries, and tuples. Additionally, it covers advanced concepts such as recursion for calculating factorials and Fibonacci series.

Uploaded by

sambhovsagar44
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)
4 views

Python Practical Assignment

The document contains a series of Python programming assignments aimed at calculating student grades, generating factors, summing natural numbers, and performing various string and list operations. It includes solutions for each task, demonstrating the use of functions, loops, and data structures like lists, dictionaries, and tuples. Additionally, it covers advanced concepts such as recursion for calculating factorials and Fibonacci series.

Uploaded by

sambhovsagar44
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/ 7

Python Practical Assignment

Name: Sambhov Sagar


Section: L
B.Com (Hons) Semester 1
Roll No. : 24BC606

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")

2:Write a program to print factors of a given number.

Solution:
a= int(input("enter a number"))
for i in range (1,a+1):
if (a%i==0):
print(i)

3:Write a program to add n natural numbers and display their sum.


Solution:
a=int(input("enter a number of natural number"))
total=0
for i in range (1,a+1):
total= total+i
print("the total is",total)

4: Write a program to print the following conversion table (use looping constructs):

Height(in Feet) Height(in inches)


5.0ft 60 inches
5.1ft 61.2inches

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()

6:Write a function that calculates factorial of a number n.


Solution:
def abc(n):
if n<0:
return "factorial doesnt exist"
b=1

for i in range(1,n+1):
b*= i
return b

a= int(input("please enter a natural number"))


print(f"the factorial of {a} is {abc(a)}")
7:Write a program to print the series and its sum: (use functions)
1/1! + 1/2! + 1/3! ................1/n!
Solution:
def abc(n):
if n<0:
return "factorial doesnt exist"
b=1

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()

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

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]

print(f"Original list: {numbers}")


print(f"Even numbers: {even_numbers}")
print(f"Odd numbers: {odd_numbers}")

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}")

print(f"Concatenated list: {even_numbers + odd_numbers}")


print(f"Repeated odd numbers: {odd_numbers * 2}")

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)

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-> [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)

a = int(input("Enter a number: "))


print(f"Factorial of {a} is {fact(a)}")

E)
def fib(n):
if n == 0:
return 0
if n == 1:
return 1
return fib(n - 1) + fib(n - 2)

a = int(input("Enter the number of terms: "))


for i in range(a):
print(fib(i), end=" ")

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

print(f"Vowels: {vcount}, Consonants: {ccount}")

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))

l=list(map(int,input("Enter a list of number").split()))


fl=listmaker(l)
print("unique list:",fl)

…………………………………………………………………………………………………………………………………
…………………………………………………………………………………………

You might also like