print("Assignment 2- Q1")
list_integers = [int(x) for x in input("Please enter a list of at least five integers: ").split()]
# (a)
print("The total number of items in the list is:", len(list_integers))
# (b)
print("The fourth item in the list is:", list_integers[3])
# (c)
print("The last three items in the list are:", list_integers[-3:])
# (d)
print("All the items in the list except the first two are:", list_integers[2:])
# (e)
print("The list in reverse order is:", list_integers[::-1])
# (f)
print("The largest value in the list is:", max(list_integers))
print("The smallest value in the list is:", min(list_integers))
# (g)
print("The sum of all the values in the list is:", sum(list_integers))
# (h)
if 0 in list_integers:
print("The index of the first zero in the list is:", list_integers.index(0))
else:
print("There are no zeroes in the list.")
# (i)
list_integers.sort()
print("The list after sorting is:", list_integers)
# (j)
list_integers.pop(0)
print("The new list after deleting the first item from the sorted list is:", list_integers)
# (k)
list_integers[-2] = 9876
print("The new list is: ", list_integers)
# (l)
list_integers.append(-500)
print("The new list is: ", list_integers)
Assignment 2- Q1 Please enter a list of at least five integers: 1 2 3 4 5 The total number of items in the list is: 5 The fourth item in the list is: 4 The last three items in the list are: [3, 4, 5] All the items in the list except the first two are: [3, 4, 5] The list in reverse order is: [5, 4, 3, 2, 1] The largest value in the list is: 5 The smallest value in the list is: 1 The sum of all the values in the list is: 15 There are no zeroes in the list. The list after sorting is: [1, 2, 3, 4, 5] The new list after deleting the first item from the sorted list is: [2, 3, 4, 5] The new list is: [2, 3, 9876, 5] The new list is: [2, 3, 9876, 5, -500]
print("Assignment 2- Q2")
list_of_numbers = input("Enter a list of numbers: ")
list_of_numbers = list_of_numbers.split()
list_of_numbers = [int(x) for x in list_of_numbers]
smallest_number = min(list_of_numbers)
index_of_smallest = list_of_numbers.index(smallest_number)
print("The smallest number in the list is {} and it is located at index {}".format(smallest_number, index_of_smallest))
Assignment 2- Q2 Enter a list of numbers: 23 45 33 21 2 45 67 78 90 100 The smallest number in the list is 2 and it is located at index 4
print("Assignment 2- Q3")
string = input("Enter a string of lowercase letters: ")
letter_counts = [0] * 26
for letter in string:
letter_index = ord(letter) - ord('a')
letter_counts[letter_index] += 1
print(letter_counts)
Assignment 2- Q3 Enter a string of lowercase letters: maruthi [1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0]
print("Assignment 2- Q4")
d = {'abc': 7, 'def': 11, 'ghi': 13, 'jkl': 17, 'mno': 19}
# (a) Print the value in the dictionary associated with the key 'def'.
print(d['def'])
# (b) Use the keys() method to print out all the keys.
print(d.keys())
# (c) Loop over the dictionary and print out all the keys and their associated values.
for key, value in d.items():
print(key, ':', value)
# (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.
if 'pqr' in d:
print('The dictionary contains the key "pqr"')
else:
print('The dictionary does not contain the key "pqr"')
# (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.
d['abc'] = 23
print(d.values())
Assignment 2- Q4 11 dict_keys(['abc', 'def', 'ghi', 'jkl', 'mno']) abc : 7 def : 11 ghi : 13 jkl : 17 mno : 19 The dictionary does not contain the key "pqr" dict_values([23, 11, 13, 17, 19])