Learn about the range() functions available in Python.


! What is the range() function?
The range() function of Python is a function that returns consecutive numbers. It's very simple to create a sequence. Below is a simple grammar.

range(startNumber, stopNumber, stepSize)


- startNumber : indicates start value, default 0 (optional)
- stopNumber : indicates the last value to be continuous. However, it does not include self (required value)
- stepSize: Determines a unit value of the number of consecutive units (optional)

It's easy to make a sequence of numbers. So let's look at the simple iteration and how it's used in the arrangement with an example.
# range()See Example of a function repeat statement
In this example, you are only going to use the required stopNumber. As an example of entering range (3) in the code to be executed, the start value will be the default value of 0..
for i in range(3):
  print(i)

The results are as follows.
0
1
2

The sum of up to 2 is displayed except for yourself 3.


! Example of using start and end values
In the following example, you want to use range() to obtain a sum of 1 to 10.
sum = 0
for i in range(1, 11):
  print(i)
  sum = sum + i

print(sum)

The output results are as follows.
1
2
3
4
5
6
7
8
9
10
55

That is, the sum of 55 from 1 to 10. The reason we used 11 here is because it does not include the second factor, stopNumber. So you have to use 11 to add up to 10.


! See another range() example
This time, set the step value to 5 and try to write an example.
sum = 0
for i in range(5, 55, 5):
  print(i)
  sum = sum + i

print(sum)

The output results are as follows.
5
10
15
20
25
30
35
40
45
50
220

As you can see, we're going to print a series of numbers from 5 to 50, and we're going to see a total of 220.



# Create an array as a range() function
Let's create a list type, which is an array of Pythons. Below is a simple example of a sequence of array values.
myArray = list(range(1, 6, 2))
print (myArray)

When you print it out, it will appear as follows:
[1, 3, 5]

As expected, the array was created.


So here's a quick look at Python's range() function.