Computer Science
What will the following snippet print?
L5 = [1500, 1800, 1600, 1200, 1900]
begin = 1
sum = 0
for c in range(begin, 4):
sum = sum + L5[c]
print(c, ':', sum)
sum = sum + L5[0] * 10
print("sum is", sum)
Answer
Output
1 : 1800
sum is 16800
2 : 18400
sum is 33400
3 : 34600
sum is 49600
Working
The given code initializes a list L5 with five elements. It then sets begin to 1 and sum to 0. The loop iterates for values 1, 2, and 3. In each iteration, the code adds the element at index c of L5 to sum, prints the c and updated sum, and then adds ten times the first element of L5 to sum and prints the value of sum.
Related Questions
Given is a Python string declaration:
voice = "Python for All Learners"Write the output of:
print(voice[20 : : -2])Write the output of the code given below:
d1 = {"A" : "Avocado", "B" : "Banana"} d2 = {'B' : 'Bag', 'C' : 'Couch'} d2.update(d1) print(len(d2))Consider the following two SQL commands with reference to a table, named MOVIES, having a column named Director:
(a) Select Distinct Director from MOVIES;
(b) Select Director from MOVIES;
- In which case will these two commands produce the same result?
- In which case will these two commands produce different results?
Write a function WordsList(S), where S is a string. The function returns a list, named Words, that stores all the words from the string which contain 'r'.
For example, if S is "Dew drops were shining in the morning", then the list Words should be: ['drops', 'were', 'morning']