Thứ Ba, Tháng Hai 11, 2025
spot_img
Homerange() function in Python - QuanTriMang.com

range() function in Python – QuanTriMang.com

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 from start and ends at stop -1
  • If step equals 0, exception ValueError 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 startthe numbers are separated by stepthe last number of the string will stop.
    • 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: Create a list of even numbers between the input parameters 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]
[]

How to iterate with ranges in Python

The Python range command returns an object consisting of a string of integers that you specify.

How to index with range

You can use index with the range function in Python to get the value at a specific location. For example, suppose you are working with range(2, 10, 2). If you looped through this range and printed each value, your result would look like this:

Range in Python

Here's how to access the first value in this range:

Index with range in Python

Any subsequent values ​​can be accessed in the same way as above:

How to use range in Python

Things to remember when using the range() function in Python

  • The range() function only works with integers, including all integers.
  • All arguments must be integers. Users cannot pass a string or float or any other type in the start, stop, and step arguments of a range().
  • All 3 arguments can be positive or negative numbers.
  • The step value is not 0. If a step is 0, Python will raise a ValueError exception.
  • Range() is a data type in Python.
  • Users can access items in a range() by index, just as users would with a list.

See also: Built-in Python functions

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments