NumPy Fundamentals for Absolute Beginners: Arrays, Slicing, and Mathematical Operations with Terminal Examples

Master NumPy from scratch with hands-on terminal demonstrations. Learn array creation, indexing, slicing, reshaping, and mathematical operations. Includes troubleshooting common import errors and circular import issues.

18 min read

NumPy (Numerical Python) is the foundation of data science in Python. It provides powerful tools for working with arrays and performing mathematical operations efficiently. In this comprehensive tutorial, we'll explore NumPy fundamentals through hands-on terminal examples, including common troubleshooting scenarios that beginners often encounter.

๐Ÿ’ก

๐ŸŽฏ What You'll Learn: In this practical tutorial, you'll discover:

  • Installing and importing NumPy correctly
  • Creating 1D and 2D arrays from Python lists
  • Understanding and resolving circular import errors
  • Accessing individual elements and array slicing
  • Advanced 2D array slicing with row and column operations
  • Reshaping arrays to change their dimensions
  • Performing mathematical operations like mean and sum
  • Troubleshooting common NumPy installation and naming issues
  • Best practices for NumPy file naming and project organization

๐Ÿš€ Getting Started with NumPy

Let's begin our NumPy journey by setting up our development environment. We'll follow the exact terminal session that demonstrates both common mistakes and their solutions.

Prerequisites

Before we dive in, make sure you have:

  • Python 3.x installed on your system
  • Basic understanding of Python lists and data types
  • Terminal/command line access
  • Text editor (nano, vim, or VS Code)
  • Basic knowledge of Python syntax and variables

Opening Our Development Environment

code .

Command Explanation:

  • code . opens Visual Studio Code in the current directory
  • The . represents the current directory
  • This gives us a full development environment for editing files

Checking Our Workspace

ls

Terminal Output:

numpy.py

We can see there's already a file called numpy.py in our directory. This will become important later when we encounter a naming conflict!

๐Ÿ”จ Creating Our First NumPy Script

Let's examine the existing NumPy script to understand what we're working with:

cat numpy.py

Terminal Output:

import numpy as np

arr1 = np.array([1,2,3,4,5])
print("1D array:" arr1)

Code Analysis:

LineCodePurpose
1import numpy as npImport NumPy library with alias 'np'
2arr1 = np.array([1,2,3,4,5])Create 1D array from Python list
3print("1D array:" arr1)โš ๏ธ Syntax Error - Missing comma

๐Ÿšจ Encountering and Fixing Syntax Errors

Let's run the script and see what happens:

python numpy.py

Terminal Output:

  File "/home/centos9/Razzaq-Labs-II/random/numpy.py", line 4
    print("1D array:" arr1)
                      ^
SyntaxError: invalid syntax

Error Analysis:

  • Problem: Missing comma in the print statement
  • Location: Line 4, character position indicated by ^
  • Solution: Add comma between string and variable

The error message tells us exactly what's wrong:

  • File path: Shows the exact file with the error
  • Line number: Line 4 contains the syntax error
  • Character position: The ^ points to where Python detected the problem
  • Error type: SyntaxError means there's a problem with Python syntax

Fixing the Syntax Error

Let's check the corrected version:

cat numpy.py

Terminal Output:

import numpy as np

arr1 = np.array([1,2,3,4,5])
print("1D array:", arr1)

Notice the crucial difference:

  • Before: print("1D array:" arr1) โŒ
  • After: print("1D array:", arr1) โœ… (added comma)

๐Ÿ”„ The Circular Import Problem

Now let's try running the corrected script:

python numpy.py

Terminal Output:

Traceback (most recent call last):
  File "/home/centos9/Razzaq-Labs-II/random/numpy.py", line 1, in <module>
    import numpy as np
  File "/home/centos9/Razzaq-Labs-II/random/numpy.py", line 3, in <module>
    arr1 = np.array([1,2,3,4,5])
AttributeError: partially initialized module 'numpy' has no attribute 'array' (most likely due to a circular import)

Understanding the Circular Import Error:

Problem ComponentWhat HappensWhy It Fails
File NameOur file is named numpy.pyConflicts with actual NumPy module name
Import Statementimport numpy as npPython imports our file instead of the library
ResultPartial initialization failureOur file doesn't have array function
โš ๏ธ

โš ๏ธ Naming Conflict: Never name your Python files the same as libraries you want to import. Python will try to import your file instead of the actual library, causing circular import errors.

๐Ÿ“ฆ Checking NumPy Installation

Before fixing the naming issue, let's verify NumPy is properly installed:

pip install numpy

Terminal Output:

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: numpy in /home/centos9/.local/lib/python3.9/site-packages (2.0.2)

Installation Output Explanation:

  • User installation: Installing to user directory (not system-wide)
  • Already satisfied: NumPy version 2.0.2 is already installed
  • Location: Installed in the user's local Python packages directory

Let's see what files are in our directory now:

ls

Terminal Output:

numpy.py  __pycache__

New Files Explained:

  • numpy.py: Our original script file
  • pycache: Python cache directory created during the failed import attempt

๐Ÿ”ง Fixing the Naming Conflict

The solution is to rename our file to avoid the conflict:

mv numpy.py numpyy.py

Command Explanation:

  • mv stands for "move" but is used to rename files
  • numpy.py is the source filename (conflicting name)
  • numpyy.py is the destination filename (safe name)
  • Adding an extra 'y' avoids the naming conflict

Now let's test our renamed script:

python numpyy.py

Terminal Output:

1D array: [1 2 3 4 5]

Success! The output shows:

  • Format: [1 2 3 4 5] - NumPy array format (no commas)
  • Type: This is a NumPy array, not a Python list
  • Elements: Contains integers 1 through 5
โœ…

โœ… Problem Solved: By renaming our file, Python can now properly import the real NumPy library and our code works correctly.

๐Ÿ“Š Creating 2D Arrays

Let's expand our script to include 2D arrays. First, let's see the updated content:

cat numpyy.py

Terminal Output:

import numpy as np

arr2 = np.array([[1,2,3],[4,5,6]])
print("2D Array:")
print(arr2)

Understanding 2D Array Creation

ComponentCodeExplanation
Outer List[[1,2,3],[4,5,6]]Contains two inner lists (rows)
First Row[1,2,3]First row with 3 elements
Second Row[4,5,6]Second row with 3 elements
Result Shape2 rows ร— 3 columnsCreates a 2ร—3 matrix

Running the 2D Array Example

python numpyy.py

Terminal Output:

2D Array:
[[1 2 3]
 [4 5 6]]

2D Array Output Analysis:

  • Structure: Two rows displayed vertically
  • Alignment: Elements are aligned in columns
  • Format: Square brackets show the array structure
  • Spacing: NumPy formats arrays for readability

๐Ÿ” Array Indexing and Element Access

Let's add element access to our script:

cat numpyy.py

Terminal Output:

import numpy as np

arr1 = np.array([1,2,3,4,5])
print("1D array:", arr1)

element = arr1[2]
print("Accessed Element:", element)

Understanding Array Indexing

ArrayIndexElementNote
[1,2,3,4,5]01First element
[1,2,3,4,5]12Second element
[1,2,3,4,5]23Third element (our example)
[1,2,3,4,5]34Fourth element
[1,2,3,4,5]45Fifth element

Running the Indexing Example

python numpyy.py

Terminal Output:

1D array: [1 2 3 4 5]
Accessed Element: 3

Indexing Explanation:

  • Index 2: Accesses the third element (zero-based indexing)
  • Value 3: The element at position 2 in array [1,2,3,4,5]
  • Zero-based: Python/NumPy arrays start counting from 0
๐Ÿ’ก

โ„น๏ธ Zero-Based Indexing: Remember that arrays start counting from 0. So arr1[2] gets the 3rd element, not the 2nd element.

โœ‚๏ธ Advanced Array Slicing

Now let's explore 2D array slicing, which is one of NumPy's most powerful features:

cat numpyy.py

Terminal Output:

import numpy as np

arr2 = np.array([[1,2,3],[4,5,6]])
print("2D Array:")
print(arr2)

subarray = arr2[0:2, 1:3]
print("Sliced Array:")
print(subarray)

Understanding 2D Array Slicing

The slice arr2[0:2, 1:3] needs detailed explanation:

ComponentSyntaxMeaningResult
Row Slice0:2Rows 0 to 1 (excludes 2)Both rows [1,2,3] and [4,5,6]
Column Slice1:3Columns 1 to 2 (excludes 3)Columns with values 2,3 and 5,6
Combined[0:2, 1:3]Intersection of rows and columns2ร—2 subarray

Visual Slicing Breakdown

Let's visualize how the slicing works on our 2D array:

Original Array:

arr2 = [[1, 2, 3],
        [4, 5, 6]]

Step-by-Step Slicing:

StepOperationResult
1Select rows 0:2[[1,2,3], [4,5,6]] (all rows)
2Select columns 1:3[[2,3], [5,6]] (columns 1 and 2)

Running the Slicing Example

python numpyy.py

Terminal Output:

2D Array:
[[1 2 3]
 [4 5 6]]
Sliced Array:
[[2 3]
 [5 6]]

Slicing Result Analysis:

  • Original: 2ร—3 array with 6 elements
  • Sliced: 2ร—2 array with 4 elements
  • Values: [[2,3], [5,6]] - the middle and right columns from both rows
โœ…

โœ… Slicing Power: NumPy slicing allows you to extract any rectangular portion of a 2D array using the [row_start:row_end, col_start:col_end] syntax.

๐Ÿ”„ Array Reshaping

Let's explore array reshaping, which changes the dimensions of an array:

cat numpyy.py

Terminal Output:

import numpy as np

arr1 = np.array([1,2,3,4,5])
print("1D array:", arr1)

reshaped_array = arr1.reshape(5,1)
print("Reshaped Array:")
print(reshaped_array)

Understanding Array Reshaping

ComponentBefore ReshapeAfter ReshapeExplanation
Shape(5,) - 1D with 5 elements(5,1) - 2D with 5 rows, 1 columnConvert horizontal to vertical
Elements[1,2,3,4,5][[1],[2],[3],[4],[5]]Same data, different layout
Total Size5 elements5ร—1 = 5 elementsMust be equal

Running the Reshaping Example

python numpyy.py

Terminal Output:

1D array: [1 2 3 4 5]
Reshaped Array:
[[1]
 [2]
 [3]
 [4]
 [5]]

Reshaping Result Analysis:

  • Original Format: Horizontal array [1 2 3 4 5]
  • Reshaped Format: Vertical column with each element on its own row
  • Structure: 5 rows ร— 1 column = column vector
  • Use Case: Useful for matrix operations and machine learning
๐Ÿ’ก

โ„น๏ธ Reshape Rule: The total number of elements must remain the same. For example, 5 elements can become 5ร—1, 1ร—5, but not 2ร—3 (which would need 6 elements).

๐Ÿ“Š Mathematical Operations

Let's add mathematical operations to our NumPy exploration:

cat numpyy.py

Terminal Output:

import numpy as np

arr1 = np.array([1,2,3,4,5])
print("1D array:", arr1)

mean_value = np.mean(arr1)
print("Mean", mean_value)

Understanding the Mean Calculation

StepOperationCalculationResult
1Sum all elements1 + 2 + 3 + 4 + 515
2Count elements5 elements5
3Divide sum by count15 รท 53.0

Running the Mean Calculation

python numpyy.py

Terminal Output:

1D array: [1 2 3 4 5]
Mean 3.0

Mean Result Analysis:

  • Input: Array [1,2,3,4,5]
  • Calculation: (1+2+3+4+5) รท 5 = 15 รท 5 = 3.0
  • Output: 3.0 (float, not integer)
  • Meaning: The average value of all elements in the array

๐Ÿงฎ Array Sum Operations

Let's add sum operations to explore more mathematical functions:

cat numpyy.py

Terminal Output:

import numpy as np

arr2 = np.array([[1,2,3],[4,5,6]])
print("2D Array:")
print(arr2)

total_sum = np.sum(arr2)
print("Total Sum:", total_sum)

Understanding 2D Array Sum

ArrayElementsSum CalculationResult
arr2[[1,2,3],[4,5,6]]1+2+3+4+5+621
First Row[1,2,3]1+2+36
Second Row[4,5,6]4+5+615
TotalAll elements6 + 1521

Running the Final Example

python numpyy.py

Terminal Output:

2D Array:
[[1 2 3]
 [4 5 6]]
Total Sum: 21

Final Output Analysis:

  • All operations successful: Every NumPy operation worked correctly
  • Data preservation: Original arrays remain unchanged unless explicitly modified
  • Efficient calculations: NumPy performed all mathematical operations quickly
  • Type consistency: Results maintain appropriate data types (float for mean, int for sum)

๐ŸŽฏ Common NumPy Operations Summary

OperationSyntaxPurposeExample Result
Array Creationnp.array([1,2,3])Convert list to NumPy array[1 2 3]
Indexingarr[2]Access element at index3
2D Slicingarr[0:2, 1:3]Extract subarray[[2 3] [5 6]]
Reshapingarr.reshape(5,1)Change array dimensionsColumn vector
Meannp.mean(arr)Calculate average3.0
Sumnp.sum(arr)Add all elements21

๐Ÿšจ Troubleshooting Common Issues

Issue 1: Circular Import Error

Problem: AttributeError: partially initialized module 'numpy' has no attribute 'array'

Cause: File named numpy.py conflicts with the actual NumPy library

Solution: Rename your file to something else (e.g., numpy_tutorial.py, my_numpy.py)

Issue 2: Syntax Errors in Print Statements

Problem: SyntaxError: invalid syntax

Cause: Missing comma in print statement between string and variable

Solution: print("text", variable) not print("text" variable)

Issue 3: Import Errors

Problem: ModuleNotFoundError: No module named 'numpy'

Cause: NumPy not installed

Solution: pip install numpy

๐ŸŽฏ Key Takeaways

โœ… Remember These Points

  1. Avoid Naming Conflicts: Never name your files the same as Python libraries
  2. Zero-Based Indexing: Arrays start counting from 0, not 1
  3. Slicing Syntax: Use [row_start:row_end, col_start:col_end] for 2D arrays
  4. Reshape Rule: Total elements must remain constant when reshaping
  5. Mathematical Functions: NumPy provides efficient functions like mean() and sum()
  6. Array vs List: NumPy arrays display without commas and support mathematical operations
  7. Import Convention: Always use import numpy as np for consistency
โœ…

๐ŸŽ‰ Congratulations! You've mastered NumPy fundamentals including array creation, indexing, slicing, reshaping, and mathematical operations. You also learned how to troubleshoot common issues like circular imports and syntax errors. These skills form the foundation for data science, machine learning, and scientific computing in Python.

๐Ÿ’ฌ Practice Challenge

Try these exercises to reinforce your learning:

  1. Array Creation: Create a 3ร—4 array with numbers 1-12
  2. Slicing Practice: Extract the middle 2ร—2 portion from your array
  3. Reshaping Challenge: Convert a 1D array of 12 elements into different shapes (3ร—4, 2ร—6, 12ร—1)
  4. Statistics: Calculate mean, sum, min, and max of a 2D array
  5. Troubleshooting: Intentionally create and fix a circular import error

This tutorial recreated real terminal sessions showing NumPy fundamentals in action. Every command, error, and solution was explained in detail to help beginners understand both successful operations and common troubleshooting scenarios.

Owais

Written by Owais

I'm an AIOps Engineer with a passion for AI, Operating Systems, Cloud, and Securityโ€”sharing insights that matter in today's tech world.

I completed the UK's Eduqual Level 6 Diploma in AIOps from Al Nafi International College, a globally recognized program that's changing careers worldwide. This diploma is:

  • โœ… Available online in 17+ languages
  • โœ… Includes free student visa guidance for Master's programs in Computer Science fields across the UK, USA, Canada, and more
  • โœ… Comes with job placement support and a 90-day success plan once you land a role
  • โœ… Offers a 1-year internship experience letter while you studyโ€”all with no hidden costs

It's not just a diplomaโ€”it's a career accelerator.

๐Ÿ‘‰ Start your journey today with a 7-day free trial

Related Articles

Continue exploring with these handpicked articles that complement what you just read

More Reading

One more article you might find interesting