关键点:外循环控制行,内循环控制列,列数取决于行数(第一行一列,第二行二列……)
方式一:for循环
for i in range(1, 10):
for j in range(1, 10):
if i >= j:
print("{}*{}={}".format(j, i, j * i), end='\t')
print()
方式二:for循环
for i in range(1, 10):
for j in range(1, i + 1):
# print("{}*{}={}".format(i, j, i*j), end='\t')
print("{0}*{1}={2}".format(j, i, j * i), end='\t')
print()
方式三:while循环
row = 1
while row <= 9:
column = 1
while column <= row:
print('{}*{}={}'.format(column, row, row * column), end='\t')
column += 1
print()
row += 1
方式四:列表表达式
L = '\n'.join(
['\t'.join(['{}*{}={}'.format(column, row, row * column) for column in range(1, row + 1)]) for row in range(1, 10)])
print(L)
方式五:递归算法
def multi(i):
if i <= 9:
for j in range(1, i + 1):
print("{}*{}={}".format(j, i, j * i), end="\t")
print()
multi(i + 1)
multi(1)
放在一块文章来源:https://www.toymoban.com/news/detail-769165.html
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
# author:ttt
# 方式一:for循环
for i in range(1, 10):
for j in range(1, 10):
if i >= j:
print("{}*{}={}".format(j, i, j * i), end='\t')
print()
print("分割线".center(100, '-'))
# 方式二: for循环
for i in range(1, 10):
for j in range(1, i + 1):
# print("{}*{}={}".format(i, j, i*j), end='\t')
print("{0}*{1}={2}".format(j, i, j * i), end='\t')
print()
print("分隔线".center(100, '-'))
# 方式三:while循环
row = 1
while row <= 9:
column = 1
while column <= row:
print('{}*{}={}'.format(column, row, row * column), end='\t')
column += 1
print()
row += 1
print("分隔线".center(100, '-'))
# 方式四:列表表达式
L = '\n'.join(
['\t'.join(['{}*{}={}'.format(column, row, row * column) for column in range(1, row + 1)]) for row in range(1, 10)])
print(L)
print("分隔线-".center(100, '-'))
# 方式五:递归算法
def multi(i):
if i <= 9:
for j in range(1, i + 1):
print("{}*{}={}".format(j, i, j * i), end="\t")
print()
multi(i + 1)
multi(1)
实现效果:文章来源地址https://www.toymoban.com/news/detail-769165.html
到了这里,关于Python打印九九乘法表的五种方式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!