Q. List out main differences between lists & tuple.
Mutability: Lists are mutable, meaning their elements can be modified after creation. Tuples are immutable, and once created, their elements cannot be changed.
Syntax: Lists are defined using square brackets [], while tuples use parentheses ().
Performance: Tuples generally have better performance than lists, especially in terms of iteration and memory usage.
Methods: Lists have more built-in methods compared to tuples because of their mutability.
Q. Demonstrate set with example.
# Set Example
fruits = {"apple", "banana", "orange"}
print(fruits)
# Adding an element to the set
fruits.add("grape")
print(fruits)
# Removing an element from the set
fruits.remove("banana")
print(fruits)
- Python is case sensitive language. Comment.
Yes, Python is case-sensitive. Variable names variable and Variable are considered different.
- Write a python program to calculate XY.
# Python Program to Calculate XY
x = 2
y = 3
result = x ** y
print(result)
- Write a python program to accept a number and check whether it is
perfect number or not.
# Python Program to Check Perfect Number
def is_perfect_number(num):
divisors_sum = sum(i for i in range(1, num) if num % i == 0)
return divisors_sum == num
number = int(input("Enter a number: "))
if is_perfect_number(number):
print(f"{number} is a perfect number.")
else:
print(f"{number} is not a perfect number.")
-Demonstrate list slicing.
# List Slicing Demonstration
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
subset = numbers[2:7] # Elements from index 2 to 6
print(subset)
-A tuple is ordered collection of items. Comment.
Yes, a tuple is an ordered collection of items. The order of elements in a tuple is preserved.
-Write a recursive function in Python to display addition of digits in
single digit.
def sum_digits(n):
# Base case: single-digit number
if n < 10:
return n
else:
return sum_digits(sum(map(int, str(n))))
result = sum_digits(12345)
print(f"Sum of digits in single digit: {result}")
-Write a program in python to accept 'n' integers in a list, compute &
display addition of all squares of these integers.
n = int(input("Enter the value of n: "))
numbers = [int(input(f"Enter integer {i + 1}: ")) for i in range(n)]
sum_squares = sum(x**2 for x in numbers)
print(f"Addition of squares: {sum_squares}")
- Explain the function enumerate( ).
The enumerate() function in Python is used to iterate over a sequence (such as a list) while keeping track of the index of the current item. It returns pairs of the form (index, value).
-- Explain the extend method of list.
Answer: The extend()
method in Python is a built-in method that is used to extend the elements of a list by appending elements from an iterable (e.g., another list, tuple, string, etc.)
# Original list fruits = ['apple', 'banana', 'orange'] # Another list to extend 'fruits' additional_fruits = ['grape', 'kiwi', 'watermelon'] # Using extend() to add elements from 'additional_fruits' to 'fruits' fruits.extend(additional_fruits) # Display the modified 'fruits' list print(fruits)
- What are required arguments in function?
Answer: Required arguments are the arguments that a function must receive in a specific order for it to work correctly.
def add_numbers(x, y): result = x + y return result # Calling the function with required arguments sum_result = add_numbers(3, 5) print("Sum:", sum_result)
add_numbers
function has two parameters (x
and y
), both of which are required. When calling the function, you need to provide values for these parameters in the order they are defined in the function signature (add_numbers(3, 5)
).- Explain any 2 built-in list functions.
Answer:
1) len(): The len()
function returns the number of elements in a list or the length of any iterable object.
my_list = [1, 2, 3, 4, 5] length_of_list = len(my_list) print("Length of the list:", length_of_list)
append()
method is used to add an element to the end of a list.Updated list: ['apple', 'banana', 'orange', 'grape']
- Write a Python program to print even length words in a string.
- def print_even_length_words(sentence): words = sentence.split() even_length_words = [word for word in words if len(word) % 2 == 0] print("Even length words:", even_length_words) # Example usage: input_sentence = input("Enter a sentence: ") print_even_length_words(input_sentence)
- Write a Python program to check if a given key already exists in a dictionary.
- def check_key_in_dict(my_dict, key): if key in my_dict: print(f"The key '{key}' already exists in the dictionary.") else: print(f"The key '{key}' does not exist in the dictionary.") # Example usage: my_dictionary = {'name': 'John', 'age': 25, 'city': 'New York'} input_key = input("Enter a key to check in the dictionary: ") check_key_in_dict(my_dictionary, input_key)
Write a Python program to find GCD of a number using recursion.