The range () function in Python

The range () function built into Python is used to create a sequence of numbers starting at 0 by default, incrementing 1 (by default) and ending at a specified number. Put simply, the function takes an integer and returns a range object (iterable).

In this article, TipsMake.com will learn about range (), syntax, parameters and examples with you. Invites you to read the track.

The syntax of the range () function in Python

The range () function in Python has two types of syntax:

 range(stop) 
 range(start, stop[, step]) 

The parameters of the range () function

The range () function has 3 parameters:

  1. start : integer begins, the string will start with this parameter. The default value is 0.
  2. stop : the integer ends, the string ends with this parameter.
  3. step : integer specifies the distance between the numbers inside the string. The default value is 1.

Return value from range ()

1. With range (stop) syntax:

  1. Returns a string starting from 0 to stop -1.
  2. Returns an empty string if the stop value is 0 or less than 0.

2. With range type syntax (start, stop [, step]):

  1. Without the step parameter, the default step will be 1: the return value is a string starting at start and ending at stop -1
  2. If step equals 0, the exception exception ValueError will be raised.
  3. If step , check if the parameters meet the constraint.
    1. If yes, returns the string according to the formula, starting from start , the numbers separated by step , the last number of the string will be < stop .
    2. Otherwise returns an empty string.

Example 1: how does range () work?

 print(list(range(0))) # sử dụng range(stop) print(list(range(10))) # sử dụng range(start, stop) print(list(range(1, 10))) 

When you run the program, the output will be:

 [] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9] 

Example 2: Making an even number list between parameters passed by using range ()

 start = 2 stop = 14 step = 2 print(list(range(start, stop, step))) 

When you run the program, the output will be:

 [2, 4, 6, 8, 10, 12] 

Example 3: range () works with step being a negative number

 start = 2 stop = -14 step = -2 print(list(range(start, stop, step))) print(list(range(start, 14, step))) 

Return value:

 [2, 0, -2, -4, -6, -8, -10, -12] [] 

See also: Python built-in functions

4 ★ | 2 Vote

May be interested

  • The function set () in PythonThe function set () in Python
    in this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
  • Help () function in PythonHelp () function in Python
    the built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
  • Sum () function in PythonSum () function in Python
    the built-in function sum () in python returns the sum of all numbers in iterable.
  • The slice () function in PythonThe slice () function in Python
    the slice () function in python returns a slice object that helps you determine how to cut an existing string.
  • The chr () function in PythonThe chr () function in Python
    in python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
  • The iter () function in PythonThe iter () function in Python
    continuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
  • The pow () function in PythonThe pow () function in Python
    the pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
  • The reversed () function in PythonThe reversed () function in Python
    the reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
  • Exec () function in PythonExec () function in Python
    the exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.
  • Built-in Python functionsBuilt-in Python functions
    in the previous article, you learned that python has a basic function type, one is a built-in function, two are user-defined functions. today, we will learn about a list of python's built-in functions.