Hi, my name is Mary and I am a senior instructor at Juni Learning. Welcome to this basic numpy array tutorial in Python!
Today we will talk about how to create an array and append data to it in Python. Specifically, we will be learning the difference between a Python list and a Python array, and how to determine which of those data structures you want to use based on the data types you want to store.
By the end of this tutorial you will be able to use the numpy library to create a numpy array in Python.
Who is this for?
- Juni Level: Python Level 2
- Coding Language: Python
- Coding experience: Intermediate
- Challenge Level: Medium
- Approx lines of code: ~2
Learning outcomes
Core concepts practiced:
- Numpy arrays
- Creating data structures
- Data type practice
Prerequisite concepts to know/review:
- Lists
- Indexing
- Concatenation
Keep in mind
Arrays and lists are similar data structures (i.e. ways to store data!) with one major difference. We use lists to store data of all different data types (integers, floats, characters, etc.) together in one list. We use arrays on the other hand to store elements that are all of the same type!
For example:
List: [1, 7, “cats”, 8.2, “J”, “I love Juni!”, 3] Array: [4, 82, 16, 3] Array: [“z”,”y”,”x”]
If for instance you add a character to an array of integers, this will turn all of the elements in the array into characters as all elements must be of the same data type.
General order of steps to implement:
- Import numpy library
- Create an array with elements in it
- Print each element in your array
- Append elements to your array
- Access individual elements in your array through indexing
How do we do each of these steps?
Step 1: Import numpy library
First we are going to import the numpy package just like we import the turtle or random packages in other Juni courses:
This will allow us to use all of the functionality of the numpy package.
Step 2: Initialize an array with elements in it
Initializing a numpy array is similar to creating a list in Python but with slightly different syntax.
First you will create, or initialize, a variable name to refer to your array. I named my array my_array. To tell this variable we want it to be an array we call the function numpy.array()
.
We will then add elements to our array, in this case integers. The syntax of this is a set of hard-brackets with the elements inside all separated by a comma. You can add any number of elements you want, but make sure they are all of the same data type!
This is where we should be up to this point:
Step 3: Print each element in your array
To print the individual integers in this array, we can loop through the array just like we would with a list. We do this by indexing into the array, which is accomplished by typing the name of the array followed by a set of hard brackets, and then filling in the hard brackets with the index number.
Since we are using a for loop, the i iterator increases from zero up until the end of the array, so we can print all of the individual elements by filling i in to these hard brackets:
Step 4: Append elements to your array
With numpy arrays, appending elements to arrays is very different from how you would append elements to a list. In this case you have to concatenate the arrays together!
As a reminder, concatenating means combining two or more things (in this case arrays) into one. To do this we will create a new array (as outlined before) and set that equal to the numpy concatenate function to combine them.
We then plug the two arrays into the concatenate function and put an extra set of parentheses around them. Set this equal to your original array to update it and then print it out to see your results:
This comes in handy if you want to add an element to your array at a different point in your code than when you declare the array, say if a certain condition is met!
Step 5: Access individual elemnents of your array through indexing
Finally, to access individual elements of your array, you can index into your array.
Remember that when indexing we first type the name of what we are indexing into, then a set of hard brackets, and then the index number of the element we want inside the hard brackets.
As always, our index numbers start from 0! For example, to print the second item in our array, we would do something like this:
Remember to access the first element of an array, the index number would have to be 0.
Want more of a challenge? Try adding these bonus features.
Extra features:
Create arrays of different data types! For example, create an array of floats and an array of characters. Remember every element in the array has to be of the same data type.
Creative suggestions:
Something unique about arrays in comparison to lists is that, because all of the elements are of the same type, you can perform operations directly on arrays. Test out these array operations by multiplying your integer array by some integer and printing it. Experiment with other operations as well!
Great job — now check out more tutorials!
Thanks for reading and hope you had fun making this project with me!
Built the project above? We'd love to see it! If you're interested in sharing your coding project or experiences with diversity in STEM, please reach out to us at hello@learnwithjuni.com.
Every week, we’ll be posting project tutorials like this one, for different coding languages and experience levels, as well as math tutorials.
Visit our coding projects blog page to find our other tutorials in more coding languages! You can also subscribe to our newsletter using the form to the right of this page for more updates on our programs and newest coding tutorials.
Need more help, or want to keep learning?
Looking up your coding questions is one of the best ways to learn! Another great way to learn is from an experienced coder or instructor. Juni Learning Computer Science Instructors like Mary work closely with students ages 8-18, and are specially trained to adapt to each child's unique learning style, pace, and interests.
Read more about coding for kids and our curriculum, or speak with a Juni Advisor by calling (650) 263-4306 or emailing advisors@learnwithjuni.com to learn which course may be best for your child’s coding journey.