Question-1. Write a program that asks the user to enter a list of at least five integers. Do the following: (a) Print out the total number of items in the list. (b) Print out the fourth item (index 3) in the list. (c) Print out the last three items in the list. (d) Print out all the items in the list except the first two. (e) Print out the list in reverse order. (f) Print out the largest and smallest values in the list. (g) Print out the sum of all the values in the list. (h) If the list contains a zero, print out the index of the first zero in the list, and otherwise print out a message saying there are no zeroes. (i) Sort the list and print out the list after sorting. (j) Delete the first item from the (now sorted) list and print out the new list. (k) Change the second-to-last item in the list to 9876 and print out the new list. (l) Append the value -500 to the end of the list and print out the new list.
input_str = input("Enter a list of at least five integers, separated by spaces: ")
input_list = input_str.split()
#print(input_str)
#print(input_list)
input_list = [int(x) for x in input_list]
#print(input_list)
Enter a list of at least five integers, separated by spaces: 10 20 30 40 50 10 20 30 40 50 ['10', '20', '30', '40', '50'] [10, 20, 30, 40, 50]
print("Total number of items:", len(input_list))
Total number of items: 5
print("Fourth item:", input_list[3])
Fourth item: 40
print("Last three items:", input_list[-3:])
Last three items: [30, 40, 50]
print("Items except the first two:", input_list[2:])
Items except the first two: [30, 40, 50]
print("Reversed list:", input_list[::-1])
Reversed list: [50, 40, 30, 20, 10]
print("Largest value:", max(input_list))
print("Smallest value:", min(input_list))
Largest value: 50 Smallest value: 10
print("Sum of values:", sum(input_list))
Sum of values: 150
if 0 in input_list:
print("Index of first zero:", input_list.index(0))
else:
print("No zeroes in the list.")
No zeroes in the list.
sorted_list = sorted(input_list)
print("Sorted list:", sorted_list)
Sorted list: [10, 20, 30, 40, 50]
del sorted_list[0]
print("List after deleting first item:", sorted_list)
List after deleting first item: [20, 30, 40, 50]
sorted_list[-2] = 9876
print("List after changing second-to-last item:", sorted_list)
List after changing second-to-last item: [20, 30, 9876, 50]
sorted_list.append(-500)
print("List after appending -500:", sorted_list)
List after appending -500: [20, 30, 9876, 50, -500]
Question-2. Write a program that asks the user to enter a list of numbers. Then print out the smallest thing in the list and the first index at which it appears in the list.
input_str = input("Enter a list of numbers, separated by spaces: ")
input_list = input_str.split()
input_list = [float(x) for x in input_list]
Enter a list of numbers, separated by spaces: 10 32 30 40 50
smallest_value = min(input_list)
first_index = input_list.index(smallest_value)
print("Smallest value:", smallest_value)
print("First index of the smallest value:", first_index)
Smallest value: 10.0 First index of the smallest value: 0
Question-3. Write a program that asks the user to enter a string of lowercase letters and creates a list containing counts of how many times each letter appears in the string. The first index is how many a’s are in the string, the second is how many b’s, etc.
input_str = input("Enter a string of lowercase letters: ")
Enter a string of lowercase letters: vikasmaddala
counts = [0] * 26
for char in input_str:
if 'a' <= char <= 'z':
counts[ord(char) - ord('a')] += 1
for i, count in enumerate(counts):
letter = chr(i + ord('a'))
print(f"Number of {letter}'s: {count}")
Number of a's: 4 Number of b's: 0 Number of c's: 0 Number of d's: 2 Number of e's: 0 Number of f's: 0 Number of g's: 0 Number of h's: 0 Number of i's: 1 Number of j's: 0 Number of k's: 1 Number of l's: 1 Number of m's: 1 Number of n's: 0 Number of o's: 0 Number of p's: 0 Number of q's: 0 Number of r's: 0 Number of s's: 1 Number of t's: 0 Number of u's: 0 Number of v's: 1 Number of w's: 0 Number of x's: 0 Number of y's: 0 Number of z's: 0
Question-4. Create a dictionary whose keys are the strings 'abc', 'def', 'ghi', 'jkl', and 'mno' and whose corresponding values are 7, 11, 13, 17, and 19. Then write dictionary code that does the following: (a) Print the value in the dictionary associated with the key 'def'. (b) Use the keys() method to print out all the keys. (c) Loop over the dictionary and print out all the keys and their associated values. (d) Use an if statement to check if the dictionary contains the key 'pqr' and print out an appropriate message indicating whether it does or doesn’t. (e) Change the value associated with the key 'abc' to 23 and then print out all the values in the dictionary using the values() method.
my_dict = {'abc': 7, 'def': 11, 'ghi': 13, 'jkl': 17, 'mno': 19}
print("Value associated with 'def':", my_dict['def'])
Value associated with 'def': 11
print("All keys:")
for key in my_dict.keys():
print(key)
All keys: abc def ghi jkl mno
print("Keys and their associated values:")
for key, value in my_dict.items():
print(f"Key: {key}, Value: {value}")
Keys and their associated values: Key: abc, Value: 7 Key: def, Value: 11 Key: ghi, Value: 13 Key: jkl, Value: 17 Key: mno, Value: 19
if 'pqr' in my_dict:
print("'pqr' is present in the dictionary.")
else:
print("'pqr' is not present in the dictionary.")
'pqr' is not present in the dictionary.
my_dict['abc'] = 23
print("Updated values:")
for value in my_dict.values():
print(value)
Updated values: 23 11 13 17 19