Get startedGet started for free

(SAMPLE CODING) Built-in practice: range()

In this exercise, you will practice using Python's built-in function range().

Remember that we can use range() in two different ways:

(1) Create a sequence of numbers from 0 to (stop - 1). This is useful when we want to create a simple sequence of numbers starting at zero:

range(stop)

# Example
list(range(11))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

(2) Create a sequence of numbers from start to (stop - 1) with a step size. This is useful when we want to create a sequence of numbers that increments by some value other than one. For example, a list of even numbers:

range(start, stop, step)

# Example
list(range(2,12,2))

[2, 4, 6, 8, 10]

This exercise is part of the course

Drag and Drop Examples

View Course

Exercise instructions

  • Create a range object that starts at zero and ends at five. Only use a stop argument.
  • Print the data type of the nums variable.
  • Convert the nums variable into a list called nums_list.
  • Create a new list called nums_list2 by unpacking the nums variable using the star character (*).

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create a range object that goes from 0 to 5
nums = ____(____)

# Print the type of nums
print(____(____))

# Convert nums to a list
nums_list = ____(____)
print(nums_list)

# Create a new list by unpacking nums
nums_list2 = [*____]
print(nums_list2)
Edit and Run Code