请应用列表推导式输出九九乘法表。
时间: 2024-04-12 16:32:03 浏览: 63
利用Python输出九九乘法表
当然可以!以下是应用列表推导式输出九九乘法表的代码:
```
multiplication_table = [[i*j for j in range(1, 10)] for i in range(1, 10)]
for row in multiplication_table:
print(row)
```
运行上述代码,你将会得到输出结果:
```
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6, 8, 10, 12, 14, 16, 18]
[3, 6, 9, 12, 15, 18, 21, 24, 27]
[4, 8, 12, 16, 20, 24, 28, 32, 36]
[5, 10, 15, 20, 25, 30, 35, 40, 45]
[6, 12, 18, 24, 30, 36, 42, 48, 54]
[7, 14, 21, 28, 35, 42, 49, 56, 63]
[8, 16, 24, 32, 40, 48, 56, 64, 72]
[9, 18, 27, 36, 45, 54, 63, 72, 81]
```
这就是九九乘法表的结果。列表推导式可以简洁地创建二维列表,通过循环和乘法运算来生成九九乘法表的每一行。希望能对你有所帮助!如果你还有其他问题,请随时提问。
阅读全文