Get Started

Generating a sequence of numbers

You may want to create an array of a range of numbers (e.g., 1 to 10) without having to type in every single number. The NumPy function arange() is an efficient way to create numeric arrays of a range of numbers. The arguments for arange() include the start, stop, and step interval as shown below:

np.arange(start, stop, step)

numpy is imported as np.

This is a part of the course

“Introduction to Python for Finance”

View Course

Exercise instructions

  • Create an array company_ids containing the numbers 1 through 7 (inclusive).
  • Create an array company_ids_odd containing only the odd numbers from 1 through 7 (inclusive).

Hands-on interactive exercise

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

# Create and print company IDs
company_ids = np.arange(____, ____, 1)
print(company_ids)

# Use array slicing to select specific company IDs
company_ids_odd = ____(1, ____, ___)
print(company_ids_odd)
Edit and Run Code