Range in Python is a useful function in web and application programming. Here's everything you need to know about range function in Python.
If you have good logical and creative thinking, try learning programming once. This is one of the most sought-after professions today with decent, even high salaries for professional programmers.
Learning programming is really a difficult subject for many people, but you can completely conquer it with great passion and determination. Choose a programming language you love to start sticking with. Python is currently popular among many software developers.
Let's learn Python from the most basic knowledge. In this article, we will learn together what is range in Python?
Python's built-in range() function is used to create a series of numbers starting at 0 by default, increasing by 1 (by default), and ending at a specified number. Simply put, the function receives an integer and returns a range object (iterable type).
In this article, Quantrimang.com will help you learn about range(), syntax, parameters and specific examples. We invite readers to follow along.
Purpose of using range() function in Python
In simple terms, the python range function allows the user to create a series of numbers within the provided range. Depending on the number of arguments the user provides to this function, the user can decide where the series of numbers will begin and end, as well as the major difference between one number and its successor. The range statement in Python can be initialized in 3 ways:
- Range (stop) takes 1 argument.
- Range(start, stop) takes 2 arguments.
- Range (start, stop, step) takes 3 arguments.
Range() function syntax in Python
The range() function in Python has two syntactic forms:
range(stop)
range(start, stop[, step])
Parameters of the range() function
The range() function has 3 parameters:
start
: starting integer, the string will start with this parameter. The default value is 0.stop
: end integer, the string will end with this parameter.step
: integer specifying the distance between numbers inside the string. The default value is 1.
Return value from range()
1. With syntax type range(stop):
- Returns a string starting from 0 to
stop
-1. - Returns an empty string if stop has a value of 0 or less.
2. With syntax type range(start, stop[, step]):
- If there are no parameters
step
,step
default will be 1: the return value is a string starting fromstart
and ends atstop
-1 - If
step
equals 0, exceptionValueError
exception will be generated. - If
step
other than 0, check whether the parameters meet the constraint or not.- If so, returns the string according to the formula, starting from
start
the numbers are separated bystep
the last number of the string will stop. - Otherwise returns an empty string.
- If so, returns the string according to the formula, starting from