Here
Here
A) [1, 2]
B) [2, 3]
C) [2, 3, 4]
D) [1, 2, 3]
Answer: B) [2, 3]
Answer: A) def
Answer: A) list()
A) 3
B) 6
C) 7
D) Error
Answer: B) 6
Answer: A) lambda
Answer:
python
def check_even_odd(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
Answer:
`break`: Exits the nearest enclosing loop completely.
`continue`: Skips the rest of the code inside the current loop iteration and moves to the next iteration
of the loop.
Answer:
python
my_dict = {'name': 'Alice', 'age': 25}
print(my_dict['name']) Access value using key
Answer:
[1]
[1, 2]
The default argument `b` retains its value between calls because default arguments are evaluated only
once.
Answer:
python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n 1)
1. Describe how you would implement a Python function to check if a string is a palindrome. Provide
the code.
Answer:
python
def is_palindrome(s):
s = s.lower().replace(" ", "")
return s == s[:: 1]
Explanation: Convert the string to lowercase, remove spaces, and check if it reads the same
backward.
2. Explain the concept of list comprehensions with an example to create a list of squares of even
numbers from 1 to 10.
Answer:
python
even_squares = [x 2 for x in range(1, 11) if x % 2 == 0]
Explanation: List comprehensions provide a concise way to create lists.
This example squares each even number in the range 1 to 10.
3. Compare and contrast `for` and `while` loops in Python with examples.
Answer:
python
for loop example
for i in range(5):
print(i)
Comparison: Use `for` loop for a known range or sequence and `while` loop for indefinite iteration
until a condition is met.
4. Explain the concept of lambda functions in Python with an example to add two numbers.
Answer:
python
add = lambda x, y: x + y
print(add(5, 3)) Output: 8
Explanation: Lambda functions are anonymous, single expression functions defined using the
`lambda` keyword. They are useful for small, throwaway functions used without needing a full `def`
statement.
5. Describe how to handle exceptions in Python with a try except block. Provide an example that
catches a division by zero error.
Answer:
python
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")
Explanation:
Use `try` to wrap code that might cause an exception.
`except` catches the exception and allows handling it gracefully without stopping the program.
1. Implement a Python function that takes a list of numbers and returns a list with only the prime
numbers. Include helper function for prime check.
Answer:
python
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n 0.5) + 1):
if n % i == 0:
return False
return True
def filter_primes(numbers):
return [num for num in numbers if is_prime(num)]
Example usage:
numbers = [2, 3, 4, 5, 6, 7, 8, 9, 10]
print(filter_primes(numbers)) Output: [2, 3, 5, 7]
2. Explain with examples the use of `*args` and ` kwargs` in Python functions. Write a function that
demonstrates both.
Answer:
python
def example_function(*args, kwargs):
print("args:", args)
print("kwargs:", kwargs)
Example usage:
example_function(1, 2, 3, a=4, b=5)
Explanation:
`*args` allows passing a variable number of positional arguments.
` kwargs` allows passing a variable number of keyword arguments.
Output:
args: (1, 2, 3)
kwargs: {'a': 4, 'b': 5}
3. Write a Python program that defines a function to calculate the Fibonacci sequence up to `n` terms.
Provide both iterative and recursive approaches.
Answer:
python
Iter