The range() Function with Python

The range() Function

Concept

The range() function is used to generate a sequence of numbers, which is often used in for loops to iterate a specific number of times. It is a powerful tool for creating loops efficiently and elegantly.

Syntax

The range() function can be used in three ways:

  • range(stop): Generates numbers from 0 up to stop – 1.
  • range(start, stop): Generates numbers from start up to stop – 1.
  • range(start, stop, step): Generates numbers from start up to stop – 1, incrementing or decrementing by step.

Parameters

  • start: The initial value of the sequence (inclusive). Defaults to 0.
  • stop: The end value of the sequence (exclusive). This parameter is required.
  • step: The increment between successive values in the sequence. Defaults to 1.

Behavior

  • Inclusivity/Exclusivity: start is inclusive in the sequence, but stop is exclusive.
  • Iterable: range() returns a range object, which is an iterable. This means it can be used in for loops.

Examples

Using range(stop)

Example: 

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

Explanation:

  • The for loop iterates from 0 to 4.
  • range(5) generates the sequence 0, 1, 2, 3, 4.
  • The values are printed accordingly.

Using range(start, stop)

Example: 

for i in range(2, 7):
    print(i)

Explanation:

  • The for loop iterates from 2 to 6.
  • range(2, 7) generates the sequence 2, 3, 4, 5, 6.
  • The values are printed accordingly.

Using range(start, stop, step)

Example: 

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

Explanation:

  • The for loop iterates from 1 to 9, incrementing by 2 each time.
  • range(1, 10, 2) generates the sequence 1, 3, 5, 7, 9.
  • The values are printed accordingly.

Using range() with Nested Loops

Example: 

for i in range(3):
    for j in range(2):
        print(f"i = {i}, j = {j}")

Explanation:

  • The outer loop iterates from 0 to 2.
  • The inner loop iterates from 0 to 1.
  • The result is a printout of all combinations of i and j.

Points to Note

  • Type range: The object returned by range() is a range object, which is a special type of iterable object. It does not store all values in memory but generates values on demand.

Example: 

r = range(5)
print(r)         # Output: range(0, 5)
print(list(r))   # Output: [0, 1, 2, 3, 4]
  • Usage with if: You can use range() to create specific sequences of values in loops and combine it with conditions to control the flow of the loop.

Example: 

for i in range(10):
    if i % 2 == 0:
        print(i, "is even")
    else:
        print(i, "is odd")
  • Advanced Features: range() can be used in complex expressions for more advanced manipulations.

Example: 

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

Explanation:

    • Here, range(10, 0, -2) generates a descending sequence from 10 to 2, with a step of -2.

Conclusion

The range() function is extremely useful for generating sequences of numbers in loops and can be used flexibly with different parameters. It allows for efficient iteration without the need to create complete lists of numbers in memory, making the code more performant and readable.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print