PHP前端开发

python如何打印99乘法表

百变鹏仔 1个月前 (01-22) #Python
文章标签 乘法表
python打印99乘法表的方法:1、使用【for-for】;2、使用【while-while】;3、使用【while-for】;4、使用【for-while】;5、 定义一个变量a,代码为【for i in a:j=1】。

相关学习推荐:python教程

python打印99乘法表的方法:

第1种方式: 使用for-for

# 九九乘法表for i in range(1, 10):    for j in range(1, i+1):        print('{}x{}={}	'.format(j, i, i*j), end='')    print()

立即学习“Python免费学习笔记(深入)”;

第2种方式: 使用while-while

# 九九乘法表i = 1while i <p><img src="https://img.php.cn/upload/image/335/123/914/1601177626388607.png" title="1601177626388607.png" alt="fff783bef784bb30fb7c69df6f81aca.png"></p><p><strong>第3种方式: 使用while-for</strong></p><pre class="brush:php;toolbar:false">i = 1while(i <p><img src="https://img.php.cn/upload/image/583/629/510/1601177631703742.png" title="1601177631703742.png" alt="d528db0166c42a5de5749f713b557f5.png"></p><p><strong>第4种方式: 使用for-while</strong></p><pre class="brush:php;toolbar:false">for i in range(1,10):    j = 0    while j <p><img src="https://img.php.cn/upload/image/575/381/510/1601177641825786.png" title="1601177641825786.png" alt="1ad151cd25a5da36b72389eb0477fb3.png"></p><p><strong>第5种方式: 定义一个变量a</strong></p><pre class="brush:php;toolbar:false">a = [1, 2, 3, 4, 5, 6, 7, 8, 9]for i in a:    j = 1    while j <p>执行结果如下图:</p><p><img src="https://img.php.cn/upload/image/888/896/338/1601177651919792.png" title="1601177651919792.png" alt="c7dc6138b0d1956f356bcd08a12c64e.png"></p><p><strong>第6种方式: 使用1行语句</strong></p><pre class="brush:php;toolbar:false">print(''.join([' '.join(["%2s x%2s = %2s" % (j, i, i*j) for j in range(1, i+1)]) for i in range(1, 10)]))

执行结果如下图: