Answer 1
Answer 1
Answer 1
ANSWER2
# Accept values from user in a tuple
user_tuple = tuple(input("Enter values for the tuple (comma-separated): ").split(","))
# Add a tuple to it
new_tuple = user_tuple + (10, 20, 30)
ANS3
ANS4
# Accept a particular class from the user and display the class teacher
selected_class = input("Enter the class for which you want to know the teacher: ")
if selected_class in class_dict:
print(f"Class: {selected_class}, Teacher: {class_dict[selected_class]}")
else:
print("Class not found in the dictionary.")
ANS5
ANS6
def display_customer_details(customers):
print("Customer Details:")
print("{:<10} {:<15} {:<10} {:<15}".format("Name", "Items Bought", "Cost", "Phone Number"))
for name, details in customers.items():
items_bought = details["items_bought"]
cost = details["cost"]
phone_number = details["phone_number"]
print("{:<10} {:<15} {:<10} {:<15}".format(name, items_bought, cost, phone_number))
# Input details for each customer and store them in the dictionary
for i in range(n):
print(f"\nEnter details for customer {i+1}:")
name = input("Name: ")
items_bought = input("Items Bought: ")
cost = float(input("Cost: "))
phone_number = input("Phone Number: ")
def capitalize_first_last_letters(string):
words = string.split() # Split the string into individual words
capitalized_words = []
# Capitalize the first and last letters of each word in the string
result = capitalize_first_last_letters(string)
Finally, the modified words are joined back into a string using the join() method
with a space as the separator. The resulting string is then displayed as the modified
string.
ANS8
def remove_duplicates(string):
# Create an empty set to store unique characters
unique_chars = set()
return result
The program iterates over each character in the input string. For each character, it
checks if the character is already in the unique_chars set. If the character is not in
the set, it means it is a unique character, so it adds the character to the set and
appends it to the result string.
After processing all characters in the input string, the function returns the result
string, which contains the input string with duplicate characters removed.
ANS9
def compute_digit_sum(string):
digit_sum = 0
return digit_sum
The program then iterates over each character in the input string. For each character,
it checks if the character is a digit using the isdigit() method. If the character is a
digit, it converts it to an integer using the int() function and adds it to the
digit_sum.
After processing all characters in the string, the function returns the digit_sum,
which represents the sum of digits in the given string.
ANS10
def find_second_most_repeated_word(string):
# Split the string into words
words = string.split()
return second_most_common_word
In this program, we utilize the Counter class from the collections module, which
provides a convenient way to count the frequency of elements in a list or any
iterable.
Finally, the program displays the second most repeated word as the output
ANS11
def exchange_first_and_last_chars(string):
# Check if the string has at least two characters
if len(string) >= 2:
# Swap the first and last characters using slicing
new_string = string[-1] + string[1:-1] + string[0]
return new_string
else:
# Return the string as is if it has less than two characters
return string
If the string has less than two characters, the function returns the string as it is, since
there are no characters to swap.
Finally, the program displays the new string with the first and last characters
exchanged as the output.
ANS12
def multiply_list_items(lst):
result = 1
for i in range(n):
element = int(input("Enter element {}: ".format(i+1)))
lst.append(element)
The program prompts the user to enter the number of elements in the list and then
asks for each element. The elements are appended to the list lst.
Next, the program calls the multiply_list_items() function, passing the list lst.
The function iterates over each item in the list and multiplies it with the result
variable, updating the result with each iteration.
After processing all the items, the function returns the final product stored in result.
Finally, the program displays the product of all the items in the list as the output.
ANS13
def get_smallest_number(lst):
if len(lst) == 0:
return None
smallest = lst[0]
return smallest
If the list is not empty, the function initializes the variable smallest with the first
number in the list. It then iterates over each number in the list and compares it with
the current smallest number. If a smaller number is found, the smallest variable is
updated.
After processing all the numbers in the list, the function returns the smallest number.
The program prompts the user to enter the number of elements in the list and then
asks for each element. The elements are appended to the list lst.
Finally, the program calls the get_smallest_number() function, passing the list lst.
It displays the smallest number in the list as the output, or a message indicating that
the list is empty if applicable.
ANS14
for i in range(n1):
element = int(input("Enter element {}: ".format(i+1)))
list1.append(element)
for i in range(n2):
element = int(input("Enter element {}: ".format(i+1)))
list2.append(element)
In this program, the append_lists() function takes two lists as input. It uses the
extend() method to append the second list to the first list. The modified first list is
then returned.
The program prompts the user to enter the number of elements in the first list,
followed by each element. The elements are appended to the list list1.
Similarly, the program asks for the number of elements in the second list and
prompts for each element, which are then appended to the list list2.
Next, the program calls the append_lists() function, passing the two lists list1
and list2. It assigns the returned appended list to the variable appended_list.
Finally, the program displays the resultant list after appending the second list to the
first list as the output.
ANS15
def generate_squared_list():
squared_list = [i**2 for i in range(1, 31)]
first_five = squared_list[:5]
last_five = squared_list[-5:]
return first_five, last_five
Finally, the program displays the first five elements and last five elements as the
output.
ANS16
def get_unique_values(input_list):
unique_values = list(set(input_list))
return unique_values
for i in range(n):
element = input("Enter element {}: ".format(i+1))
input_list.append(element)
The program prompts the user to enter the number of elements in the list, followed
by each element. The elements are appended to the list input_list.
Next, the program calls the get_unique_values() function, passing the input_list
as an argument. It assigns the returned list of unique values to the variable
unique_values.
Finally, the program displays the unique values extracted from the list as the output.
ANS17
def convert_string_to_list(input_string):
char_list = list(input_string)
return char_list
The program prompts the user to enter a string and assigns it to the variable
input_string.
Finally, the program displays the string converted to a list as the output. Each
character of the string will be represented as a separate element in the list.