Today we’re going to talk about something that might seem a little boring at first glance but trust me, it’s actually pretty ***** cool. We’re talking about Python shapes and sizes!
Now, before you start yawning and reaching for your coffee mug, let me explain why this is important. When we talk about the “shape” of a data structure in Python, what we really mean is its dimensionality how many axes or dimensions it has. And when we talk about the “size” of a data structure, well… that’s pretty self-explanatory!
So let’s dive right into some examples and see what I mean. First up: lists! Lists are one-dimensional they have just one axis or dimension. You can think of them as being like a line in the sand, with each element (or “item”) represented by a point on that line.
Here’s an example list: `[1, 2, 3]`
This list has three elements one for each point along our imaginary line. If we want to find out how many elements are in this list, we can use the built-in len() function. So if we do `len([1, 2, 3])`, it will return an integer value of 3:
# Define a list with three elements
my_list = [1, 2, 3]
# Use the built-in len() function to find the number of elements in the list
# The len() function returns an integer value representing the length of the list
# Syntax: len(list)
length = len(my_list)
# Print the length of the list
print(length)
# Output: 3
Now let’s move on to something a little more complex arrays! Arrays are also one-dimensional, but they have some additional features that make them useful for certain types of data. For example, you can create an array using the numpy library (which is super handy for scientific computing), and then perform operations like slicing or indexing on it:
# Importing the numpy library and assigning it to the alias "np"
import numpy as np
# Creating an array using the arange function from the numpy library, with values from 0 to 9
my_array = np.arange(10)
# Printing the value at index 5 of the array (which is 5 in this case)
print(my_array[5])
# Printing elements starting from index 2 and ending before index 7 (inclusive) of the array
print(my_array[2:7])
# Output:
# 5
# [2 3 4 5 6]
# Note: Arrays are one-dimensional data structures that can be created using the numpy library. They have additional features such as slicing and indexing, which allow for easy manipulation of data. In this script, we create an array using the arange function and then print specific values from the array using indexing and slicing.
So arrays are still one-dimensional, but they have some extra functionality that makes them more powerful than regular lists. But what about two-dimensional data structures? What do we call those? Well, my friends… you guessed it! We call ’em matrices! Matrices are like spreadsheets they have rows and columns (or “axes”), which gives us a whole new level of complexity to work with.
Here’s an example matrix: `[[1, 2], [3, 4]]`
This matrix has two rows and two columns. If we want to find out how many elements are in this matrix (i.e., the total number of “cells”), we can use a similar approach as before just multiply the number of rows by the number of columns:
# Define a matrix with two rows and two columns
my_matrix = [[1, 2], [3, 4]]
# Use the len() function to get the number of rows in the matrix
num_rows = len(my_matrix)
# Use the len() function to get the number of columns in the first row of the matrix
num_cols = len(my_matrix[0])
# Multiply the number of rows by the number of columns to get the total number of elements in the matrix
total_elements = num_rows * num_cols
# Print the total number of elements in the matrix
print(total_elements)
# Output: 4
# Note: The original script did not have any output, so I added a print statement to display the result.
I hope that was helpful but if not…