Python Lecture on Arrays and NumPy

Jul 17, 2024

Python Lecture Notes

Introduction to Arrays

  • Arrays allow us to store multiple values in a single variable.
  • We have learned how to:
    • Insert values
    • Fetch values

Types of Arrays

  1. Single Dimensional Array

    • One row and multiple columns.
    • Example: [1, 2, 3, 4]
  2. Multi-Dimensional Array

    • Multiple rows and multiple columns.
    • Examples:
      • 2D Array: Used for matrices.
      • 3D Array: Can be visualized like a cube.
      • Higher dimensions.

Multi-Dimensional Arrays in Python

  • Standard Python array does not support multi-dimensional arrays out-of-the-box.
  • Example: Attempting multi-dimensional arrays with the array module results in errors.

Introduction to NumPy

  • NumPy is a third-party library that supports multi-dimensional arrays.
  • Use the command pip install numpy to install NumPy.

Installing NumPy

  1. Command Prompt:

    • Use pip install numpy.
    • Make sure it displays numpy successfully installed.
    • Confirm by importing NumPy in Python shell: import numpy
  2. IDE Specific Installation:

    • PyCharm:
      • Go to Settings (Ctrl + Alt + S).
      • Select Project Interpreter.
      • Search for and install NumPy.

Working with NumPy

  • Importing NumPy: import numpy as np
  • Creating Arrays:
    • Single Dimensional: arr = np.array([1, 2, 3])
    • Multi-Dimensional: arr = np.array([[1, 2, 3], [4, 5, 6]])
  • Type specification (optional): arr = np.array([1, 2, 3], dtype=int)

Summary

  • NumPy is essential for working with multi-dimensional arrays in Python.
  • Installation and setup are straightforward via pip and IDE settings.
  • Upcoming topics will cover more advanced NumPy operations and use-cases.

Please leave your comments and hit the like button if you enjoyed the lecture!


Instructor: Ivan Ready