Group A
Group B
11.
Membership operators in Python are used to test whether a value or variable exists in a sequence (such as a string, list, tuple, set, or dictionary). There are two membership operators: in
and not in
.
in
Operator: This operator checks if a specified value is present in a sequence. If the value is found, it returnsTrue
; otherwise, it returnsFalse
.not in
Operator: This operator checks if a specified value is not present in a sequence. If the value is not found, it returnsTrue
; otherwise, it returnsFalse
.
1 2 3 |
my_list = [1, 2, 3, 4] print(2 in my_list) # Output: True print(5 not in my_list) # Output: True |
12. Write a program to count the number of vowels in a string.
1 2 3 4 5 6 7 8 9 10 11 |
def count_vowels(s): vowels = "aeiouAEIOU" count = 0 for char in s: if char in vowels: count += 1 return count # Example usage string = "Hello, World!" print(count_vowels(string)) # Output: 3 |
15.Write a program using a function to find the sum of any numbers passed to the function.
1 2 3 4 5 6 |
def sum_numbers(*args): return sum(args) # Example usage print(sum_numbers(1, 2, 3, 4)) # Output: 10 print(sum_numbers(5, 10, 15)) # Output: 30 |
16 . How do you draw multiple plots in a single figure using Matplotlib?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import matplotlib.pyplot as plt # Create a 2x1 grid of subplots plt.subplot(2, 1, 1) # First subplot plt.plot([1, 2, 3], [4, 5, 6]) plt.title('First Plot') plt.subplot(2, 1, 2) # Second subplot plt.plot([1, 2, 3], [10, 20, 30]) plt.title('Second Plot') plt.tight_layout() # Adjust the layout plt.show() |
Group C: Long Answer Questions
17. Write a program to check if a number entered is prime or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def is_prime(number): if number <= 1: return False for i in range(2, int(number**0.5) + 1): if number % i == 0: return False return True # Example usage num = int(input("Enter a number: ")) if is_prime(num): print(f"{num} is a prime number.") else: print(f"{num} is not a prime number.") |
18.Explain match-case statement with a suitable example.
The match-case
statement, introduced in Python 3.10, is similar to a switch-case statement found in other languages. It allows you to match patterns in a value or object and execute code blocks based on those patterns
1 2 3 4 5 6 7 8 9 10 11 12 |
def identify_animal(animal): match animal: case "dog": return "It's a dog!" case "cat": return "It's a cat!" case _: return "Unknown animal" # Example usage print(identify_animal("dog")) # Output: It's a dog! print(identify_animal("bird")) # Output: Unknown animal |
19.
List comprehension is a concise way to create lists in Python. It allows for the generation of lists based on existing lists, and it can include a conditional statement for filtering.
1 2 3 4 5 6 7 8 9 |
# Traditional way to create a list of squares squares = [] for i in range(10): squares.append(i ** 2) # List comprehension squares = [i ** 2 for i in range(10)] print(squares) # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
20.
Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. The overridden method in the subclass has the same name, parameters, and return type as the method in the superclass.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Animal: def speak(self): return "Some sound" class Dog(Animal): def speak(self): return "Woof!" # Example usage animal = Animal() dog = Dog() print(animal.speak()) # Output: Some sound |