# Python Solutions for S.Y.B.C.A.
(Science) Sem-IV Practical Exam
# Q1: Display a pattern
def pattern(n):
for i in range(n, 0, -1):
print(*[2 * j for j in range(1, i + 1)])
pattern(4)
# Q2: Check if a string is palindrome
def is_palindrome(s):
return s == s[::-1]
print(is_palindrome("madam"))
# Q3: Count character frequency
def char_frequency(s):
from collections import Counter
return dict(Counter(s))
print(char_frequency("google.com"))
# Q4: Find length, max, and min of a set
def set_operations(s):
return len(s), max(s), min(s)
print(set_operations({3, 5, 8, 1}))
# Q5: Combine dictionaries
def merge_dicts(d1, d2):
from collections import Counter
return dict(Counter(d1) + Counter(d2))
d1 = {'a':100,'b':200,'c':300}
d2 = {'a':300,'b':200,'d':400}
print(merge_dicts(d1, d2))
# Q6: Anonymous function for square area
square_area = lambda side: side ** 2
print(square_area(5))
# Q7: Show employee details
def showEmployee(name, salary=9000):
print(f"Name: {name}, Salary: {salary}")
showEmployee("John")
# Q8: Average of list numbers
def average(lst):
return sum(lst) / len(lst)
print(average([10, 20, 30, 40]))
# Q9: Fibonacci series
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b
print(fibonacci(10))
# Q10: Multiply all numbers in a list
def multiply_list(lst):
from functools import reduce
return reduce(lambda x, y: x * y, lst)
print(multiply_list([1, 2, 3, 4]))
# Q11: Count occurrences of words
def word_count(sentence):
from collections import Counter
return dict(Counter(sentence.split()))
print(word_count("hello world hello"))
# Q12: Unpacking a tuple
t = (1, 2, 3)
a, b, c = t
print(a, b, c)
# Q13: Person and Student classes
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"Hi, I'm {self.name}, {self.age} years old.")
class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age)
self.grade = grade
def introduce(self):
print(f"Hi, I'm {self.name}, {self.age} years old, studying in grade
{self.grade}.")
s = Student("Alice", 20, "A")
s.introduce()
# Q14: BankAccount class
class BankAccount:
def __init__(self, holder, balance=0):
self.__balance = balance
self.__holder = holder
def deposit(self, amount):
self.__balance += amount
def withdraw(self, amount):
if amount <= self.__balance:
self.__balance -= amount
else:
print("Insufficient balance")
def get_balance(self):
return self.__balance
acc = BankAccount("John Doe", 5000)
acc.deposit(1000)
acc.withdraw(2000)
print(acc.get_balance())
# Q15: Set operations
a = {1, 2, 3}
b = {3, 4, 5}
print(a.union(b), a.intersection(b), a.difference(b))
# Q16: Sorting dictionary and NumPy array check
import numpy as np
d = {'b': 2, 'a': 1, 'c': 3}
print(dict(sorted(d.items())))
arr = np.array([1, 2, 3])
print(np.all(arr))
# Q17: Filter even numbers using lambda
nums = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, nums))
print(evens)
# Q18: Sum of prime numbers till n
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
print(sum(filter(is_prime, range(10))))
# Q19: Convert tuple to string
tup = ('H', 'e', 'l', 'l', 'o')
print(''.join(tup))
# Q20: Remove key from dictionary
del d['b']
print(d)
# Q21: Remove odd-indexed characters
def remove_odd_index(s):
return s[::2]
print(remove_odd_index("abcdef"))