YouTip LogoYouTip

Python3 Func Range

# Python3.x Python3 range() Function Usage [![Image 3: Python3 Built-in Functions](#) Python3 Built-in Functions](#) The Python3 range() function returns an iterable object (type is object), not a list type, so it will not print a list when printed. The Python3 list() function is an object iterator that can convert the iterable object returned by range() into a list, and the returned variable type is a list. The Python2 [range() function](#) returns a list. ### Function Syntax range(stop) range(start, stop[, step]) Parameter Description: * start: Counting starts from start. By default, it starts from 0. For example, range(5) is equivalent to range(0, 5) * stop: Counting stops at stop, but does not include stop. For example: range(0, 5) is [0, 1, 2, 3, 4] without 5 * step: Step size, default is 1. For example: range(0, 5) is equivalent to range(0, 5, 1) ### Examples If only the start and end values are provided, the step size defaults to 1: ## Example for number in range(1,6): print(number) Output: 12345 If only one parameter is provided, it will generate an integer sequence starting from 0, with the parameter as the end value and the step size defaulting to 1: ## Example for number in range(6): print(number) Output: 012345 You can specify the step size to increase by a different amount each time: ## Example for number in range(1,6,2): print(number) Output: 135 **Note**: The end value 6 is not included. Additionally, we can use a negative number as the step size to generate a sequence in reverse order from the end value: ## Example for number in range(6,1, -1): print(number) Output: 65432 **Note:** If a negative number is used as the step size, the start value must be greater than the end value. If you only need to generate an integer sequence and do not need to use a for loop to iterate over it, you can convert the return value of the range() function to a list or tuple, for example: The following generates a list of integers: ## Example >>> numbers =list(range(1,6)) >>> numbers [1,2,3,4,5] The following generates a tuple of integers: ## Example >>> numbers =tuple(range(1,6)) >>> numbers (1,2,3,4,5) [![Image 4: Python3 Built-in Functions](#) Python3 Built-in Functions](#)
← Mongodb Delete CollectionPhp Eof Heredoc β†’