The document contains Python solutions for various programming problems suitable for a practical exam. It includes functions for displaying patterns, checking palindromes, counting character frequency, and performing set operations, among others. Additionally, it features class definitions for Person, Student, and BankAccount, demonstrating object-oriented programming concepts.
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views
python_based_questions
The document contains Python solutions for various programming problems suitable for a practical exam. It includes functions for displaying patterns, checking palindromes, counting character frequency, and performing set operations, among others. Additionally, it features class definitions for Person, Student, and BankAccount, demonstrating object-oriented programming concepts.
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()
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))))