In: Computer Science
Which of the following will give you a sum of numbers equal to 15
1. |
s = 0 |
|
2. |
All of the above. |
|
3. |
sum(list((9,5,1))) |
|
4. |
None of the above. |
|
5. |
s = 0 |
If we want to iterate over a sequence like a list, a tuple, a dictionary, a set or a
The for keyword used like other programming languages.
We can execute a set of statements, once for each item in a list, tuple, set etc. using the for loop.
Syntax of for Loop
for val in sequence:
Body of for
Option 1: Correct
s = 0 // initialization of sum
variable
for i in [9, 5, 1]:// iterate over a list one by one
s = s + I // add value into variable s which is
sum of number.
Option 3: Correct
The sum () function returns the sum by adding the items of an iterable ((list, tuple, dict, etc). The items of the iterable should be numbers.
Option 5: Correct
The expression s += i is shorthand for s = s + i.So code is similar to option 1
Thus Answer is
2. |
All of the above. |
If at any point you find any difficulty feel free ask me.