# Python: Print a given N by M matrix of numbers line by line in forward > backwards > forward >... order

## Python Basic - 1: Exercise-78 with Solution

Write a Python program to print a given N by M matrix of numbers line by line in forward > backwards > forward >... order.

Input matrix:

[[1, 2, 3, 4],

[5, 6, 7, 8],

[0, 6, 2, 8],

[2, 3, 0, 2]]

Output:

1

2

3

4

8

7

6

5

0

6

2

8

2

0

3

2

**Sample Solution**:

**Python Code:**

```
def print_matrix(nums):
flag = True
for line in nums:
if flag == True:
i = 0
while i < len(line):
print(line[i])
i += 1
flag = False
else:
i = -1
while i > -1 * len(line) - 1:
print(line[i])
i = i - 1
flag = True
print_matrix([[1, 2, 3, 4],
[5, 6, 7, 8],
[0, 6, 2, 8],
[2, 3, 0, 2]])
```

Sample Output:

1 2 3 4 8 7 6 5 0 6 2 8 2 0 3 2

**Flowchart:**

**Python Code Editor:**

**Have another way to solve this solution? Contribute your code (and comments) through Disqus.**

**Previous:** Write a Python program to find the maximum profit in one transaction.

**Next:** Write a Python program to compute the largest product of three integers from a given list of integers.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**Weekly Trends**- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join