-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathmatrix.py
More file actions
32 lines (21 loc) · 792 Bytes
/
matrix.py
File metadata and controls
32 lines (21 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""
Different ways to create a 5x5 matrix initialized with zeros.
This file demonstrates three approaches for educational purposes.
"""
# 1️⃣ Using nested loops
matrix_loop = []
for row in range(5):
matrix_loop.append([])
for _ in range(5):
matrix_loop[row].append(0)
# 2️⃣ Using list comprehension (recommended)
matrix_comprehension = [[0 for _ in range(5)] for _ in range(5)]
# 3️⃣ Using multiplication (be careful with nested lists!)
matrix_multiplication = [[0] * 5 for _ in range(5)]
if __name__ == "__main__":
print("Matrix created using loops:")
print(matrix_loop)
print("\nMatrix created using list comprehension:")
print(matrix_comprehension)
print("\nMatrix created using multiplication:")
print(matrix_multiplication)