Home>Article>Backend Development> Introduction to Python functions: introduction and examples of range function

Introduction to Python functions: introduction and examples of range function

王林
王林 Original
2023-11-04 10:10:41 1649browse

Introduction to Python functions: introduction and examples of range function

Introduction to Python functions: introduction and examples of range functions

Python is a high-level programming language widely used in various fields. It is simple and easy to learn. And it has a rich built-in function library. Among them, the range function is one of the commonly used built-in functions in Python. This article will introduce the function and usage of the range function in detail, and demonstrate its specific application through examples.

The range function is a function used to generate an integer sequence. It accepts three parameters, namely the start value (start), the end value (end) and the step size (step). Among them, the starting value and step size are optional. The default starting value is 0 and the step size is 1.

The syntax format of the range function is as follows:
range(start, end, step)

Below we use some examples to illustrate the usage and effect of the range function.

Example 1: Generate an integer sequence from 0 to 9

for i in range(10): print(i)

In the above code, the starting value of the range function defaults to 0, the ending value is 10, and the step size defaults to 1. In this example, the loop will output integers from 0 to 9 in sequence.

Example 2: Generate an integer sequence from 1 to 10 with a step size of 2

for i in range(1, 11, 2): print(i)

In the above code, we set the starting value of the range function to 1 and the end value to 11 , with a step size of 2, generates an odd sequence from 1 to 10. The loop will output the odd numbers 1, 3, 5, 7, and 9 in sequence.

Example 3: Generate an integer sequence from 10 to 1 with a step size of -1

for i in range(10, 0, -1): print(i)

In the above code, we set the starting value of the range function to 10 and the ending value to 0, with a step size of -1, generating a sequence of integers in reverse order from 10 to 1. The loop will output the integers 10, 9, 8, 7, 6, 5, 4, 3, 2, and 1 in sequence.

In addition to being used with the range function in a for loop, the range function is also often used to generate lists. We can convert the resulting sequence of integers into a list by passing the range function as a parameter to the list function.

Example 4: Save the first 5 even numbers into a list

even_numbers = list(range(0, 10, 2)) print(even_numbers)

Run the above code, we can get a list containing the first 5 even numbers [0, 2, 4, 6, 8 ].

To sum up, the range function is a very practical function in Python, which can help us easily generate an integer sequence. By setting the start value, end value and step size, we can freely control the range and regularity of the generated sequence. Whether it is used to iterate over a sequence of integers in a for loop or when generating a list, the range function can play a great role. If you are a beginner in Python, you might as well try using the range function. I believe you will be able to discover its convenience.

The above is the detailed content of Introduction to Python functions: introduction and examples of range function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn