Day 6— Learning Python

Maitreyee Maheshwari
3 min readApr 12, 2024

Welcome to your exciting journey into the world of Python! This guide will serve as your daily companion as you embark on a structured learning path, one step at a time. Python, known for its readability and versatility, is a fantastic choice for beginners and seasoned programmers alike. By following this daily plan, you’ll gain a solid foundation in the core concepts and gradually build your programming skills.

2D Lists

A 2D list, also known as a list of lists, is a list in which each element is itself a list. This allows you to represent data in a two-dimensional grid, like a table with rows and columns.

matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

Here, matrix is a 2D list with 3 rows and 3 columns.

Accessing elements from 2-D list:

# Accessing the element at row 1, column 2
element = matrix[1][2]
print("Element at row 1, column 2:", element)

In this example:

  • matrix[1] accesses the second row of the matrix (remember, indexing in Python starts from 0).
  • matrix[1][2] then accesses the third element (index 2) in this row, which is 6 in this case.

So, the output will be:

Element at row 1, column 2: 6

Here’s what happens step by step:

  1. matrix[1] returns the second row [4, 5, 6].
  2. matrix[1][2] accesses the third element (index 2) in this row, which is 6.

You can access any element in the 2D list by providing the appropriate row and column indices. Make sure the indices you provide are within the bounds of the list to avoid IndexError.

Nested Loops:

A nested loop is a loop inside another loop. When you have a 2D list, you typically use nested loops to iterate over its elements.

for row in matrix:
for item in row:
print(item, end=' ')
print()

This code will print each element of the 2D list matrix, row by row.

Let’s break down the code:

  • The outer loop iterates over each row in the matrix list.
  • The inner loop iterates over each item in the current row.
  • The print(item, end=’ ‘) statement prints each item followed by a space, without moving to a new line.
  • The print() statement at the end of the inner loop is used to move to the next line after printing all items in the current row.

Now, let’s combine lists and nested loops to perform some operations on a 2D list.

Example 1: Finding the sum of all elements in a 2D list.

matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
total_sum = 0
for row in matrix:
for item in row:
total_sum += item
print("Sum of all elements in the matrix:", total_sum)

In conclusion, mastering the use of 2D arrays in Python is a crucial step in enhancing your programming capabilities, especially for tasks involving data manipulation, scientific computing, and image processing. By understanding how to create, access, and manipulate 2D arrays, you can efficiently handle complex data structures, significantly improving your problem-solving skills in Python. This knowledge not only allows for the structured storage and manipulation of data in a tabular format but also opens up new possibilities for solving intricate problems with more efficiency and elegance. As we continue to explore the depths of Python programming, the ability to work with 2D arrays will undoubtedly prove invaluable in tackling a wide range of applications. Stay tuned for Day 7.

--

--