Lesson 1
Exploring the Dimensions: A Beginner's Guide to Multidimensional Arrays in Python
Topic Overview

Welcome to today's session on "Multidimensional Arrays and Their Traversal in Python ". Multidimensional arrays are types of arrays that store arrays at each index instead of single elements. Picture it as an 'apartment building' with floors (the outer array) and apartments on each floor (the inner array). Our goal today is to strengthen your foundational knowledge of these 'apartment buildings' and how to handle them effectively in Python.

Creating Multidimensional Arrays

To construct a multidimensional or nested array in Python, we use lists inside lists. Here's an example of a 2-dimensional array:

Python
1# Creating a 2D array 2array = [[1, 2, 3], 3 [4, 5, 6], 4 [7, 8, 9]] 5print(array)

In this example, array is a 2-dimensional array, just like a 3-storey 'apartment building,' where every floor is an inner list.

Indexing in Multidimensional Arrays

All indices in Python arrays are 0-based. Let's say you want to visit an apartment on the second floor (index 1) and bring a package to the first unit (index 0) in this building. Here's how you can do it:

Python
1# Accessing an element 2print(array[1][0]) # Outputs: 4

We visited the element 4 in the array by its position. The number 1 inside the first square brackets refers to the second inner list, and 0 refers to the first element of that list.

Updating Multidimensional Arrays

Continuing with the apartment-building analogy, suppose the task was to replace the old locker code (the second element in the first list) with a new one. Here's how we can achieve this:

Python
1# Updating an element 2array[0][1] = 'New Code' 3print(array)
Common Built-in Methods

Python offers a variety of built-in methods that are handy with multidimensional arrays:

  1. len(Array): It's like asking how many floors are there in our 'apartment building'. It gives us the number of rows in an array:
Python
1# Finding the number of rows 2num_floors = len(array) 3print(num_floors) # Outputs: 3
  1. append(Element): With append(), we can add a new floor and units on that floor to our 'apartment building'.
Python
1# Adding a new row to our array 2array.append(['Unit-1', 'Unit-2', 'Unit-3']) 3print(array)
  1. remove(Element): We can rely on remove() to help us get rid of a floor or apartment in our 'apartment building'.
Python
1# Removing an element 2array[1].remove('New Code') 3print(array)
Traversing Multidimensional Arrays

We can visit every floor (outer array) and every apartment on each floor (inner array) by using nested loops.

Python
1array = [["Apt 101", "Apt 102", "Apt 103"], 2 ["Apt 201", "Exit Floor", "Apt 203"], 3 ["Apt 301", "Apt 302", "Apt 303"]] 4# Loop through 2D array 5for floor in array: 6 for unit in floor: 7 print(unit, end =', ') 8 print() 9""" 10Prints: 11Apt 101, Apt 102, Apt 103, 12Apt 201, Exit Floor, Apt 203, 13Apt 301, Apt 302, Apt 303, 14"""
Break/Continue in Nested Loops

Sometimes, when we visit every apartment on each floor, we might need to start visiting the next floor midway. break helps us exit the current loop, while continue helps us skip the current iteration and move to the next one.

Python
1# Break in nested loop 2for floor in array: 3 for unit in floor: 4 if unit == 'Exit Floor': 5 break 6 print(unit, end =', ') 7 print() 8""" 9Prints: 10Apt 101, Apt 102, Apt 103, 11Apt 201, 12Apt 301, Apt 302, Apt 303, 13"""

Here, as soon as 'Exit Floor' is found on a floor, the entire loop breaks, and no further units on the floor are visited. However, the further units are processed as before, as break breaks only the nested loop.

We can also make use of continue in a similar scenario:

Python
1# Continue in nested loop 2for floor in array: 3 for unit in floor: 4 if unit == 'Exit Floor': 5 continue 6 print(unit, end =', ') 7 print() 8""" 9Prints: 10Apt 101, Apt 102, Apt 103, 11Apt 201, Apt 203, 12Apt 301, Apt 302, Apt 303, 13"""

In this case, when 'Exit Floor' is encountered, the continue statement is executed. This skips printing 'Exit Floor' and continues with the next unit in the same floor. The loop doesn't stop entirely but simply skips over 'Exit Floor', resulting in a missing apartment in the printout for the second floor.

Lesson Summary and Practice

That was exciting! We went through various operations on multidimensional arrays, starting from their creation, methods to update, and useful built-in methods. We also learned how we can visit every floor and every apartment on each floor.

Practice solidifies learning! Your new adventure awaits in our upcoming practical exercises, where you can apply these concepts on multidimensional arrays! Buckle up and have fun!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.