Day 02 - Range¶
Range is a built-in funtion that generates a sequence of numbers from the given values.
The syntax is as follows:
range([start,] stop [, step])
As you can see, the function receives up to 3 parameters, however only one is required
start: (Optional) The initial value in the sequence, by default is 0
stop: (required) End of the sequence, without including its value.
step: (optional) The size of steps in the sequence the default value is 1
We’ll print out what the function range returns if we pass it number 5. Remember that it has a parameter that is required (stop).
So, In this case the sequence will start at 0 and end at 5 (not including it).
step will take their default values.
We’ll see that when printing it returns something similar to range(0, 5), but what we want is to see the complete sequence, so to see the values that compose it, for this we must cast this range as a list.
sequence = list(range(5)) print(sequence)
The output is now more understandable:
[0, 1, 2, 3, 4]
When we call range passing two parameters, with the first value we define where the sequence starts and with the second one where it ends.
Remember that the value where it ends is not included in the sequence
print(list(range(2, 9))) # Output: # [2, 3, 4, 5, 6, 7, 8]
Finally, when calling the function with all its arguments, we will define where it starts, where it ends, and how many steps the sequence will be.
print(list(range(-5, 5, 2))) # Output: # [-5, -3, -1, 1, 3]
You can see that the sequence starts at -5 and ends at 5, but where’s number 4?
This is due to the steps we are indicating, which are 2, this way it will omit 1 value before returning the next number.
- All values must be integers
- Values can be positive or negative
print(list(range(5, -5, -1)))