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”
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)
This exercise is part of the course
Introduction to Python for Finance
Build Python skills to elevate your finance career. Learn how to work with lists, arrays and data visualizations to master financial analyses.
This chapter introduces packages in Python, specifically the NumPy package and how it can be efficiently used to manipulate arrays.
Exercise 1: ArraysExercise 2: Create an arrayExercise 3: Elementwise operations on arraysExercise 4: Subsetting elements from an arrayExercise 5: 2D arrays and functionsExercise 6: Creating a 2D arrayExercise 7: Subsetting 2D arraysExercise 8: Calculating array statsExercise 9: Generating a sequence of numbersExercise 10: Using arrays for analysisExercise 11: Who's above average?Exercise 12: Who's in health care?What is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.