"""
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.
"""
a = []
print(" Enter 9 Integers")
for i in range(9):
x=int(input())
a.append(x)
print(a)
Enter 9 Integers 12 54 8 64 34 7 6 148 34 [12, 54, 8, 64, 34, 7, 6, 148, 34]
print(len(a)) #(a) Print out the total number of items in the list.
9
print(a[3]) #(b) Print out the fourth item (index 3) in the list.
64
print(a[-3:]) #(c) Print out the last three items in the list.
[6, 148, 34]
print(a[2:]) # (d) Print out all the items in the list except the first two.
[8, 64, 34, 7, 6, 148, 34]
print(a[::-1]) # (e) Print out the list in reverse order.
[34, 148, 6, 7, 34, 64, 8, 54, 12]
print(min(a),max(a)) # (f) Print out the largest and smallest values in the list.
6 148
print(sum(a)) # (g) Print out the sum of all the values in the list.
367
# (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.
if 0 in a:
print('Index of value 0 is: ',end=' ')
for i in range(len(a)):
if(a[i]==0):
print(i)
break
else:
print('There are no zero in the list')
a.sort() #(i) Sort the list and print out the list after sorting.
print(a)
[6, 7, 8, 12, 34, 34, 54, 64, 148]
a.pop(0) # (j) Delete the first item from the (now sorted) list and print out the new list.
print(a)
[7, 8, 12, 34, 34, 54, 64, 148]
a[-2]=9876 # (k) Change the second-to-last item in the list to 9876 and print out the new list.
print(a)
[7, 8, 12, 34, 34, 54, 9876, 148]
a.append(-500) # (l) Append the value -500 to the end of the list and print out the new list.
print(a)
[7, 8, 12, 34, 34, 54, 9876, 148, -500]
# 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.
a=[]
b=int(input('Enter the no of elements :'))
for i in range(b):
c=int(input())
a.append(c)
print('The smallest value in the list is: ',end=' ')
print(min(a))
for i in range(b):
if a[i]==min(a):
print('The index of smallest value is: ',end=' ')
print(i)
break
Enter the no of elements :5 2 45 0 58 275 The smallest value in the list is: 0 The index of smallest value is: 2
# 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.
x=[]
y=input('Enter a string in lower case: ')
for i in range(97,123):
x.append(y.count(chr(i)))
print(x)
Enter a string in lower case: jntuh college of engineering manthani [2, 0, 1, 0, 5, 1, 3, 2, 3, 1, 0, 2, 1, 6, 2, 0, 0, 1, 0, 2, 1, 0, 0, 0, 0, 0]
"""
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.
"""
# a) Printing the value of key 'def'
dict={'abc':5,'def':7,'ghi':9,'jkl':11,'mno':13}
k=dict['def']
print(k)
7
# (b) Use the keys() method to print out all the keys.
print(dict.keys())
dict_keys(['abc', 'def', 'ghi', 'jkl', 'mno'])
# (c) Loop over the dictionary and print out all the keys and their associated values.
for key,value in dict.items():
print(key,':',value)
abc : 5 def : 7 ghi : 9 jkl : 11 mno : 13
# (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 dict.keys():
print('key pqr contains in the dictionary')
else:
print('key pqr does not contain n the dictionary')
key pqr does not contain n the dictionary
# (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.
dict['abc']=23
print(dict.values())
dict_values([23, 7, 9, 11, 13])