Question

In: Computer Science

Which of the following will give you a sum of numbers equal to 15 1. s...

Which of the following will give you a sum of numbers equal to 15

1.

s = 0
for i in [9,5,1]:
    s = s + i

2.

All of the above.

3.

sum(list((9,5,1)))

4.

None of the above.

5.

s = 0
for i in [9,5,1]:
    s += i

Solutions

Expert Solution

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.


Related Solutions

1. A) How many three-digit numbers are there for which the sum of the digits is...
1. A) How many three-digit numbers are there for which the sum of the digits is at least 25? B) How many three-digit numbers can be formed if only odd numbers are allowed to be re-used Please combinatorics principles where applicable.
Sum of numbers between 1 and 10
Calculate the values from the  smallest number to the largest number
Q2: The following code is supposed to return the sum of the numbers between 1 and...
Q2: The following code is supposed to return the sum of the numbers between 1 and n inclusive, for positive n. An analysis of the code using our "Three Question" approach reveals that: int sum(int n){ if (n == 1)     return 1; else     return (n + sum(n - 1)); } Answer Choices: it passes on all three questions and is a valid algorithm.     it fails the smaller-caller question.     it fails the base-case question.     it fails...
Input 10 integers and display the following: a. the sum of even numbers. b. the sum...
Input 10 integers and display the following: a. the sum of even numbers. b. the sum of odd numbers. c. the largest integer d. the smallest integer e. the total count of even numbers f. the total count of odd numbers. Using C++ program with for loop..
For which of the following reactions is ΔH∘rxn equal to ΔH∘f of the product(s)? You do...
For which of the following reactions is ΔH∘rxn equal to ΔH∘f of the product(s)? You do not need to look up any values to answer this question. Check all that apply. Hints Check all that apply. S(s)+O2(g)→SO2(g) SO(g)+12O2(g)→SO2(g) Li(s)+12Cl2(l)→LiCl(s) Li(s)+12Cl2(g)→LiCl(s) SO3(g)→12O2(g)+SO2(g) 2Li(s)+Cl2(g)→2LiCl(s)
For which of the following reactions is ?H?rxn equal to ?H?f of the product(s)? You do...
For which of the following reactions is ?H?rxn equal to ?H?f of the product(s)? You do not need to look up any values to answer this question. Check all that apply. SO(g)+12O2(g)?SO2(g) 2Na(s)+F2(g)?2NaF(s) Na(s)+12F2(g)?NaF(s) S(s)+O2(g)?SO2(g) Na(s)+12F2(l)?NaF(s) SO3(g)?12O2(g)+SO2(g)
What is the 8-bit sum of the following 2’s complement numbers: 11101100+00001101? Show your work.
What is the 8-bit sum of the following 2’s complement numbers: 11101100+00001101? Show your work.
How many sets of five numbers from 1 to 15 can you make in which exactly...
How many sets of five numbers from 1 to 15 can you make in which exactly two of the numbers are divisible by 3?
create two random numbers between 1 and 6. if when the sum of two numbers are...
create two random numbers between 1 and 6. if when the sum of two numbers are added togethere their sum is less than 5 or greater than 12, output to the console: "you win". if is not, output "you lose" C++
Problem 6.3 Sum of Numbers - Similar to Problems 6.1 & 6.2, you must sum up...
Problem 6.3 Sum of Numbers - Similar to Problems 6.1 & 6.2, you must sum up 5 numbers. However, you will obtain these numbers from a list box for which you have used the String Collection Editor to insert a number on each of 5 lines in the box. You may use any type of loop. The result is displayed in a text box. The program is initiated with "Go" button. Problem 6.3 does NOT require an Input Dialog Box....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT