Part of the series

Several example codes

~2 min read · Updated Sep 17, 2025

Program Overview

This Python program creates a 3×3 matrix and fills it with random integers between 0 and 20.
It then performs the following operations:
- Calculates and displays the total sum of all elements
- Calculates and displays the average of all elements
- Displays the maximum value in each row
- Displays the minimum value in each column


Python Code:


import random

# Create 3x3 matrix with random values between 0 and 20
matrix = [[random.randint(0, 20) for _ in range(3)] for _ in range(3)]

# Display the matrix
print("Matrix:")
for row in matrix:
    print(row)

# Sum and average of all elements
total_sum = sum(sum(row) for row in matrix)
average = total_sum / 9

print("\nTotal sum of elements:", total_sum)
print("Average of elements:", round(average, 2))

# Maximum of each row
print("\nMaximum value in each row:")
for i, row in enumerate(matrix):
    print(f"Row {i+1}: {max(row)}")

# Minimum of each column
print("\nMinimum value in each column:")
for col in range(3):
    column_values = [matrix[row][col] for row in range(3)]
    print(f"Column {col+1}: {min(column_values)}")

Sample Output:


Matrix:
[12, 3, 18]
[7, 0, 14]
[20, 5, 6]

Total sum of elements: 85
Average of elements: 9.44

Maximum value in each row:
Row 1: 18
Row 2: 14
Row 3: 20

Minimum value in each column:
Column 1: 7
Column 2: 0
Column 3: 6

Explanation:

Here’s how the program works:
- A 3×3 matrix is generated using nested list comprehensions and random.randint()
- The matrix is printed row by row
- The total sum is calculated using nested sum() calls
- The average is computed by dividing the total by 9
- Each row’s maximum is found using max()
- Each column’s minimum is found by extracting column values and applying min()


Written & researched by Dr. Shahin Siami

Related Articles

Complete Guide to Python Decorators – Enhancing Function Behavior with Reusable Logic

Decorators in Python are a powerful tool for modifying or extending the behavior of functions and classes without changing their original code. They allow developers to implement reusable logic such as logging, authentication, caching, or timing in a clean and maintainable way. This article explores the structure, definition, common use cases, and practical examples of decorators in Python.

Continue

Comprehensive Guide to Python Classes – Object-Oriented Design, Structure, Inheritance, and Practical Use

Classes in Python are the foundation of object-oriented programming, allowing developers to define complex data structures, encapsulate logic, and create scalable, maintainable systems. This article walks through the fundamentals of defining classes, constructors, methods, inheritance, encapsulation, and real-world applications in Python projects.

Continue

Several example codes

understand the Python programming language

Continue

Complete Guide to Getting Text Input from Users in Python

In Python, accepting input from users is one of the simplest yet most powerful features for interactive programming. This article explores the input() function in depth, explains how to cast types, validate user data, and use input within loops and conditions. Real-life examples help clarify each concept, making this guide perfect for beginners and intermediate learners.

Continue

Comprehensive Guide to Functions in Python: Structure, Types, and Use Cases

Functions in Python are essential building blocks for writing modular, reusable, and maintainable code. This article introduces function definitions, distinguishes between built-in and user-defined functions, explores parameters and return values, and presents advanced topics such as recursion and lambda expressions. Whether you're a beginner or brushing up for interviews, this guide offers a clear and accessible foundation.

Continue

Loops, Iterators, and Repetition Control in Python

Loops, Iterators, and Repetition Control in Python

Continue