UNIT 3 Solution Python Programming QUESTION BANK 2023-24
UNIT 3 Solution Python Programming QUESTION BANK 2023-24
UNIT-3
QUESTION BANK SOLUTIONS
Q1: Explain the concept of string indexing & slicing and provide an example in Python.
Ans:
1: Positive Indexing:
❑ Positive indexing start from Zero (0) and from left hand side i.e. first character store at index 0,
second at index 1 and so on.
2: Negative Indexing:
❑ Negative Indexing starts from negative indexing start from -1 and from right-hand side.
❑ Last character store at index -1 and as we move towards the left, it keeps increasing like -2, -3,
and so on.
Example:
Q2: Differentiate between Mutable and Immutable objects in python languages with
example?
Ans:
Q3:
Explain the difference between function arguments and function
parameters. Discuss the concept of variable scope within
functions in Python
1. Function Parameters:
o Parameters are the names used in the function
definition to represent the input values that a
function expects.
o They act as placeholders for the actual values
(arguments) that will be passed into the function
when it is called.
o Parameters are defined in the function signature and
serve as a way to declare what kind of values a
function should accept.
python
• def add(x, y): # x and y are parameters
result = x + y
return result
• Function Arguments:
python
2. # 2 and 3 are arguments
3. sum_result = add(2, 3)
4.
1. Local Scope:
o Variables defined within a function have a local
scope, meaning they are only accessible within that
function.
o Once the function completes execution, the local
variables are destroyed, and their values are not
accessible from outside the function.
python
• def example_function():
local_variable = "I am local"
print(local_variable)
example_function()
# This would cause an error because local_variable is
not defined here:
# print(local_variable)
• Global Scope:
python
• global_variable = "I am global"
def example_function():
print(global_variable)
example_function()
print(global_variable)
• Scope Hierarchy:
python
x = 10 # Global variable
def outer_function():
y = 20 # Enclosed variable
def inner_function():
z = 30 # Local variable
print(x, y, z)
inner_function()
outer_function()
Q4: Write a Python program to add 'ing' at the end of a given string
(length should be at least 3). If the given string already ends with 'ing',
add 'ly' instead. If the string length of the given string is less than 3,
leave it unchanged.
Ans:
def modify_string(input_string):
if len(input_string) >= 3:
if input_string.endswith('ing'):
else:
else:
result_string = input_string
return result_string
# Example usage:
input_str1 = "play"
input_str2 = "swimming"
input_str3 = "go"
result1 = modify_string(input_str1)
result2 = modify_string(input_str2)
result3 = modify_string(input_str3)
Q5:
When to Use Python Lists and when to Use Tuples, Dictionaries
or Sets.
ANS:
In Python, lists, tuples, dictionaries, and sets are all data structures that serve different purposes.
The choice of which one to use depends on the specific requirements of your program. Here are
some guidelines on when to use each:
1. Lists:
o Use when:
▪ You need an ordered collection of items.
▪ You need a collection that allows duplicate elements.
▪ You need to modify the elements (add, remove, or modify) after creation.
o Example:
python
• •
• my_list = [1, 2, 3, 'hello', 2.5]
• Tuples:
• Use when:
o You need an ordered, immutable collection of items.
o You want to create a collection that should not be changed during the program's
execution.
o You want to use the collection as a key in a dictionary (since keys must be
immutable).
• Example:
python
• •
• my_tuple = (1, 2, 'world', 3.14)
• Dictionaries:
• Use when:
o You need an unordered collection of key-value pairs.
o You want to look up values based on some unique identifier (the key).
o You need to associate values with labels or names.
• Example:
python
• •
• my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
• Sets:
• Use when:
o You need an unordered collection of unique elements.
o You want to perform set operations like union, intersection, and difference.
• Example:
python
4.
o my_set = {1, 2, 3, 4, 5}
o
Note:
• Use lists when you need an ordered collection with the ability to modify elements.
• Use tuples when you need an ordered and immutable collection.
• Use dictionaries when you need a collection of key-value pairs for quick lookups.
• Use sets when you need an unordered collection of unique elements and want to perform
set operations.
Ultimately, the choice between lists, tuples, dictionaries, and sets depends on the specific
requirements of your program and the characteristics of each data structure.
Q6:
Explain Tuples and Unpacking Sequences in Python Data
Structure
ANS:
Tuples in Python:
A tuple is a data structure in Python that is similar to a list but is immutable, meaning its elements
cannot be changed after creation. Tuples are defined using parentheses () and can contain elements
of different data types. Tuples are often used to represent fixed collections of items.
Creating a Tuple:
python
my_tuple = (1, 'hello', 3.14)
Accessing Elements:
python
first_element = my_tuple[0] # Accessing the first element
Immutable Nature:
python
# This will result in an error because tuples are immutable
my_tuple[1] = 'world'
Unpacking sequences is a feature in Python that allows you to assign the elements of a sequence
(like a tuple or a list) to multiple variables in a single line. This is often used with tuples to assign
values to individual variables.
Example:
python
# Creating a tuple
coordinates = (3, 4)
print("x:", x) # Output: x: 3
print("y:", y) # Output: y: 4
In this example, the tuple (3, 4) is unpacked into the variables x and y. This is a concise way to
assign values to multiple variables at once.
Swapping Variables:
python
a = 5
b = 10
print("a:", a) # Output: a: 10
print("b:", b) # Output: b: 5
Unpacking sequences is not limited to tuples; it can be used with any iterable, such as lists.
python
# Unpacking with the * operator
first, *rest = (1, 2, 3, 4, 5)
Q7:
Describe the difference between append () and extend () methods
in Python lists.
ANS:
In Python, append() and extend() are two methods used to add elements to lists, but they behave
differently:
1. append() Method:
o The append() method is used to add a single element to the end of the list.
o The element can be of any data type, including another list or any other object.
o It modifies the original list in place by adding the specified element as a single item.
python
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
If the element being appended is a list, it is added as a single element, not as individual elements.
python
• my_list = [1, 2, 3]
my_list.append([4, 5])
print(my_list) # Output: [1, 2, 3, [4, 5]]
• extend() Method:
• The extend() method is used to append elements from an iterable (e.g., a list, tuple, string)
to the end of the list.
• It modifies the original list in place by adding each element from the iterable individually.
python
my_list = [1, 2, 3]
my_list.extend([4, 5])
print(my_list) # Output: [1, 2, 3, 4, 5]
Q8:
Explain the algorithm Sieve of Eratosthenes used in Python
Programming
ANS:
The Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to a given limit.
It efficiently identifies prime numbers and eliminates multiples of each prime as it iterates through
the numbers. The algorithm is named after the ancient Greek mathematician Eratosthenes, who
first described it.
python
def sieve_of_eratosthenes(limit):
primes = []
is_prime = [True] * (limit + 1)
is_prime[0] = is_prime[1] = False
return primes
# Example usage:
limit = 30
result = sieve_of_eratosthenes(limit)
print(result)
Q9:
Explain the concept of list slicing and demonstrate its usage with
a sample list.
ANS:
List slicing in Python is a technique that allows you to create a subsequence (or "slice") of a list
by specifying a range of indices. The general syntax for list slicing is list[start:stop:step],
where start is the index of the first element to include, stop is the index of the first element to
exclude, and step is the number of indices between each element in the slice. All three parameters
are optional.
• start: The index of the first element to include in the slice. If omitted, it defaults to 0 for
positive step values or -1 for negative step values (i.e., it starts from the end of the list).
• stop: The index of the first element to exclude from the slice. If omitted, it defaults to the
end of the list for positive step values or the beginning of the list for negative step values.
• step: The number of indices between each element in the slice. If omitted, it defaults to 1.
python
# Sample list
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Output:
less
Original list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Slice 1: [2, 3, 4, 5]
Slice 2: [3, 4, 5, 6, 7, 8, 9]
Slice 3: [0, 1, 2, 3, 4]
Slice 4: [1, 3, 5, 7]
Slice 5: [7, 8, 9]
Slice 6: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
In the example above, various slices of the original list are created using different combinations of
start, stop, and step values. The results demonstrate how list slicing can be a powerful and
concise way to extract specific portions of a list.
Q10:
Discuss the concept of function decorators and their role in
Python
ANS:
In Python, a decorator is a design pattern that allows you to extend or modify the behavior of
functions or methods without changing their actual code. Decorators are applied using the
@decorator syntax before the function definition. They are a powerful and flexible feature in
Python, commonly used for tasks such as logging, timing, authentication, and more.
Key Concepts:
1. Syntax:
o A decorator is a function that takes another function as an argument and usually returns
a new function that enhances or modifies the behavior of the original function.
python
• @decorator
def my_function():
# Original function code
• In Python, functions are first-class objects, which means they can be passed as arguments to other
functions and returned as values from other functions.
• You can apply multiple decorators to a single function, and they are executed in the order they
are listed.
python
3. @decorator1
4. @decorator2
5. def my_function():
6. # Original function code
7. This is equivalent to decorator1(decorator2(my_function)).
Example:
Let's consider a simple example of a decorator that logs information about the function call:
python
def log_decorator(func):
def wrapper(*args, **kwargs):
print(f"Calling {func.__name__} with args: {args}, kwargs: {kwargs}")
result = func(*args, **kwargs)
print(f"{func.__name__} returned: {result}")
return result
return wrapper
@log_decorator
def add(a, b):
return a + b
result = add(3, 5)
In this example, the log_decorator function takes another function (func) as its argument,
defines a new function (wrapper) that logs information before and after calling func, and returns
this new function. The @log_decorator syntax is used to apply the decorator to the add function.
When add(3, 5) is called, it is actually the wrapper function created by the decorator that gets
executed, and it logs information about the function call.
Role in Python:
• Code Reusability: Decorators allow you to encapsulate common behavior and reuse it
across multiple functions.
• Separation of Concerns: Decorators help separate the concerns of different aspects of
code, making it more modular and easier to maintain.
• Aspect-Oriented Programming (AOP): Decorators support the AOP paradigm by
providing a way to add cross-cutting concerns (e.g., logging, authentication) without
modifying the core logic of functions.
• Readability: Decorators improve code readability by keeping the core functionality of a
function separate from additional features.
Decorators are a powerful tool in Python, enabling clean and modular code design by promoting
the separation of concerns and facilitating code reuse. They are widely used in various frameworks
and libraries to enhance the functionality of functions and methods.
Q11:
Find all of the numbers from 1-1000 that are divisible by 7 using list
comprehension.
ANS:
divisible_by_7 = [num for num in range(1, 1001) if num % 7 == 0]
In this code:
When you run this code, it will print a list containing all the numbers from 1 to 1000 that are
divisible by 7.
Q12:
Write a Python program to get a list, sorted in increasing order
by the last element in each tuple from a given list of non-empty
tuples.
Sample List: [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
Expected Result: [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
ANS:
# Sample list of tuples
sample_list = [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
# Define a custom key function to extract the last element from each tuple
def last_element_of_tuple(t):
return t[-1]
# Use sorted() with the custom key function to sort the list of tuples
sorted_list = sorted(sample_list, key=last_element_of_tuple)
This program defines a custom key function last_element_of_tuple that takes a tuple and
returns its last element (t[-1]). Then, the sorted() function is used with this custom key function
to sort the list of tuples based on their last elements.
When you run this program, it will output the expected result:
less
Original List: [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
Sorted List: [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
Q13:
Write a Python script to sort (ascending and descending) a dictionary
by value
ANS:
#Sample dictionary
sample_dict = {'apple': 5, 'banana': 2, 'orange': 8, 'grape': 1}
In this script:
When you run this script, it will output the original dictionary and two sorted dictionaries
(ascending and descending):
python
Original Dictionary: {'apple': 5, 'banana': 2, 'orange': 8, 'grape': 1}
Sorted Dictionary (Ascending): {'grape': 1, 'banana': 2, 'apple': 5, 'orange':
8}
Sorted Dictionary (Descending): {'orange': 8, 'apple': 5, 'banana': 2, 'grape':
1}
Q14:
Write a Python program to count the number of characters
(character frequency) in a string. Sample String: google.com'
ANS:
# Sample string
sample_string = 'google.com'
This program uses a dictionary (char_frequency) to store the frequency of each character in the
given string. The for loop iterates through each character in the string, and the get() method is
used to retrieve the current count of the character (or 0 if it's the first occurrence), and then
increments it.
When you run this program with the provided sample string, it will output the character
frequencies:
makefile
Character Frequencies in the String:
g: 2
o: 4
l: 1
e: 1
.: 1
c: 1
m: 1
Q15:
Write a Python program to compute the square of the first N
Fibonacci numbers, using the map function and generate a list of
the numbers.
ANS:
# Function to generate the first N Fibonacci numbers
def generate_fibonacci(n):
fib_numbers = [0, 1]
while len(fib_numbers) < n:
fib_numbers.append(fib_numbers[-1] + fib_numbers[-2])
return fib_numbers[:n]
In this program:
Adjust the value of N as needed, and when you run the program, it will output the squared Fibonacci
numbers:
python
The first 10 Fibonacci numbers squared:
[0, 1, 1, 9, 25, 121, 361, 1444, 3249, 8100]
Q16: Implement a function that finds the largest e;ement in a list without using the max() function?
ANS:
def find_largest_element(lst):
# Check if the list is empty
if not lst:
return None
return largest
# Example usage:
my_list = [3, 8, 2, 7, 5, 1, 4]
result = find_largest_element(my_list)
print("List:", my_list)
print("Largest Element:", result)
Q17: Write a program that merges two dictionaries and handles duplicate keys intelligently.
ANS:
def merge_dictionaries(dict1, dict2, conflict_resolution=lambda x, y: x + y):
merged_dict = dict1.copy()
return merged_dict
# Example dictionaries
dict1 = {'a': 10, 'b': 20, 'c': 30}
dict2 = {'b': 25, 'c': 35, 'd': 40}
Q18: Develop a Python function that removes duplicates from a list while maintaining the original
order.
ANS:
def remove_duplicates(original_list):
seen = set()
unique_list = []
return unique_list
# Example usage:
my_list = [3, 2, 1, 2, 4, 3, 5, 4, 6]
result = remove_duplicates(my_list)
In this example:
• The remove_duplicates function takes a list (original_list) as input.
• It uses a set (seen) to keep track of elements that have already been encountered.
• It iterates through the original list, and if an element is not in the set, it adds it to both the
set and a new list (unique_list).
• The function returns the list with duplicates removed.
When you run this code with the provided example list, it will output:
mathematica
Original List: [3, 2, 1, 2, 4, 3, 5, 4, 6]
List with Duplicates Removed: [3, 2, 1, 4, 5, 6]
Q19: Write a Python program to create a lambda function that adds 15 to a given number passed in as
an argument, also create a lambda function that multiplies argument x with argument y and prints the
result
ANS:
# Lambda function to add 15 to a given number
add_15 = lambda x: x + 15
# Example usage
number_to_add_15 = 5
result_add_15 = add_15(number_to_add_15)
print(f"Adding 15 to {number_to_add_15}: {result_add_15}")
number1 = 3
number2 = 7
result_multiply = multiply(number1, number2)
print(f"Multiplying {number1} and {number2}: {result_multiply}")
In this program:
• The add_15 lambda function takes one argument x and adds 15 to it.
• The multiply lambda function takes two arguments x and y and multiplies them.
yaml
Adding 15 to 5: 20
Multiplying 3 and 7: 21
Q20: Write a Python function to calculate the factorial of a number (a non-negative integer). The
function accepts the number as an argument.
ANS:
def factorial(n):
# Base case: factorial of 0 or 1 is 1
if n == 0 or n == 1:
return 1
else:
# Recursive case: n! = n * (n-1)!
return n * factorial(n - 1)
# Example usage:
number = 5
result = factorial(number)
In this function:
When you run this code with the provided example (number = 5), it will output:
csharp
The factorial of 5 is: 120