The program asks for a range of integers, asking the user to give the lower and upper value of the range and then prints the sum of the numbers in that range in an inclusive manner.

sumofnumatom sumofnumcy

The new code I used for this assignment was range()lists and a for- loop.

Range() is a function that, as the name says, makes a range of values based on the values given: range(x,y) were x is the lower value and y is the upper value. Usually, the upper value is not inclusive, so in my code I added 1 to the lower value to make it inclusive.

A sequence is a data structure. Lists are a type of sequence that assigns a number (its position or index) to each element inside of it. The first index is zero, the second index is one, and so forth. The syntax of lists is the name of the list, followed by an = and the elements (separated by commas) inside brackets [ ], finishing with a semi colon (;) . The elements can be numbers (integers and floats), strings or even variables. You can transform data into a list with the function list(). There are various other methods of list objects you can browse here.

For loops are used when you have a piece of code which you want to repeat n number of times. The difference between for and while, is that while is used when a condition is to be met and for repeats the code inside it for an established number of times. The syntax is for x: (where x is the number of times you want the loop to repeat) and the code you want repeated on a new line of code, indented to the right. You can use for to loop over a sequence, so that, for each element in the sequence, the loop repeats. The syntax of this would be for i in sequence: , where i is a new variable (local to for) that takes the value of the element in order of indexes, and sequence is either a range, a list or any other kind of sequence.

CC BY 4.0 WSQ07 – Sum of Numbers by carminaperezguerrero is licensed under a Creative Commons Attribution 4.0 International License.