Computer Programming Deepanshu File (1)
Computer Programming Deepanshu File (1)
Practical File
Name – DEEPANSHU
Roll No. – 2024UEC2604
Question 1: Install python and set up the development environment
Print(‘Hello world’)
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.")
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
if string.upper()==string[::-1].upper():
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())
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}"
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
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]
import math
print(add(10, 50))
print(subtract(10, 50))
print(divide(10, 50))
print(multiply(10, 50))
print(math.pi)
print(math.e)