python如何打印99乘法表?在Python中,打印99乘法表的方法有很多种,比如:for-for、while-while、while-for等,本文为大家介绍6种打印99乘法表的方法,快来学习一下吧。
1、使用for-for
#九九乘法表
for i in range(1.10)
for j in range(1.i+1):
print('{}x{}={}\t'.format(j,i,i*j),end='')
print()
2、while-while
# 九九乘法表
i = 1
while i <= 9:
j = 1
while(j <= i): #j的大小是由i来控制的
print('%d*%d=%-3d' %(i, j, i*j),end='\t' )
j += 1
print('')
i += 1
3、while-for
i = 1
while(i <=9):
for j in range (1.i+1): #range()函数左闭右开
print('%d*%d=%-3d'%(i,j,i*j),end='')
i += 1
print()
4、for-while
for i in range(1.10):
j = 0
while j < i:
j += 1
print("%d*%d=%-3d"%(i,j,i*j),end='')
print()
5、定义一个变量a
a = [1. 2. 3. 4. 5. 6. 7. 8. 9]
for i in a:
j = 1
while j <= i:
print('%d*%d=%-3d'%(i,j,i*j),end='\t')
# %-3d 是控制输出结果占据3位,且从左面开始对齐
j += 1
print()
6、使用1行语句
print('\n' .join([' '.join(["%2s x%2s = %2s" % (j, i, i*j)for j in range(1. i+1)])for i in range(1. 10)]))
标签:%-,end,Python,range,99,while,print,乘法表,3d From: https://blog.51cto.com/u_14661964/5992981