Python 官方文档:入门教程 => 点击学习
#学习python,多种种方法实现做九九乘法表。嵌套map,列表解释,生成器表达式功能很强大。# -*- coding: cp936 -*- def jiujiuWhile(a): '''use WHILE for
#学习python,多种种方法实现做九九乘法表。嵌套map,列表解释,生成器表达式功能很强大。
# -*- coding: cp936 -*-
def jiujiuWhile(a):
'''use WHILE for loop'''
y = 1
while y <= a:
x=1
while x <= y:
print "%d X %d = %-2d\t" % (x,y,x*y),
x += 1
y += 1
print
def jiujiuFor(a):
'''use FOR for loop'''
for y in range(1, a+1) :
for x in range(1, y+1) :
print "%d x %d = %-2d\t" % (x,y,x*y),
print
def ThisLine(a):
print "1 X %d = %-2d" % (a,a),
for x in range(2,a+1):
print "\t%d X %d = %-2d" % (x,a,x*a),
print
def jiujiuDigui(a):
if a>1:
jiujiuDigui(a-1)
ThisLine(a)
elif a==1:
ThisLine(1)
def jiujiuList(a):
'''用map()函数一次计算出这个小九九的一行列表'''
for y in range(1,a+1):
jiujiu = map(lambda x: "%d X %d = %-2d" %(x,y,x*y) ,range(1,y+1))
for each in jiujiu:
print str(each)+"\t",
print
def jiujiuMM(a):
'''用map()函数一次计算出这个二维小九九列表'''
jiujiu = map(lambda i:map(lambda x: "%d X %d = %-2d" %(x,i,x*i) ,range(1,i+1)),range(1,a+1))
for x in jiujiu:
for y in x:
print str(y)+"\t",
print
def jiujiuList2(a):
'''用列表解释一次计算出这个二维小九九列表'''
jiujiu = [["%d X %d = %-2d" %(x,y,x*y) for x in range(1,y=1)] for y in range(1,a+1)]
for x in jiujiu:
for y in x:
print str(y)+"\t",
print
#一直在考虑能否在列表解析中使用print,没有成功。例如:[(print x) for x in range(1,9)]。今天看到别人使用join函数实现了使用列表解析一次构造出整个九九乘法表。
print '\n'.join([' '.join(['%s*%s=%-2s' % (y,x,x*y) for y in range(1,x+1)]) for x in range(1,10)])
#实验证明这个语句中的列表解析也可以换成生成器表达式,如下:
print '\n'.join((' '.join(('%s*%s=%-2s' % (y,x,x*y) for y in range(1,x+1))) for x in range(1,10)))
#不过这样比较费解,如果生成器表达式可以使用别的符号就更好了。
jiujiuFor(9)
jiujiuWhile(9)
jiujiuDigui(9)
jiujiuList(9)
jiujiuMM(9)
jiujiuList2(9)
运行的结果如下,完全达到了预期的目的:
1 x 1 = 1
1 x 2 = 2 2 x 2 = 4
1 x 3 = 3 2 x 3 = 6 3 x 3 = 9
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42 7 x 7 = 49
1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40 6 x 8 = 48 7 x 8 = 56 8 x 8 = 64
1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45 6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81
1 X 1 = 1
1 X 2 = 2 2 X 2 = 4
1 X 3 = 3 2 X 3 = 6 3 X 3 = 9
1 X 4 = 4 2 X 4 = 8 3 X 4 = 12 4 X 4 = 16
1 X 5 = 5 2 X 5 = 10 3 X 5 = 15 4 X 5 = 20 5 X 5 = 25
1 X 6 = 6 2 X 6 = 12 3 X 6 = 18 4 X 6 = 24 5 X 6 = 30 6 X 6 = 36
1 X 7 = 7 2 X 7 = 14 3 X 7 = 21 4 X 7 = 28 5 X 7 = 35 6 X 7 = 42 7 X 7 = 49
1 X 8 = 8 2 X 8 = 16 3 X 8 = 24 4 X 8 = 32 5 X 8 = 40 6 X 8 = 48 7 X 8 = 56 8 X 8 = 64
1 X 9 = 9 2 X 9 = 18 3 X 9 = 27 4 X 9 = 36 5 X 9 = 45 6 X 9 = 54 7 X 9 = 63 8 X 9 = 72 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5 = 10 3 X 5 = 15 4 X 5 = 20 5 X 5 = 25
1 X 6 = 6 2 X 6 = 12 3 X 6 = 18 4 X 6 = 24 5 X 6 = 30 6 X 6 = 36
1 X 7 = 7 2 X 7 = 14 3 X 7 = 21 4 X 7 = 28 5 X 7 = 35 6 X 7 = 42 7 X 7 = 49
1 X 8 = 8 2 X 8 = 16 3 X 8 = 24 4 X 8 = 32 5 X 8 = 40 6 X 8 = 48 7 X 8 = 56 8 X 8 = 64
1 X 9 = 9 2 X 9 = 18 3 X 9 = 27 4 X 9 = 36 5 X 9 = 45 6 X 9 = 54 7 X 9 = 63 8 X 9 = 72 9 X 9 = 81
1 X 1 = 1
1 X 2 = 2 2 X 2 = 4
1 X 3 = 3 2 X 3 = 6 3 X 3 = 9
1 X 4 = 4 2 X 4 = 8 3 X 4 = 12 4 X 4 = 16
1 X 5 = 5 2 X 5 = 10 3 X 5 = 15 4 X 5 = 20 5 X 5 = 25
1 X 6 = 6 2 X 6 = 12 3 X 6 = 18 4 X 6 = 24 5 X 6 = 30 6 X 6 = 36
1 X 7 = 7 2 X 7 = 14 3 X 7 = 21 4 X 7 = 28 5 X 7 = 35 6 X 7 = 42 7 X 7 = 49
1 X 8 = 8 2 X 8 = 16 3 X 8 = 24 4 X 8 = 32 5 X 8 = 40 6 X 8 = 48 7 X 8 = 56 8 X 8 = 64
1 X 9 = 9 2 X 9 = 18 3 X 9 = 27 4 X 9 = 36 5 X 9 = 45 6 X 9 = 54 7 X 9 = 63 8 X 9 = 72 9 X 9 = 81
--结束END--
本文标题: Python写的小九九
本文链接: https://lsjlt.com/news/184845.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0