Multidimensional Lists and Functions Diving into Higher Dimensions and Modularity! Dr. Rohit Saluja (IIT Mandi)
- What we learned: - Strings are immutable. - Basic operations: - Concatenation - Slicing - Indexing. - Useful methods: .upper(), .lower(), .replace(), .split(), .join(). - Today: String formatting using old methods and f-strings. - Today’s Topic: Multidimensional Lists and Functions Quick Recap : Strings in Python
What will the output be? Answer in zoom chat:
What will the output be? Answer in zoom chat:
What will the output be? Answer in zoom chat:
What’s the result True or False? Answer in zoom chat:
Motivation name= “Alice” age = 25 print(“My name is name and I am age years old.”) My name is name and I am age years old. What we want: My name is Alice and I am 25 years old. String Formatting Methods
➢ Old-Style Formatting (%) ○ Uses the % operator to embed variables into strings. ○ Syntax: "string % (values)" where %s is for strings, %d for decimal integers, and %f for floating points. String Formatting Methods
String Formatting Methods Output ➢ Old-Style Formatting (%)
➢ .format() Method ○ More powerful than the old-style formatting. It allows placeholders {} in the string which are replaced with values using the .format() method. ○ Syntax: "string {}".format(values) String Formatting Methods
➢ .format() Method String Formatting Methods Output
➢ f-Strings (Python 3.6+) ○ The most modern and concise way to embed variables directly into a string by prefixing the string with an f or F. ○ Syntax: f"string {variable}" String Formatting Methods
➢ f-Strings (Python 3.6+) String Formatting Methods Output
Lecture Overview - Multidimensional Lists - 2D Lists, 3d Lists - List Manipulation - Applications of Multidimensional Lists - Functions - Defining Functions - Function Parameters - Return Values - Lambda Functions
Multidimensional List
What are Multidimensional Lists? A multidimensional list is a list where elements themselves are lists. Example
2D Lists A 2D list is a grid or table-like structure, where rows and columns represent data. Example matrix[0] is [1, 2, 3] Matrix[2] is ?
Accessing Elements in 2D Lists To access elements, use two indices: Example as matrix[0] is [1, 2, 3]
Iterating Through a 2D List You can use nested loops to iterate through a 2D list: Example
In Class Activity Question - What will be the output of the following code?
3D Lists A 3D list is a collection of 2D lists. Example cube[0] is [[1, 2], [3, 4]] cube[0][0] is [1, 2] cube[0][1] is ?
Accessing Elements in 3D Lists To access elements, use three indices: Example As cube[0][1] is [3, 4]
Iterating Through a 3D List You can use nested loops to iterate through a 3D list: Example
In Class Activity Question - What will be the output of the following code?
List Manipulation Adding and Removing Elements/Rows Example
List Manipulation Adding and Removing Elements Example
In Class Activity Question - What will be the output of the following code?
List Comprehension for Multidimensional Lists Example
In Class Activity Question - What will be the output of the following code? Output - [[0, 1], [1, 2]]
Applications of Multidimensional Lists Multidimensional lists are widely used in: - Matrices for mathematical operations - Image processing (e.g., a 2D array for grayscale images, 3D for RGB) - Game boards (e.g., chessboard, tic-tac-toe)
Functions
What are Functions? Functions are reusable blocks of code that perform specific tasks. Syntax
Defining Functions
Function Parameters You can pass data to functions using parameters: Example
Positional vs Keyword Arguments - Positional Arguments: Passed in order - Keyword Arguments: Passed by name Example
Function Return Values Functions can return values using the return statement: Example
Multiple Return Values in Functions A function can return multiple values: Example
In Class Activity Question - What will be the output of the following code?
Lambda Functions A lambda function is an anonymous function defined with a single expression: Example This is similar to- def lambda_add(x, y): return x + y
- What we learned: - Multidimensional Lists - List Manipulation - Applications of Multidimensional Lists - Functions - Defining Functions - Function Parameters - Return Values - Lambda Functions - Next : Classes and Inbuilt Functions Takeaways
Thank You

Multidimensional Lists and Fuctions in python