Range() and Enumerate()

1 - Range()

Range function is pretty common when it comes to coding in Python. How does range work? Well, range lists out all the numbers between any 2 given numbers.

For example, if we have range(1,10), Python would give us an output of 1,2,3,4,5,6,7,8 and 9 but not 10. In range, the end is not counted but the start is.

The maximum number of parameters range() can take in is 3. The first parameter is the start, second comes the end and the third is the jump.

But in our previous range function, we haven't set the jump parameter. If that is the case, the function would take in 0 for the jump parameter. In the range() function, if any other parameter is not set, Python is going to set the parameter to 0.

For example, if we have range(10), python is going to give us an output of 0,1,2,3,4,5,6,7,8 and 9. Because we have just defined on value, range is going to take that in as the end and going to take start and jump as 0.

Remember that if just one parameter is given, that is going to be taken as the end. If two parameters are given, that is going to be taken as start and end. If three parameters are given, this is going to be start, end and jump. 4

In order to use the range function, there are 2 ways.

1 - We use the for loop. When we use the range() function, it would be zipped. In simple, it is something that is packed or something that is concise and in order to use these. We use the for loop in addition to the 'in' operator.

2 - We use the '*' operator. This operator is used to unzip.

Using the for loop and the 'in' operator

Using the '*' operator.

(2) Enumerate()

Enumerate is one of the amazing functions of Python. We have learned about the index previously. This enumerate function separates every character in a string into indices. To use this function, we pass in the parameter and this parameter is the string. Before getting to use this string, there is an important information that needs to be provided. This enumerate function is zipped too and to use this, we will have to use the for loop. Now, in our normal for loops, we passed in a variable name but for the enumerate function, we will have to declare two variables. Or, we could use the '*' operator. The first variable is for the character and the second variable is for the index. Here is an illustration:

Using the for loop

Using the '*' operator