The range function is a python built-in function that allows users to generate a series of numbers within a given range. Determine the start and end positions of numbers and the difference between one number and another based on the parameter variables passed by the user to the function
The range() function is a built-in function in Python, mainly used for loops. Next, in the article, I will introduce the usage of this function in detail, which has a certain reference effect. I hope it will be helpful to everyone.
[Recommended course: Python tutorial]
range() function
range() is a built-in function of Python. It is used when the user needs to perform a specific number of operations, which means looping. The built-in function range() can be used to generate a sequence of numbers in the form of a list. The most common usage in the range() function is to use for and while loops to iterate over sequence types (List, string, etc.). Simply put the range() function allows the user to generate a range of numbers within a given range. Depending on the number of arguments the user passes to the function, the user can decide where the series of numbers begins and ends and how much the difference is between one number and the next
range() function usage
Syntax structure:
range(start, stop, step)
Parameter introduction:
start: Indicates the starting number of the returned sequence, by default The next number starts from 0
stop: indicates the number that generates the most but does not include this number
step: refers to the difference between each number in the sequence, the default value is 1
Example: Print a sequence of numbers from 1 to 20 separated by 3
x = range(1, 20, 3) for n in x: print(n)
The output result is: 1 4 7 10 13 16 19
Note: All parameters in the range function must If it is an integer but it can be a positive or negative integer, the index starts from 0 instead of 1
Summary: The above is the entire content of this article, I hope it will be helpful to everyone
The above is the detailed content of How to use the range function in Python. For more information, please follow other related articles on the PHP Chinese website!