 Introduction to Python

• Status
• 1
Input and Output
• 2
Variables and Integer Arithmetic
• 3
Conditional Operators
• 4
• 5
Types and Variables
• 6
Strings
• 7
Lists
• 8
For Loops
• 9
While Loops
• 10
continue, break, while...else
• 11
String and List methods
• 12
Nested Loops
• 13
List Comprehension
• 14
Tuples and Sets
• 15
dict
• 16
Functions
• 17
Functions 2
• 18
Lambda and higher-order functions
• 19
Files

• # range

When working with numbers, it’s sometimes very useful to work with sequences. In some applications, we might be interested in the sequence of numbers `0, 1, 2, ..., n`. In another application, the important numbers might lie on a segment `l, l + 1, l + 2, ..., r`.
The `range()` command makes it easy to work with ranges of numbers. It can generate numbers in any range with any interval between each pair:
``````print(list(range(5)))         # [0, 1, 2, 3, 4]
print(list(range(7)))         # [0, 1, 2, 3, 4, 5, 6]
print(list(range(1, 5)))      # [1, 2, 3, 4]
print(list(range(4, 7)))      # [4, 5, 6]
print(list(range(5, 3)))      # []
print(list(range(2, 15, 4)))  # [2, 6, 10, 14]
print(list(range(11, 2, -1))) # [11, 10, 9, 8, 7, 6, 5, 4, 3]``````
`range()` can take 1, 2 or 3 arguments:
• 1 argument `r`: generate numbers `0, 1, 2, ... r-1`
• 2 arguments `l` and `r`: generate numbers `l, l+1, ..., r-1`
• 3 arguments `l`, `r`, and `d`: generate numbers `l, l+d, ..., r-1`
Note that similar to slices of strings or lists, the `range()` also works with an inclusive start and an exclusive end.
When used with other expressions, we can use `range()` without the `list()`. In the examples above, we have used `list()` so that the `print` statement displays a nice output.

#### Challenge

Given an integer `n` as an input, write a program that would output the sum of numbers `1, 2, ... n`.
 Input Output 2 3 5 15