Computer Science Practical
Computer Science Practical
Output:-
a)Even – Odd Number
Code:-
num = int(input("Please Enter a Number:- "))
if(num % 2 == 0):
print("It is an even number")
elif(num % 2 == 1 or num == 1):
print("It is odd number")
else:
print("Wrong input found")
Output:
b) Simple Calculator
Code:-
print("Welcome to Simple Calculator")
symbol = input("Enter the operator that you want to perform:- ")
num1 = int(input("Enter Number 1:- "))
num2 = int(input("Enter Number 2:- "))
if symbol == "+":
print(num1 + num2)
elif symbol == "-":
print(num1 - num2)
elif symbol == "/":
print(num1 / num2)
elif symbol == "*":
print(num1 * num2)
else:
print("Given inputs are wrong")
Output:-
c) Print Fibonacci Series using for loop
Code:-
def print_fibonacci(n):
"""Print Fibonacci series up to n terms."""
a, b = 0, 1
print("Fibonacci series:")
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
print()
num_terms = 10
print_fibonacci(num_terms)
Output:-
a) Checking if String is palindrome or not
Input-
def is_palindrome(number):
str_num = str(number)
return str_num == str_num[::-1]
num = int(input("Input your number:- "))
if is_palindrome(num):
print("Given number is palindrome")
else:
print("Given number is not a palindrome")
Output:-
b) Perform various operations on list
Code:-
fruits = ['apple', 'banana', 'cherry']
print("Initial list:", fruits)
fruits.append('orange') # Adding elements in List
print("After append:", fruits)
fruits.insert(1, 'mango') # Inserting new elements
print("After insert:", fruits)
fruits.remove('banana') # Removing elements
print("After remove:", fruits)
popped_fruit = fruits.pop(2) # Removing elements by index
print("After pop:", fruits)
print("Popped fruit:", popped_fruit)
first_fruit = fruits[0] # Accessing elements of list
print("First fruit:", first_fruit)
sliced_fruits = fruits[1:3] # Slicing a list
print("Sliced fruits:", sliced_fruits)
fruits.sort() # Sorting a list
print("Sorted list:", fruits)
fruits.reverse() # Reversing a list
print("Reversed list:", fruits)
vegetables = ['carrot', 'broccoli']
all_items = fruits + vegetables # Combining two lists
print("Combined list:", all_items)
Output:-
c) Using a dictionary to store and retrieve student grades.
Input:-
dict = {}
def student_update(student_name, student_grade):
dict[student_name] = float(student_grade)
def student_new(student_name, student_grade):
dict.update({student_name:float(student_grade)})
def student_remove(student_name):
dict.pop(student_name)
isRunning = True
while isRunning:
updated = "Updated Successfully\n"
print("1 -> To add new student's detail")
print("2 -> To update student's datails")
print("3 -> To remove any student's detail")
print("4 -> To know details of student")
inp = input("What you want to do: ")
if inp == '1':
inp1 = input("Input student's name :- ")
inp2 = input("Input your grade:- ")
student_new(inp1, inp2)
elif inp == '2':
inp1 = input("Input student's name :- ")
inp2 = input("Input student's grade:- ")
student_update(inp1, inp2)
elif inp == '3':
inp1 = input("Input student's name :- ")
student_remove(inp1)
elif inp == '4':
inp1 = input("Input student's name:- ")
print(dict[inp1])
elif inp == 'Exit':
isRunning = False
updated = "Program Ended"
print(updated)
Output :-
a) Create a class Book with attributes and methods
Code
Input:-
class Book:
def __init__(self, title, author, publication_year, genre):
self.title = title
self.author = author
self.publication_year = publication_year
self.genre = genre
self.is_checked_out = False
def return_book(self):
if self.is_checked_out:
self.is_checked_out = False
return f"You have returned '{self.title}'."
else:
return f"'{self.title}' was not checked out."
def book_info(self):
return (f"Title: {self.title}\n"
f"Author: {self.author}\n"
f"Publication Year: {self.publication_year}\n"
f"Genre: {self.genre}\n"
f"Checked Out: {'Yes' if self.is_checked_out else 'No'}")
Output:-
b) Implement Inheritance
Code:-
Input:-
class Book:
def __init__(self, title, author, publication_year, genre):
self.title = title
self.author = author
self.publication_year = publication_year
self.genre = genre
def book_info(self):
return (f"Title: {self.title}\n"
f"Author: {self.author}\n"
f"Publication Year: {self.publication_year}\n"
f"Genre: {self.genre}")
class Ebook(Book):
def __init__(self, title, author, publication_year, genre, file_format, file_size):
super().__init__(title, author, publication_year, genre)
self.file_format = file_format
self.file_size = file_size
def ebook_info(self):
base_info = self.book_info()
return (f"{base_info}\n"
f"File Format: {self.file_format}\n"
f"File Size: {self.file_size} MB")
my_ebook = Ebook("1984", "George Orwell", 1949, "Dystopian", "PDF", 1.5)
print(my_ebook.ebook_info())
Output:-
c) Generate a function to print Fibonacci Series
Code:-
Input:-
def fibonacci_series(n):
"""Generate a Fibonacci series up to n terms."""
fib_series = []
a, b = 0, 1
for _ in range(n):
fib_series.append(a)
a, b = b, a + b
return fib_series
num_terms = int(input("Enter number upto which you want fibonacci numbers:- "))
print(f"Fibonacci series up to {num_terms} terms: {fibonacci_series(num_terms)}")
Output:-