A NumPy array is a special type of container in Python that holds a collection of items, usually numbers. Unlike a regular shopping list, these arrays are designed to be extremely fast and efficient for computers to process. They are the foundation of almost all data science and artificial intelligence work today. You install it once with pip install numpy, then import as np to create fixed-type arrays that zoom through math ops on tons of data.
Creating an Array
To create an array, you essentially take a standard list of items and hand it over to NumPy to “upgrade” it. This process tells your computer to treat this group of items as a single, organized unit rather than just a collection of random objects. It is the first step in unlocking all the advanced features NumPy has to offer.
import numpy asnp # Turning a list of temperatures into a NumPy array temps=np.array([22.5, 24.0, 19.8, 21.2]) print(temps)
Inserting New Items (Adding)
When you want to put a new item onto your shelf, you use a process called “appending”. Since arrays are very strict about their size to stay fast, adding an element actually creates a brand-new, slightly larger shelf that includes your new item. It is like adding an extra seat to a row of chairs.
import numpy asnp arr=np.array([1, 2, 3]) # Adding the number 4 to the end new_arr=np.append(arr, 4) print(new_arr) #output: [1 2 3 4]
Deleting Data (Removing)
If you have a piece of data that is no longer needed or was entered by mistake, you can remove it using its “index” (its position number). When you delete an item, the array cleans itself up by shifting the remaining items to fill in the empty space. This ensures your data stays compact and organized without any “empty holes” in the middle.
import numpy asnp colors=np.array(['Red', 'Blue', 'Green', 'Yellow']) # Removing 'Blue' which is at position index 1 new_colors=np.delete(colors, 1) print(new_colors) #output: ['Red' 'Green' 'Yellow']
Slicing an Array
Slicing is like cutting a specific piece out of a long loaf of bread. You tell Python where to start and where to stop, and it gives you back just that middle section. This is incredibly helpful when you have thousands of pieces of data but only want to look at a small “slice” of them.
import numpy asnp letters=np.array(['A', 'B', 'C', 'D', 'E']) # Taking a slice from index 1 to 3 slice_of_letters=letters[1:4] print(slice_of_letters) # Outputs: B, C, D
Searching for Elements
Searching is the process of asking the array, “Where is the number I’m looking for?” Python will look through the whole shelf and give you the address (the index) of every spot where that number lives. It’s like using a “Find” tool in a document to highlight specific words.
import numpy asnp prices=np.array([5, 10, 15, 10, 20]) # Finding where the price is exactly 10 where_is_ten=np.where(prices==10) print(where_is_ten) # output: (array([1, 3]),)
Changing Existing Values (Updating)
Updating is the process of replacing an old value with a new one at a specific location. You don’t need to delete the whole array; you just point to the position you want to fix and give it a new assignment. It’s like erasing a mistake on a whiteboard and writing the correct number in its place.
import numpy asnp inventory=np.array([5, 10, 15]) # Changing the very first item (position 0) from 5 to 100 inventory[0] =100 print(inventory) #output: [100 10 15]
Different Operations on Python Arrays
Python arrays (lists) support handy operations like sorting with sort(), counting with count(), or joining into strings via join(). Math isn’t built-in for whole lists, but you can sum numbers with sum() or find max/min easily. These make data dance—reverse, shuffle, or copy as needed. They’re everyday tools for organizing, like alphabetizing a playlist. Chain them for power without fancy code.
Reversing an array is like looking at your data in a mirror. The item that was last becomes first, and the first becomes last. This is useful when you want to see the most recent data first or if you simply need to process a sequence in the opposite direction.
Extending (or concatenating) is like taking two separate shelves and gluing them together to make one long shelf. This allows you to combine different sets of data into a single master list. It is a very common task when you are gathering information from multiple different sources.
import numpy asnp group_a=np.array([1, 2]) group_b=np.array([3, 4]) # Joining the two groups together big_group=np.concatenate((group_a, group_b)) print(big_group) # Output: [1 2 3 4]