Reference no: EM132356936
Answer the following Questions :
Problem 1. What will be the output of the following code?
numbers = [10] * 5
print(numbers)
Problem 2. What will be the output of the following code?
numbers = list(range(1, 20, 2))
print(numbers)
Problem 3. What will be the output of the following code?
numbers = [1, 2, 3, 4, 5]
print(numbers[-2])
Problem 4. What will be the output of the following code?
numbers1 = [1, 2, 3]
numbers2 = [10, 20, 30]
numbers2 += numbers1
print(numbers1)
print(numbers2)
Problem 5. What will be the output of the following code?
numbers = [1, 2, 3, 4, 5]
myList = numbers[:]
print(myList)
Problem 6. What will be the output of the following code?
numbers = [1, 2, 3, 4, 5]
myList = numbers[-3:]
print(myList)
Problem 7. What will be the output of the following code?
names = ['Jim', 'Jill', 'John', 'Jasmine']
if 'Jasmine' not in names:
print('Cannot find Jasmine')
else:
print('Jasmine\'s family:')
print(names)
Problem 8. What will happen if you try to access a list index which is out of range?
a) ValueError exception
b) IndexError exception
c) The list will be erased and the program will continue to run
d) The invalid index will be ignored and the program will continue to run
Problem 9. Assume the following statement appears in a program:
myList = [ ]
Which of the following statements should you use to add the string Labrador to the list at index 0?
a) myList[0] = 'Labrador'
b) myList.insert(0, 'Labrador')
c) myList.append('Labrador')
d) myList.insert('Labrador', 0)
Problem 10. Given lst = [30, 1, 2, 1, 0], what will lst look like after applying each of the following statements? Assume each line of code to be independent.
a) lst.append(40)
b) lst.insert(1, 43)
c) lst.extend([1, 43])
d) lst.remove(1)
e) lst.pop(1)
f) lst.pop()
g) lst.sort()
h) lst.reverse()
i) random.shuffle(lst)
Problem 11. Given lst = [30, 1, 2, 1, 0], what is the return value for each of the following statements? Assume each line of code to be independent.
a) lst.index(1)
b) lst.count()
c) len(lst)
d) max(lst)
e) min(lst)
f) sum(lst)
Problem 12. What will be the list for each of the following statements?
a) [x for x in lst if x > 1] # Assume lst = [30, 1, 2, 1, 0]
b) [x for x in range(0, 10, 2)]
c) [x for x in range(10, 0, -2)]
Problem 13. Given list1 = [30, 1, 2, 1, 0] and list2 = [1, 21, 13], what will the following statements evaluate to?
a) list1 < list2
b) list1 <= list2
c) list1 == list2
d) list1 != list2
e) list1 > list2
f) list1 >= list2
Problem 14. Ask the user to enter a store's sales for each day of the week. Use a loop to store the amounts in a list. Use a loop to calculate the total sales for the week and display the result. [Note: You are not allowed to use the sum() function.]
Sample program output:
Enter sales for day 1: 2000
Enter sales for day 2: 1000
Enter sales for day 3: 2500
Enter sales for day 4: 3400
Enter sales for day 5: 4600
Enter sales for day 6: 1100
Enter sales for day 7: 2200
Total sales for the week: 16800
Problem 15. Ask the user to enter a series of 10 numbersseparated by space. The program should store the numbers in a list and then display the following:
• The lowest number in the list
• The highest number in the list
• The total of the numbers in the list
• The average of the numbers in the list
[Note: You may make use of inbuilt functions.]
Sample program output:
Enter 10 numbers (separate by space): 21 54 77 10 34 45 91 66 2 12
Lowest: 2
Highest: 91
Total: 412
Average: 41.2
Problem 16. Write a python script to check whether a list is already sorted in decreasing order or not.
Sample program output I:
Enter some numbers: 3 2 1 4 5 6
The list is not sorted
Sample program output II:
Enter some numbers: 45 34 23 4
The list is sorted
Problem 17. Ask the user to enter his first name and surname on a single line. Save the first name and surname in a list and print out the list. Ask the user to enter middle name. Insert the middle name in the list in the proper position and print the list again.
Sample program output:
Enter first name and surname: Harry Potter
[‘Harry', ‘Potter']
Enter middle name: James
[‘Harry', ‘James', ‘Potter']
Problem 18. Write a python script to generate 1000 random digits (0 - 9) and display the count for each digit.
[Hint: Use a list of size 10 initialized to all 0s, where each index corresponds to the digit, and update the counts of the digits accordingly.]
Sample program output:
0: 108
1: 113
2: 98
3: 98
4: 100
5: 96
6: 91
7: 95
8: 106
9: 95
Problem 19 (Extra credit). Write a python script to enter a string of characters (without any whitespace, but can include digits). Print out a list of the distinct letters from the entered string.
[Hint: Start with an empty list. Read each character from the string and add it to the list only if it is a letter (read up on isalpha()), and it is not in the list.]
Sample program output:
Enter a string: abBA113av6
The distinct letters are [‘a', ‘b', ‘B', ‘A', ‘v']