[go: up one dir, main page]

0% found this document useful (0 votes)
74 views12 pages

Vityarthi Module 5 Solution

The document contains multiple worksheets with questions and solutions related to Python programming concepts, including data types, functions, and algorithms. It features multiple-choice questions, fill-in-the-blank exercises, matching questions, and coding tasks with provided solutions. Key topics include dictionaries, recursion, base conversion, and error correction in Python code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views12 pages

Vityarthi Module 5 Solution

The document contains multiple worksheets with questions and solutions related to Python programming concepts, including data types, functions, and algorithms. It features multiple-choice questions, fill-in-the-blank exercises, matching questions, and coding tasks with provided solutions. Key topics include dictionaries, recursion, base conversion, and error correction in Python code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

NAME:V.

ASHWITH
REG NO:24MIM1021

Module 5 – Worksheet 1

Multiple Choice Questions

Q.No 1. Which typecode is used to represent signed integer of 1 Byte?


a) b
b) i
c) B
d) I

Answer: a) b

Q.No 2. Tuple Objects are mutable?


a) True
b) False
Answer: b) False

Q.No 3. What is the result of y=x*3 where x=[2,3]?


a) [2,3,3]
b) [6,9]
c) [2,3,2,3,2,3]
d) [2,3][2,3][2,3]

Answer: c) [2,3,2,3,2,3]

Q.No 4. Square brackets in an assignment statement will create which type of data structure?
( s=[] )

a) List
b) queue
c) set
d) dictionary
Answer: a) List

Q.No 5. To insert an the string "strawberries" in the first position of a list we use
a) fruit.append("strawberries, 1")
b) fruit.insert("strawberries",0)
c) fruit.insert(1, "strawberries")
d) fruit.insert(0, "strawberries")
Answer: d) fruit.insert(0, "strawberries")

Q.No 6. Select the all correct way to remove the key marks from a dictionary
a) student.popitem("marks")
b) student.pop("marks")
c) del student["marks"]
d) student.remove("marks")

e) Answer: b) student.pop("marks")

Q.No 7. In Python, Dictionaries are immutable


a) True
b) False
Answer: b) False

Q.No 8. Items are accessed by their position in a dictionary and All the keys in a dictionary
must be of the same type.
a) True
b) False
Answer: b) False

Q.No 9. The "pass" statement in Python is used for?


a) Terminating a loop
b) Skipping the current iteration of a loop
c) Creating an empty function or class
d) Exiting the program

Answer: c) Creating an empty function or class


Q.No 10. What is the purpose of the base conversion algorithm?
a) Sorting
b) Changing the data type of a variable
c) Converting a number from one numeral system to another
d) Calculating the power of a number

Answer: c) Converting a number from one numeral system to another

Module 5 – Worksheet 2
Fill in the blanks

Q. No1. Dictionaries use _________ and keys are separated with _________

Answer: curly braces {} , colon :

Q.No.2. Comparing a dictionary to Python to a dictionary for words, ____________ is each word we
look up_______ is definition of the word

Answer: the key , the value

Q.No.3. ______________ can be strings, integers and lists

Answer: values in a dictionary

Q.No.4. The keyword used for conditional statements with multiple branches in Python is _______.

Answer: elif

Q.No.5. The _______ algorithm is used for reversing a list in Python.

Answer: slicing

Match the following


PART A PART B ANSWER
(1) Factorial Computation (A) Changing the numeral system of a number

(2) Iteration (B) Early termination of a loop

(3) Base Conversion (C) Computing the product of consecutive integers


(4) if-elif-else (D) Conditional statements with multiple branches
(5) Reverse (E) Inverting the order of elements in a sequence

Answer:

PART A PART B ANSWER


(1 Factorial Computation (A) Changing the numeral system of a number (1) – (C)
)

(2 Iteration (B) Early termination of a loop (2) – (B)


)
(3 Base Conversion (C) Computing the product of consecutive integers (3) – (A)
)
(4 if-elif-else (D) Conditional statements with multiple branches (4) – (D)
)
(5 Reverse (E) Inverting the order of elements in a sequence (5) – (E)
)

Module 5 – Worksheet 3
Q.No 1 : Write a Python function to calculate the factorial of a given number using recursion.
Solution:
def factorial(n):
# Base case: if n is 0 or 1, return 1
if n == 0 or n == 1:
return 1
# Recursive case: n! = n * (n-1)!
else:
return n * factorial(n - 1)

# Example usage
num = int(input("Enter a number: "))
print(f"The factorial of {num} is {factorial(num)}")

Q.No 2: Implement a Python program to find the sum of the Fibonacci sequence up to the nth
term, where n is provided by the user.
Solution:
def fibonacci_sum(n):
if n <= 0:
return 0
elif n == 1:
return 1

a, b = 0, 1
total_sum = a + b

for _ in range(2, n):


a, b = b, a + b
total_sum += b

return total_sum

# Example usage
n = int(input("Enter the number of terms: "))
print(f"The sum of the first {n} terms of the Fibonacci sequence is {fibonacci_sum(n)}")

Q. No 3 Develop a Python function that reverses a string without using any built-in reverse
function.
Solution:
def reverse_string(s):
reversed_s = ""
for char in s:
reversed_s = char + reversed_s
return reversed_s

# Example usage
input_string = input("Enter a string: ")
print("Reversed string:", reverse_string(input_string))

Q. No: 4. Create a Python program that converts a decimal number to binary using the base
conversion algorithm
Solution:
def decimal_to_binary(n):
if n == 0:
return "0"
binary = ""
while n > 0:
binary = str(n % 2) + binary
n = n // 2
return binary
# Example usage
decimal_number = int(input("Enter a decimal number: "))
binary_number = decimal_to_binary(decimal_number)
print(f"The binary equivalent of {decimal_number} is {binary_number}")

Q. No: 5. Write a Python function that takes a character as input and returns its ASCII value
using character to number conversion.

Solution:

def char_to_ascii(char):
return ord(char)

# Example usage
input_char = input("Enter a character: ")
ascii_value = char_to_ascii(input_char)
print(f"The ASCII value of '{input_char}' is {ascii_value}")
Q. No: 6. The following Python code is intended to print the sum of all even numbers from 1
to 10. Identify and fix the error

sum_even = 0
for i in range(1, 11):
if i % 2 == 0:
sum_even += i
print("Sum of even numbers:", sum_even)
Solution:
The given Python code is already correct and does not contain any errors. It correctly
calculates the sum of all even numbers from 1 to 10. Here's the code:

sum_even = 0
for i in range(1, 11):
if i % 2 == 0:
sum_even += i
print("Sum of even numbers:", sum_even)

When you run this code, it will output:


Sum of even numbers: 30

Q. No: 7. There's an error in the following Python code that is supposed to calculate the
factorial of a given number. Identify and correct the error

def factorial(n):
result = 1
for i in range(1, n+1):
result *= i
return result
num = input("Enter a number: ")
print("Factorial:", factorial(num))
Solution:
The error in the provided code is that the input num is read as a string, but it needs to be
converted to an integer before passing it to the factorial function. Here is the corrected code:

def factorial(n):

result = 1

for i in range(1, n+1):

result *= i

return result

num = int(input("Enter a number: ")) # Convert input to integer

print("Factorial:", factorial(num))

The change made is converting the input num to an integer using int(). This ensures that the
factorial function receives an integer argument, which is necessary for the calculation.

Q. No 8. The following Python function is meant to reverse a list, but there's an error.
Identify and fix the issue
def reverse_list(lst):
return lst[::-1]
original_list = [1, 2, 3, 4, 5]
reversed_list = reverse_list(original_list)
print("Reversed List:", reversed_list)
Solution:
The provided Python function reverse_list is actually correct and should work as intended to
reverse a list. Here's the code for reference:

def reverse_list(lst):

return lst[::-1]

original_list = [1, 2, 3, 4, 5]

reversed_list = reverse_list(original_list)

print("Reversed List:", reversed_list)

When you run this code, it will output:

Reversed List: [5, 4, 3, 2, 1]

Q. No 9. This Python program is intended to convert a given decimal number to binary.


However, there's a mistake. Find and correct the error
def decimal_to_binary(decimal):
return bin(decimal)
num = int(input("Enter a decimal number: "))
binary_num = decimal_to_binary(num)
print("Binary equivalent:", binary_num)
Solution:

The provided code has a small issue: the bin() function returns a binary string prefixed with
0b. To get a binary string without the prefix, you can slice the result. Here's the corrected
code:
def decimal_to_binary(decimal):

return bin(decimal)[2:]

num = int(input("Enter a decimal number: "))

binary_num = decimal_to_binary(num)

print("Binary equivalent:", binary_num)

In this corrected code, bin(decimal)[2:] removes the 0b prefix from the binary string, giving
you the binary representation without the prefix.

For example, if you input 10, the output will be:

Binary equivalent: 1010

Q. No 10. The following Python code is intended to check if a given number is positive,
negative, or zero. However, there's a logical error. Identify and fix the issue
num = int(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num < 0:
print("Negative number")
else:
print("Zero")
Solution:

The provided code snippet is logically correct for checking if a given number is positive,
negative, or zero. It will produce the correct output based on the input number.

Here is the code again for reference:

python

num = int(input("Enter a number: "))


if num > 0:

print("Positive number")

elif num < 0:

print("Negative number")

else:

print("Zero")

The logic of the code is:

 If num is greater than 0, it prints "Positive number".

 If num is less than 0, it prints "Negative number".

 If num is exactly 0, it prints "Zero".

You might also like