Approx. read time: 2.8 min.

Post: Learn about Numpy Arrays in Python programming

Lesson: Introduction to Numpy Arrays in Python

Getting Started with Numpy Arrays

Numpy arrays are a powerful alternative to Python lists. They are particularly useful for handling large amounts of data due to their speed and efficiency. Numpy arrays allow you to perform mathematical operations across entire arrays, which is faster and more intuitive than using Python lists.

Some key advantages of Numpy arrays:

  • Fast: Numpy arrays are more efficient than Python lists.
  • Easy to work with: You can easily create and manipulate arrays.
  • Mathematical operations: You can perform calculations across entire arrays in one go.

In this lesson, we will explore the basics of Numpy arrays with examples.


Example 1: Creating a Numpy Array

You can convert Python lists into Numpy arrays using the numpy.array() function. Let’s create two arrays from height and weight data.

Copy to Clipboard

Explanation:

  • We first create two lists: height and weight.
  • We use np.array() to convert these lists into Numpy arrays: np_height and np_weight.
  • The type() function is used to confirm that np_height is indeed a Numpy array.

Output:

Copy to Clipboard

Example 2: Element-wise Calculations

One of the major benefits of Numpy arrays is the ability to perform element-wise operations. Let’s calculate the Body Mass Index (BMI) for each observation using the formula:

Copy to Clipboard

Explanation:

  • We compute BMI by dividing the weight by the square of the height for each observation.
  • This is an element-wise operation performed on the entire array.

Output (example BMI values):

Copy to Clipboard

Example 3: Subsetting Numpy Arrays

You can easily subset Numpy arrays to extract specific values. Let’s find the BMI values that are greater than 23.

Copy to Clipboard

Explanation:

  • We use a boolean condition bmi > 23 to create a mask (array of True or False values).
  • We use this mask to filter and extract only the BMI values that satisfy the condition.

Output (example):

Copy to Clipboard

Exercise: Convert Weights from Kilograms to Pounds

Task: Convert the list of weights from kilograms to pounds. Use the scalar conversion factor of 1 kg=2.2 lbs.

Steps:

  1. Convert the list of weights in kilograms to a Numpy array.
  2. Convert each weight from kilograms to pounds using element-wise multiplication.
  3. Print the resulting array of weights in pounds.
Copy to Clipboard

Expected Output:

Copy to Clipboard

Final Assignment: Numpy Arrays Practice

Problem: You are given two lists: one containing the heights of individuals in meters and the other containing their weights in kilograms. You need to:

  1. Convert both lists into Numpy arrays.
  2. Calculate the BMI for each individual.
  3. Find and print the BMI values greater than 25.
  4. Convert the weights from kilograms to pounds and print the result.

Here is the data:

  • Heights: [1.72, 1.85, 1.65, 1.90, 1.75] (in meters)
  • Weights: [65.0, 80.5, 72.3, 90.0, 85.6] (in kilograms)

Final Assignment Solution (Answer Key)

Copy to Clipboard

Expected Output:

Copy to Clipboard

Conclusion

In this lesson, we learned how to:

  • Create Numpy arrays from Python lists.
  • Perform element-wise operations on Numpy arrays.
  • Subset arrays based on conditions.
  • Use Numpy to quickly convert units of measurement.

Numpy arrays are an excellent tool for handling large datasets efficiently, and with these basic skills, you can now start exploring more advanced features of Numpy.


Assignment Idea: a. Try working with larger datasets and practice subsetting and element-wise operations. b. Explore other Numpy functions like np.mean(), np.median(), and np.std() for basic statistical calculations.

About the Author: Bernard Aybout (Virii8)

I am a dedicated technology enthusiast with over 45 years of life experience, passionate about computers, AI, emerging technologies, and their real-world impact. As the founder of my personal blog, MiltonMarketing.com, I explore how AI, health tech, engineering, finance, and other advanced fields leverage innovation—not as a replacement for human expertise, but as a tool to enhance it. My focus is on bridging the gap between cutting-edge technology and practical applications, ensuring ethical, responsible, and transformative use across industries. MiltonMarketing.com is more than just a tech blog—it's a growing platform for expert insights. We welcome qualified writers and industry professionals from IT, AI, healthcare, engineering, HVAC, automotive, finance, and beyond to contribute their knowledge. If you have expertise to share in how AI and technology shape industries while complementing human skills, join us in driving meaningful conversations about the future of innovation. 🚀