COMPUTER PROGRAMMING
Practical File
Netaji Subhas University of
Technology
Session - 2024-2025
Name – DEEPANSHU
Roll No. – 2024UEC2604
Question 1: Install python and set up the development environment
After installing the setup adding it to path
Question 2 : Write a python program to print “Hello World!”
Print(‘Hello world’)
Question 3 : Write a python program to calculate the area of a circle given
the radius.
from math import pi
def area():
r=int(input('enter the radius of the circle '))
a=pi*(r**2)
print('the area of the circler is ', a)
area()
Question 3 : Write a python program to check if a number is even or odd
and also check whether it is prime or not?
def numberchecker(no):
if no % 2 == 0:
print(f"{no} is even.")
else:
print(f"{no} is odd.")
if no <= 1:
print(f"{no} is not prime.")
elif no <= 3:
print(f"{no} is prime.")
else:
for i in range(2, no):
if n % i == 0:
print(f"{no} is not prime.")
break
else:
print(f"{no} is prime.")
num = int(input("Enter a number: "))
numberchecker(num)
Question 4: Implement a simple calculator using conditional statements.
n1 = float(input("Enter the first number: "))
operator = input("Enter the operator (+, -, *, /): ")
n2 = float(input("Enter the second number: "))
if operator == "+":
print(n1 + n2)
elif operator == "-":
print(n1 - n2)
elif operator == "*":
print(n1 * n2)
elif operator == "/":
if n2 != 0:
print(n1 / n2)
else:
print("Error")
else:
print("Error")
Question 5: Write a python program to print the Fibonacci series using a
for loop
def fibonacci(n):
x=0
y=1
for i in range(n):
print(x, end=" ")
x=y
y=x+y
num = int(input("Enter the number of digits to print: "))
fibonacci(num)
Question 6: Implement a function to check if a given string is palindrome
string=input('enter the string to check palindrome')
if string.upper()==string[::-1].upper():
print('the string is pallindrome')
else:
print('the string is not pallindrome')
Question 7: Perform various operations on lists.
list1 = [5, 2, 8, 3, 1, 6, 4]
# Slicing:
print(list1[2:5])
# Sorting:
list1.sort()
print(list1)
# Indexing:
print(list1.index(3))
# Insertion:
list1.insert(3, 7)
print(list1)
Question 8: Use dictionaries to store and retrieve student grades.
dictionary={}
def data(n):
for i in range(n):
name= input('enter the name')
grade=int(input('enter the grades'))
dictionary.update({name:grade})
def fetchdata():
search=input('enter the name whose grade you want to see ')
for i in dictionary.keys():
if i.lower()==search.lower():
return dictionary[i]
data(3)
print(fetchdata())
Question 9: Create a class to represent a book with attributes and methods
class Book:
def __init__(self, title, author, pages, year_published):
object.__setattr__(self, 'title', title)
object.__setattr__(self, 'author', author)
object.__setattr__(self, 'pages', pages)
object.__setattr__(self, 'year_published', year_published)
def __str__(self):
return f"{self.title} by {self.author}, {self.pages} pages, published in
{self.year_published}"
# Create a new book object
book1 = Book("wings of fire ", "abdul kalam", 281, 1960)
# Print the book's details
print(book1)
# Get the book's title
print(book1.title)
# Set the book's title to a new
value
book1.title = "wings of fire"
print(book1.title)
Write a python program to implement inheritance in the above class
class Book:
def __init__(self, title, author, pages, year_published):
self.title = title
self.author = author
self.pages = pages
self.year_published = year_published
def __str__(self):
return f"{self.title} by {self.author}, {self.pages} pages, published in
{self.year_published}"
class EBook(Book):
def __init__(self, title, author, pages, year_published, file_size, file_format):
super().__init__(title, author, pages, year_published)
self.file_size = file_size
self.file_format = file_format
def __str__(self):
return super().__str__() + f" - eBook ({self.file_size} MB,
{self.file_format})"
class Audiobook(Book):
def __init__(self, title, author, pages, year_published, duration, narrator):
super().__init__(title, author, pages, year_published)
self.duration = duration
self.narrator = narrator
def __str__(self):
return super().__str__() + f" - Audiobook ({self.duration} hours, narrated
by {self.narrator})"
ebook1 = EBook("The Best Seller", "Chetan Bhagat", 250, 2004, 10, "PDF")
audiobook1 = Audiobook("The Immortals of Meluha", "Amish Tripathi", 350,
2010, 8, "Amitabh Bachchan")
print(ebook1)
print(audiobook1)
Question 11: Write a python program to implement a generator function to
generate the fibonacci sequence.
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, a + b
for num in fibonacci(10):
print(num)
fib_gen = fibonacci(10)
print(next(fib_gen))
print(next(fib_gen))
print(next(fib_gen))
print(next(fib_gen))
print(next(fib_gen))
Question 12: Use lambda functions, map, and filter to perform operations
on a list.
numList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
squaredNums = list(map(lambda x: x ** 2, numList))
print(squaredNums)
evenNums = list(filter(lambda x: x % 2 == 0, numList))
print(evenNums)
doubledNums = list(map(lambda x: x * 2, numList))
print(doubledNums)
numsLessThan50 = list(filter(lambda x: x < 50, numList))
print(numsLessThan50)
Question 13: Create a module that contains functions for mathematical
operations
import math
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def divide(a, b):
if b == 0:
raise ValueError("Cannot divide by zero!")
return a / b
def multiply(a, b):
return a * b
print(add(10, 50))
print(subtract(10, 50))
print(divide(10, 50))
print(multiply(10, 50))
print(math.pi)
print(math.e)
Question 14: Import and use functions from external packages.
import math
import random
print(f"Value of Pi: {math.pi}")
print(f"Square root of 2200: {math.sqrt(2200)}")
print(f"Tangent value of pi: {math.tan(math.pi)}")
print(f"The factorial of 5 is: {math.factorial(5)}")
print(f"Random float 0 and 10: {random.random()}")
print(f"Random integer between 100 and 500: {random.randint(100, 500)}")
choices = ["apple", "banana", "cherry", "date"]
print(f"Random fruit: {random.choice(choices)}")
random.shuffle(choices)
print(f"Shuffle list: {choices}")