Table of Contents
This updated guide examines The Range ()() Function in Python and organizes the essential facts, background, and practical takeaways in clear American English.
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:
start: integer begins, the string will start with this parameter. The default value is 0.stop: the integer ends, the string ends with this parameter.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:
- Returns a string starting from 0 to
stop-1. - Returns an empty string if the stop value is 0 or less than 0.
2. With range type syntax (start, stop [, step]):
- Without the
stepparameter, the defaultstepwill be 1: the return value is a string starting atstartand ending atstop-1 - If
stepequals 0, the exception exceptionValueErrorwill be raised. - If
step, check if the parameters meet the constraint.- If yes, returns the string according to the formula, starting from
start, the numbers separated bystep, the last number of the string will be<stop. - Otherwise returns an empty string.
- If yes, returns the string according to the formula, starting from
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
FAQ
What is The Range ()() Function in Python about?
It provides a structured overview of range () function, explains the main context, and highlights practical takeaways for readers.
Why does this topic matter?
Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.
How should readers use this information?
Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.
Reader Comments 0
Sign in with email or Google to join the discussion.