0 to Infinity Lesson

Python Numerical Thinking

Learn indexing, slicing, dictionaries, NumPy arrays, shape, axis, zeros, ones, random arrays, and how to compute mean, median, minimum, maximum and range with arrays.

1. Overview

Before Machine Learning, Artificial Intelligence, or data analysis, we must become comfortable with numerical data. A model does not directly understand stories. It understands numbers, arrays, rows, columns, shape, and patterns.

Indexing

Pick one value from a list, string, or array.

Slicing

Pick a part of a list, string, or array.

NumPy

Calculate quickly on numerical arrays.

Final output: a basic numerical thinking notebook that computes mean, median, minimum, maximum, and range using arrays.
Advertisement

2. Indexing

Indexing means selecting one item from a sequence. Python indexing starts from 0.

index 0
10
index 1
20
index 2
30
index 3
40
index 4
50
numbers = [10, 20, 30, 40, 50]

print(numbers[0])   # 10
print(numbers[1])   # 20
print(numbers[4])   # 50

Negative indexing

numbers = [10, 20, 30, 40, 50]

print(numbers[-1])  # 50
print(numbers[-2])  # 40
print(numbers[-5])  # 10
If a list has 5 values, the last positive index is 4. numbers[5] will give an error.

3. Slicing

Slicing means selecting a range of values.

list[start : stop : step]

The start value is included. The stop value is not included.

numbers = [10, 20, 30, 40, 50, 60]

print(numbers[1:4])    # [20, 30, 40]
print(numbers[:3])     # [10, 20, 30]
print(numbers[3:])     # [40, 50, 60]
print(numbers[::2])    # [10, 30, 50]
print(numbers[::-1])   # [60, 50, 40, 30, 20, 10]

Slicing strings

name = "Champak"

print(name[0])      # C
print(name[1:4])    # ham
print(name[::-1])   # kapmahC
Indexing gives one value. Slicing gives a collection of values.

4. Dictionaries

A dictionary stores data as key-value pairs. It is useful when data has names.

student = {
    "name": "Aarav",
    "math": 86,
    "science": 91,
    "english": 78
}

print(student["name"])
print(student["math"])

Dictionary operations

student = {
    "name": "Aarav",
    "math": 86,
    "science": 91
}

print(student["math"])

student["english"] = 78
student["math"] = 90

print(student.get("history", "Not found"))

for key, value in student.items():
    print(key, "=", value)
Structure Best used for
List Ordered values
Dictionary Named values
NumPy array Fast numerical calculation

5. NumPy Arrays

NumPy means Numerical Python. It gives us arrays that are powerful for numerical work.

import numpy as np

marks = np.array([70, 80, 90, 60])

print(marks)

List multiplication vs array multiplication

numbers = [1, 2, 3]
print(numbers * 2)

# Output:
# [1, 2, 3, 1, 2, 3]
import numpy as np

numbers = np.array([1, 2, 3])
print(numbers * 2)

# Output:
# [2 4 6]
Normal lists think generally. NumPy arrays think numerically.

6. Creating NumPy Arrays: zeros, ones, random, arange and linspace

So far, we created NumPy arrays manually using np.array(). But in real numerical work, we often need to create arrays automatically. NumPy gives us many useful functions for this.

zeros

Creates an array filled with 0. Useful when we want an empty numerical structure to fill later.

ones

Creates an array filled with 1. Useful for starting weights, masks, or simple test arrays.

random

Creates random numbers. Useful for testing, simulations, and Machine Learning experiments.

np.zeros()

np.zeros() creates an array filled with zeroes.

import numpy as np

a = np.zeros(5)

print(a)

Output:

[0. 0. 0. 0. 0.]

Two-dimensional zeros array

import numpy as np

a = np.zeros((3, 4))

print(a)
print("Shape:", a.shape)

This creates an array with 3 rows and 4 columns.

np.ones()

np.ones() creates an array filled with ones.

import numpy as np

a = np.ones(5)

print(a)

Output:

[1. 1. 1. 1. 1.]

Two-dimensional ones array

import numpy as np

a = np.ones((2, 3))

print(a)
print("Shape:", a.shape)

np.full()

np.full() creates an array filled with a value of our choice.

import numpy as np

a = np.full((3, 3), 7)

print(a)

Output:

[[7 7 7]
 [7 7 7]
 [7 7 7]]

np.arange()

np.arange() creates numbers in a range. It is similar to Python's range(), but it creates a NumPy array.

import numpy as np

a = np.arange(1, 11)

print(a)

Output:

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

arange with step

import numpy as np

even_numbers = np.arange(2, 21, 2)

print(even_numbers)

Output:

[ 2  4  6  8 10 12 14 16 18 20]

np.linspace()

np.linspace() creates evenly spaced values between a starting value and an ending value.

import numpy as np

a = np.linspace(0, 1, 5)

print(a)

Output:

[0.   0.25 0.5  0.75 1.  ]
Difference between arange and linspace: np.arange() focuses on step size. np.linspace() focuses on how many values we want.

np.random.rand()

np.random.rand() creates random decimal numbers between 0 and 1.

import numpy as np

a = np.random.rand(5)

print(a)

Two-dimensional random array

import numpy as np

a = np.random.rand(3, 4)

print(a)
print("Shape:", a.shape)

np.random.randint()

np.random.randint() creates random integers.

import numpy as np

marks = np.random.randint(40, 101, size=10)

print(marks)

This creates 10 random marks from 40 to 100. The ending value 101 is not included.

Random marks table

import numpy as np

marks = np.random.randint(40, 101, size=(5, 3))

print(marks)
print("Shape:", marks.shape)

print("Subject-wise mean:", np.mean(marks, axis=0))
print("Student-wise mean:", np.mean(marks, axis=1))
Important: Random arrays are very useful for practice. They help us test our numerical thinking without manually typing every value.

Quick reference table

Function Purpose Example
np.array() Create an array from existing values np.array([1, 2, 3])
np.zeros() Create an array filled with zeroes np.zeros((2, 3))
np.ones() Create an array filled with ones np.ones((3, 4))
np.full() Create an array filled with a chosen value np.full((3, 3), 7)
np.arange() Create values using start, stop and step np.arange(1, 11, 2)
np.linspace() Create evenly spaced values np.linspace(0, 1, 5)
np.random.rand() Create random decimal values np.random.rand(3, 4)
np.random.randint() Create random integer values np.random.randint(1, 101, size=10)

7. Shape

Shape tells us the structure of an array.

One-dimensional array

import numpy as np

a = np.array([10, 20, 30, 40])

print(a.shape)
(4,)

Two-dimensional array

import numpy as np

marks = np.array([
    [80, 85, 90],
    [70, 75, 65],
    [92, 88, 95]
])

print(marks.shape)
(3, 3)
Shape Meaning
(4,) One-dimensional array with 4 values
(3, 2) 3 rows and 2 columns
(2, 3, 4) Three-dimensional array
Shape is very important in Machine Learning. If the shape is wrong, the model may not train.

8. Axis

Axis tells NumPy the direction of calculation.

import numpy as np

marks = np.array([
    [80, 85, 90],
    [70, 75, 65],
    [92, 88, 95]
])
Student Math Science English
Student 1 80 85 90
Student 2 70 75 65
Student 3 92 88 95

axis = 0

axis=0 calculates column-wise.

print(np.mean(marks, axis=0))

axis = 1

axis=1 calculates row-wise.

print(np.mean(marks, axis=1))
Memory trick: axis=0 gives column results. axis=1 gives row results.

9. Mean and Median

import numpy as np

data = np.array([12, 18, 20, 25, 30, 35, 40])

mean_value = np.mean(data)
median_value = np.median(data)

print("Mean:", mean_value)
print("Median:", median_value)

Mean is the average. Median is the middle value after sorting.

10. Minimum, Maximum and Range

import numpy as np

data = np.array([12, 18, 20, 25, 30, 35, 40])

min_value = np.min(data)
max_value = np.max(data)
range_value = max_value - min_value

print("Minimum:", min_value)
print("Maximum:", max_value)
print("Range:", range_value)
Range tells us how spread out the values are.

11. Two-dimensional Statistics

import numpy as np

marks = np.array([
    [80, 85, 90],
    [70, 75, 65],
    [92, 88, 95],
    [60, 72, 68]
])

print("Marks:")
print(marks)

print("Shape:", marks.shape)

print("Overall mean:", np.mean(marks))
print("Subject-wise mean:", np.mean(marks, axis=0))
print("Student-wise mean:", np.mean(marks, axis=1))

print("Subject-wise minimum:", np.min(marks, axis=0))
print("Subject-wise maximum:", np.max(marks, axis=0))
print("Subject-wise range:", np.max(marks, axis=0) - np.min(marks, axis=0))

12. Output: Basic Numerical Thinking Notebook

Copy this into the editor and run it.

"""
Basic Numerical Thinking Notebook
Programmer's Picnic by Champak Roy
"""

import numpy as np

print("=" * 60)
print("BASIC NUMERICAL THINKING NOTEBOOK")
print("=" * 60)

# 1. Indexing

numbers = [10, 20, 30, 40, 50]

print("\n1. INDEXING")
print("Numbers:", numbers)
print("First value:", numbers[0])
print("Second value:", numbers[1])
print("Last value:", numbers[-1])

# 2. Slicing

print("\n2. SLICING")
print("First three values:", numbers[:3])
print("Values from index 1 to 3:", numbers[1:4])
print("Every second value:", numbers[::2])
print("Reverse list:", numbers[::-1])

# 3. Dictionary

student = {
    "name": "Aarav",
    "math": 80,
    "science": 85,
    "english": 90
}

print("\n3. DICTIONARY")
print("Student dictionary:", student)
print("Name:", student["name"])
print("Math marks:", student["math"])

# 4. NumPy Array

marks = np.array([80, 85, 90, 70, 75, 65])

print("\n4. NUMPY ARRAY")
print("Marks array:", marks)
print("Marks multiplied by 2:", marks * 2)
print("Marks plus 5:", marks + 5)

# 4B. Creating NumPy arrays automatically

print("\n4B. CREATING ARRAYS AUTOMATICALLY")

zeros_array = np.zeros(5)
ones_array = np.ones(5)
full_array = np.full((2, 3), 7)
range_array = np.arange(1, 11)
even_array = np.arange(2, 21, 2)
line_array = np.linspace(0, 1, 5)
random_decimal_array = np.random.rand(5)
random_marks = np.random.randint(40, 101, size=10)

print("Zeros array:", zeros_array)
print("Ones array:", ones_array)
print("Full array:")
print(full_array)
print("Range array:", range_array)
print("Even numbers:", even_array)
print("Linspace array:", line_array)
print("Random decimal array:", random_decimal_array)
print("Random marks:", random_marks)

print("Mean of random marks:", np.mean(random_marks))
print("Median of random marks:", np.median(random_marks))
print("Minimum random mark:", np.min(random_marks))
print("Maximum random mark:", np.max(random_marks))
print("Range of random marks:", np.max(random_marks) - np.min(random_marks))

# 5. Basic Statistics

print("\n5. BASIC STATISTICS")
print("Mean:", np.mean(marks))
print("Median:", np.median(marks))
print("Minimum:", np.min(marks))
print("Maximum:", np.max(marks))
print("Range:", np.max(marks) - np.min(marks))

# 6. Two-dimensional Array

class_marks = np.array([
    [80, 85, 90],
    [70, 75, 65],
    [92, 88, 95],
    [60, 72, 68]
])

print("\n6. TWO-DIMENSIONAL ARRAY")
print("Class marks:")
print(class_marks)
print("Shape:", class_marks.shape)

# 7. Axis

print("\n7. AXIS THINKING")

subject_names = np.array(["Math", "Science", "English"])

subject_mean = np.mean(class_marks, axis=0)
student_mean = np.mean(class_marks, axis=1)

print("Subject names:", subject_names)
print("Subject-wise mean:", subject_mean)
print("Student-wise mean:", student_mean)

print("Subject-wise min:", np.min(class_marks, axis=0))
print("Subject-wise max:", np.max(class_marks, axis=0))
print("Subject-wise range:", np.max(class_marks, axis=0) - np.min(class_marks, axis=0))

# 8. Dictionary + NumPy together

report = {
    "subjects": subject_names,
    "subject_mean": subject_mean,
    "student_mean": student_mean,
    "overall_mean": np.mean(class_marks),
    "overall_median": np.median(class_marks),
    "overall_min": np.min(class_marks),
    "overall_max": np.max(class_marks),
    "overall_range": np.max(class_marks) - np.min(class_marks)
}

print("\n8. FINAL REPORT DICTIONARY")

for key, value in report.items():
    print(key, ":", value)

print("\n" + "=" * 60)
print("NOTEBOOK COMPLETE")
print("=" * 60)

13. Python Editor

Run the notebook here. Change the arrays and observe how the results change.

Embedded Python Editor Numerical Thinking Practice

14. Practice Tasks

1 Indexing

Create a list of 7 temperatures. Print the first, third, and last temperature.

2 Slicing

Print the first 3 values, last 3 values, and reversed list.

3 Dictionary

Create a dictionary with city, morning temperature, afternoon temperature, and evening temperature.

4 NumPy

Convert the temperatures into a NumPy array and compute mean, median, min, max, and range.

5 Axis

Create a 2D array where rows are days and columns are morning, afternoon, and evening temperatures.

6 Array Creation

Create one array using np.zeros(), one using np.ones(), one using np.arange(), one using np.linspace(), and one using np.random.randint(). Print the shape of each array.

Show solved practice program
import numpy as np

temperatures = [24, 26, 29, 31, 30, 28, 25]

print("Temperatures:", temperatures)

print("First:", temperatures[0])
print("Third:", temperatures[2])
print("Last:", temperatures[-1])

print("First three:", temperatures[:3])
print("Last three:", temperatures[-3:])
print("Reverse:", temperatures[::-1])

city_weather = {
    "city": "Varanasi",
    "morning_temp": 24,
    "afternoon_temp": 31,
    "evening_temp": 28
}

print("City weather:", city_weather)

temp_array = np.array(temperatures)

print("Mean:", np.mean(temp_array))
print("Median:", np.median(temp_array))
print("Min:", np.min(temp_array))
print("Max:", np.max(temp_array))
print("Range:", np.max(temp_array) - np.min(temp_array))

print("\nArray creation practice")

zeros_array = np.zeros(5)
ones_array = np.ones((2, 3))
range_array = np.arange(1, 11)
line_array = np.linspace(0, 1, 5)
random_array = np.random.randint(20, 41, size=(3, 3))

print("Zeros:", zeros_array, "Shape:", zeros_array.shape)
print("Ones:")
print(ones_array)
print("Shape:", ones_array.shape)
print("Arange:", range_array, "Shape:", range_array.shape)
print("Linspace:", line_array, "Shape:", line_array.shape)
print("Random integers:")
print(random_array)
print("Shape:", random_array.shape)

weekly_weather = np.array([
    [24, 31, 28],
    [25, 32, 29],
    [26, 33, 30],
    [24, 30, 27],
    [23, 29, 26],
    [25, 31, 28],
    [26, 32, 29]
])

print("Weekly weather:")
print(weekly_weather)

print("Shape:", weekly_weather.shape)

print("Mean of morning, afternoon, evening:")
print(np.mean(weekly_weather, axis=0))

print("Mean of each day:")
print(np.mean(weekly_weather, axis=1))

15. Final Challenge

Create a marks analysis notebook for 5 students and 4 subjects.

Show starter code
import numpy as np

marks = np.array([
    [80, 85, 90, 88],
    [70, 75, 65, 72],
    [92, 88, 95, 91],
    [60, 72, 68, 70],
    [85, 89, 84, 87]
])

# Write your code below
Show solution
import numpy as np

marks = np.array([
    [80, 85, 90, 88],
    [70, 75, 65, 72],
    [92, 88, 95, 91],
    [60, 72, 68, 70],
    [85, 89, 84, 87]
])

print("Marks:")
print(marks)

print("Shape:", marks.shape)
print("Mean:", np.mean(marks))
print("Median:", np.median(marks))
print("Minimum:", np.min(marks))
print("Maximum:", np.max(marks))
print("Range:", np.max(marks) - np.min(marks))

print("Subject-wise mean:", np.mean(marks, axis=0))
print("Student-wise mean:", np.mean(marks, axis=1))

print("\nRandom marks analysis")

random_marks = np.random.randint(40, 101, size=(5, 4))

print("Random marks:")
print(random_marks)

print("Shape:", random_marks.shape)
print("Mean:", np.mean(random_marks))
print("Median:", np.median(random_marks))
print("Minimum:", np.min(random_marks))
print("Maximum:", np.max(random_marks))
print("Range:", np.max(random_marks) - np.min(random_marks))

print("Subject-wise mean:", np.mean(random_marks, axis=0))
print("Student-wise mean:", np.mean(random_marks, axis=1))
Top