How to use the range function in Python?

In Python, range() is a built-in function used to generate a sequence of integers. It is commonly used in the form of range(stop), which creates an integer sequence from 0 to stop-1. Here are some examples:

  1. Generate a sequence of integers from 0 to 9.
  2. Print numbers from 0 to 9.
  3. Generate a sequence of integers from 1 to 10.
  4. Output numbers from 1 to 10.
  5. Generate a sequence of even numbers between 0 and 10.
  6. Print every second number from 0 to 10.
  7. Convert the result of range() into a list.
  8. numbers = list(range(5))
    print(numbers) # Output: [0, 1, 2, 3, 4]

In these examples, the first parameter of the range() function is the starting value (default is 0), the second parameter is the ending value (not included in the sequence), and the third parameter is the step size (default is 1).

bannerAds