Write a Python program to print the following number pattern using nested for loop. 5 10 15 20 25 4 8 12 16 3 6 9 2 4 1
时间: 2024-10-22 16:14:59 浏览: 18
python角谷猜想-25-列表的遍历之for.ev4.rar
Sure! Here's a simple Python program that prints the given number pattern using nested `for` loops:
```python
# Number pattern starts from 1 and increments by 5 for each row
pattern = [i * 5 for i in range(1, 6)]
# Loop through rows
for i in pattern:
# Loop through columns (decrementing by 1 for each row)
for j in range(i, 0, -1):
print(j, end=" ")
# Move to the next line after printing a row
print()
```
This code first defines the pattern as a list of numbers starting from 5 and increasing by 5 for each step down. Then it uses two nested `for` loops: one iterates over the rows, and the inner one prints the numbers decreasing from the current row value to 1. The `end=" "` argument in `print()` is used to separate the numbers with spaces instead of newlines, and `print()` without arguments moves to the next line after each row.
Here's the output when you run this program:
```
5 10 15 20 25
4 8 12 16
3 6 9
2 4
1
```
阅读全文