Earn 20 XP


Learning Objectives

  • Understanding keywords
  • Arrays
  • Lists vs Arrays

Before we get into the Numpy library in Python and its functionalities, we will first look at what are Arrays, Vectors, and Matrices - these are basic concepts of Linear Algebra, a field of Mathematics. These concepts are also used extensively in Data Science, so we must familiarize ourselves with them.

Understanding Keywords

Before jumping into what arrays are all about, we need to recap what we learned about Lists in the “Introduction to Python Basics for Data Science” course. In addition, we need to learn about some keywords such as a “Dimension”.

List

  • As the name suggests, List is an ordered sequence of data. In real life, if you could make a list of things that come to your mind (or event for any specific purpose), it could be something like this –

    • Brush
    • Leuven
    • 48851964400
    • 3.14
    • Mom
  • You could make your own list & include whatever you want in it. So, in my list, I have included what I do early in the morning, my city, my mobile number, the value of pi to two digits, and mom.

  • If you look at it, my list has different types of data – strings, float, and integer. And, this is the kind of flexibility Python List provides. It can hold different types of data types. Declaring a List is fairly straightforward. You use square brackets ([]) and separate the items by a comma. Let me write an example -

    A = ["Brush", "Leuven", 48851964400, 3.14, "Mom"]

Dimensions

Now, what are dimensions? Let’s take the example of a box to explain this. A box has three dimensions - width, length and depth (or height). Similarly, in data science we will be working with “N” dimensions in a data structure (list, arrays, vectors etc). “N” could be any number.

image.png

What are Arrays?

  • Similar to a list, an array is also a data structure which can hold more than one value at a time.
  • However, an array can only hold a collection of ordered elements of the same data type.

Here is an intuitive example of an array: Imagine you have a bunch of toy cars. Each toy car is exactly the same, but they are all different colors. You can line them up on a shelf to keep them organized. The shelf would be like an array, and each toy car would be like an element in the array. Just like how you can identify each toy car by its position on the shelf, you can identify each element in an array by its position in the array.

For example, you might have a red toy car at the first position, a blue toy car at the second position, and a green toy car at the third position. In an array, these would be called the first element, second element, and third element, respectively.

Examples of Arrays & Lists

Arrays - Examples

  • [red, blue, green] is an array of car colors in the above toy car example.
  • [1, 2, 3, 4, 5] is an array of integers.
  • [ ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ ] is an array of strings.
  • [[ 1 , 2 , 3 , 4],
    [ 5, 6, 7, 8 ]] is a 2 dimensional array.

Lists - Examples

  • [ 1, 2, 3, ‘a’, 5 ] is a list containing both integers and a string.
  • [ 1, 2, 3, 4, ‘b’, [ 1, 2, 3 ] ] is a list containing integers, a string, and another list.

Why Arrays?

  • A combination of Arrays, together with Python could save you a lot of time. Arrays help reduce the overall size of your code.

List vs Array

  • A list can store different data types such as integers, strings, etc., whereas an array stores only single data type values, i.e., you can only have an array of integers, an array of strings, etc.