Python/Python Programming
[Python] multiplication table (파이썬 구구단)
Pydole
2019. 10. 10. 16:35
for문과 range를 이용한 구구단 만들기
# 구구단 만들기
for x in range(2,10,1):
for y in range(1, 10, 1):
print(x, '*', y, '=', x*y )
-------------------------------------
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
...
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
# 중첩 while문을 이용한 구구단
x = 1
while x < 9:
x += 1
y = 0
while y < 9:
y += 1
print(x,'*',y,'=',x * y)
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
...
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
# list Comprehension 을 이용한 구구단
for x in [ [x, y, x * y] for x in range(2,10,1) for y in range(1, 10, 1) ]:
print(x[0],'*',x[1],'=',x[2])
---------------------------------------------------------------------------
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
...
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81