Menu
CoddyTech

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:

Two rows of dots on a number line from 0 to 20. np.arange(0, 20, 5) gives dots at 0, 5, 10 and 15, with an empty circle at 20, left out; np.linspace(0, 20, 5) gives dots at 0, 5, 10, 15 and 20

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 icon

Challenge

Easy

A running coach sets out cones. The program reads the first and last cone's position (m), the gap, and how many water stations. Print:

  1. Cones: first to last, a gap apart (np.arange).
  2. Stations: evenly spaced, the first and last cones included (np.linspace).
  3. 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Data and Plots

Practice on your own: Online Python compiler