Sem (Iv) (Aktu) Theory Examination 2023-24 Solution
Sem (Iv) (Aktu) Theory Examination 2023-24 Solution
PYTHON PROGRAMMING
Dynamic typing in Python means that the type of a variable is determined at runtime, not at compile time.
This allows you to assign different types of data to the same variable without explicitly declaring its type.
x = 10 # x is an integer
print(x) # Output: 10
x = "Hello" # x is now a string
print(x) # Output: Hello
In this example, the variable x is first assigned an integer value and then a string value. Python dynamically
changes the type of x based on the value assigned to it.
Explain how to define a list in Python. Write a Python program to remove duplicates
A list in Python is defined by enclosing elements in square brackets [], separated by commas.
Example
my_list = [1, 2, 2, 3, 4, 4, 5]
Explain the concept of functions in Python. Write a function that takes a list of numbers and returns the sum
of all the numbers in the list.
A function is a block of reusable code that performs a specific task. Functions are defined using the def
keyword, followed by the function name and parentheses ().
Write a Python program to read a file named "ABC.txt" and count the number of lines, words, and characters
in the file.
Explain the basic usage of matplotlib for plotting graphs. Write a Python program to plot a simple line graph
showing the relationship between
X = [1, 2, 3, 4, 5 ]
x=[1,2,3,4,5] and y = [1,4,9,16,25]
y=[1,4,9,16,25].
Matplotlib is a plotting library in Python used for creating static, animated, and interactive visualizations. The
basic steps to create a plot are:
Write a program that takes two strings and checks for common letters in both the strings.
Write a Python Program to find the sum of all the items in a dictionary.
# Example dictionary
d = {'A': 100, 'B': 540, 'C': 239}
# Calculate and print the sum
total_sum = sum(d.values())
print(f"The sum of all the items in the dictionary is: {total_sum}")
A set in Python is an unordered collection of unique elements. Sets are mutable, meaning they can be
changed after they are created. The key characteristics of sets are:
Elements can be added to a set using the add() method for a single element or the update() method for
multiple elements.
# Creating a set
my_set = {1, 2, 3}
# Adding elements
my_set.add(4)
my_set.update([5, 6])
Elements can be removed using the remove() or discard() methods. The remove() method raises an error if
the element is not found, while discard() does not.
# Creating a set
my_set = {1, 2, 3}
# Removing elements
my_set.remove(3)
my_set.discard(7) # No error even if 7 is not in the set
Explain how lambda functions can be used within a list comprehension. Write a Python program that uses a
lambda function within a list comprehension to convert a list of temperatures in Celsius to Fahrenheit.
Lambda functions are anonymous functions defined using the lambda keyword. They can be used within list
comprehensions to apply a function to each element in a list.
r Open text file for reading. Raises an I/O error if the file does not exist.
w Open the file for writing. Truncates the file if it already exists. Creates a new file if it does not exist.
a Open the file for writing. The data being written will be inserted at the end of the file. Creates a new
file if it does not exist.
r+ Open the file for reading and writing. Raises an I/O error if the file does not exist.
w+ Open the file for reading and writing. Truncates the file if it already exists. Creates a new file if it
does not exist.
a+ Open the file for reading and writing. The data being written will be inserted at the end of the file.
Creates a new file if it does not exist.
rb Open the file for reading in binary format. Raises an I/O error if the file does not exist.
wb Open the file for writing in binary format. Truncates the file if it already exists. Creates a new file if it
does not exist.
ab Open the file for appending in binary format. Inserts data at the end of the file. Creates a new file if
it does not exist.
rb+ Open the file for reading and writing in binary format. Raises an I/O error if the file does not exist.
wb+ Open the file for reading and writing in binary format. Truncates the file if it already exists. Creates
a new file if it does not exist.
ab+ Open the file for reading and appending in binary format. Inserts data at the end of the file. Creates
a new file if it does not exist.
Program
with open('example.txt', 'r') as file:
content = file.read()
capitalized_content = ' '.join(word.capitalize() for word in content.split())
with open('example.txt', 'w') as file:
file.write(capitalized_content)
Generators are a type of iterable, like lists or tuples, but they do not store all their values in memory at once.
Instead, they generate values on the fly using the yield keyword. This makes them memory efficient for large
datasets.
Generators are created using functions that contain the yield keyword. When the function is called, it returns
a generator object.
def simple_generator():
yield 1
yield 2
yield 3
# Using the generator
gen = simple_generator()
for value in gen:
print(value) # Output: 1 2 3
Describe how to generate random numbers using NumPy. Write a Python program to create an array of 5
random integers between 10 and 50.
NumPy provides the random module to generate random numbers. The randint() function can be used to
generate random integers within a specified range.
Output
The == operator helps us compare the equality of objects. The is operator helps us check whether different
variables point towards a similar object in the memory
You can use the chr() function to convert an ASCII value to its corresponding character.
ascii_value = 65
character = chr(ascii_value)
print(character) # Output: A
Can you use else with a for loop? If so, when is it executed?
Yes, you can use else with a for loop in Python. The else block is executed when the loop has exhausted
iterating over the items, i.e., when the loop completes normally without encountering a break statement.
for i in range(3):
print(i)
else:
print("Loop completed") # This will be executed after the loop finishes
The split() method is used to split a string into a list of substrings based on a specified delimiter. If no
delimiter is specified, it defaults to splitting on whitespace.
What does the readline() function return when it reaches the end of a file?
The readline() function returns an empty string ('') when it reaches the end of a file.
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]