Ranges of Numbers
Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 7 of 60.
Typing 100 evenly spaced numbers by hand? NumPy makes them for you, two ways:
np.arange(start, stop, step) counts by a step and, like range, stops before stop. np.linspace(a, b, n) makes n values from a to b, both included:
print(np.arange(0, 20, 5))
print(np.linspace(0, 20, 5))This prints:
[ 0 5 10 15]
[ 0. 5. 10. 15. 20.]Pick by what you know: the gap or the count. For an empty tally, np.zeros(n) and np.ones(n) make n zeros or ones:
print(np.zeros(3)) # [0. 0. 0.]Challenge
EasyA running coach sets out cones. The program reads the first and last cone's position (m), the gap, and how many water stations. Print:
Cones: first to last, a gap apart (np.arange).Stations: evenly spaced, the first and last cones included (np.linspace).Times: a zero per cone, for the runners' times (np.zeros).
For 0, 40, 10 and 3:
Cones: [ 0 10 20 30 40]
Stations: [ 0. 20. 40.]
Times: [0. 0. 0. 0. 0.]Try it yourself
import numpy as np
first = int(input())
last = int(input())
gap = int(input())
stations = int(input())
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Data and Plots
2NumPy Arrays
Creating ArraysShape and SizeRanges of NumbersIndexing and SlicingRecap: Temperature GridPractice on your own: Online Python compiler