Python range() Method with Examples

range() Method in Python:

The range() function returns a sequence of numbers that starts at 0 and increments by 1 (by default) and stops before the specified value.

Syntax:

range(start, stop, step)

Parameters

start: This is Optional. An integer number indicates the starting position. The default value is 0.

stop: This is Required. An integer number indicating the position at which to stop (not included).

step: This is Optional. It is an integer number. The incrementation is specified this. The default value is 1.

range() Method in Python with Examples

1)Passing only one argument

The two parameters, step, and start are optional and are set to 1 and 0 respectively by default. The stop argument, on the other hand, is required for sequence formation.

When only stop is specified, the range() method generates a sequence with step 1 ranging from 0 to (stop-1).

Approach:

  • Pass some random number(stop) as an argument to the range() function to generate numbers from 0 to given number-1 and convert it into a list using the list() function.
  • Store it in a variable.
  • Print the given list.
  • Get the Object type returned by the range() method using the type() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Pass some random number as an argument to the range() function to generate numbers from 0 to
# given number-1 and convert it into list using the list() function.
# Store it in a variable.
gvn_lst = list(range(7))
# Print the given list.
print("The given list = ", gvn_lst)
# Get the Object type returned by the range() method using the type() function
print("Object type returned by the range() method = ", type(range(7)))

Output:

The given list =  [0, 1, 2, 3, 4, 5, 6]
Object type returned by the range() method =  <class 'range'>

2)Passing Two arguments

Approach:

  • Give the start value as static input and store it in a variable.
  • Give the stop value as static input and store it in another variable.
  • Pass the given start, stop values as the arguments to the range() function to generate numbers from given start to stop-1 values.
  • Store it in a variable.
  • Convert the above result into a list using the list() function.
  • Store it in another variable.
  • Print the above result list obtained.
  • The Exit of the Program.

Below is the implementation:

# Give the start value as static input and store it in a variable.
gvn_strt = 8
# Give the stop value as static input and store it in another variable.
gvn_stop = 12
# Pass the given start, stop values as the arguments to the range() function
# to generate numbers from given start to stop-1 values.
# Store it in a variable.
gvn_range = range(gvn_strt, gvn_stop)
# Convert the above result into a list using the list() function.
# Store it in a variable.
rslt_lst = list(gvn_range)
# Print the above result list obtained.
print("The result list = ", rslt_lst)

Output:

The result list =  [8, 9, 10, 11]

3)Passing Three arguments

Approach:

  • Give the start value as static input and store it in a variable.
  • Give the stop value as static input and store it in another variable.
  • Give the step value as static input and store it in another variable.
  • Pass the given start, stop, step values as the arguments to the range() function to generate numbers from given start to stop-1 values with the given step size.
  • Store it in a variable.
  • Convert the above result into a list using the list() function.
  • Store it in another variable.
  • Print the above result list obtained.
  • The Exit of the Program.

Below is the implementation:

# Give the start value as static input and store it in a variable.
gvn_strt = 10
# Give the stop value as static input and store it in another variable.
gvn_stop = 30
# Give the step value as static input and store it in another variable.
gvn_step = 2
# Pass the given start, stop, step values as the arguments to the range() function
# to generate a numbers from given start to stop-1 values with the given step size.
# Store it in a variable.
gvn_range = range(gvn_strt, gvn_stop, gvn_step)
# Convert the above result into list using the list() function.
# Store it in another variable.
rslt_lst = list(gvn_range)
# Print the above result list obtained.
print("The result list = ", rslt_lst)

Output:

The result list =  [10, 12, 14, 16, 18, 20, 22, 24, 26, 28]

4) Using range() In For Loop

As previously stated, range() is extensively used in for loop structures.

# Iterate from 1 to 7 using the for loop and range() function
for k in range(1, 7):
    # Inside the parent for loop, Loop again from 1, to k+1 using the inner for loop
    for m in range(1, k+1):
        # Print the iterator value of the inner for loop separated by spaces
        print(m, end="")
    print()

Output:

1
12
123
1234
12345
123456

 

 

Leave a Comment