Jan 1, 2023

파이썬 반복문 (for문), range(..., ..., ...,) example

■ 문자로 for문

for x in "abcdef":
    print(x)

Output
a b c d e f

■ List로 for문

words = ["apple", "bus", "camera", "dog", "eye", "five", "game"]

for x in words:
    print(x)

Output
apple bus camera dog eye five game

■ for문 range 활용 I

for i in range(6):
    print(i)

Output
0 1 2 3 4 5

■ for문 range 활용 II

for i in range(1, 6):
    print(i)

Output
1 2 3 4 5

■ for문 range 활용 III

for i in range(0, 8, 2):
    print(i)

Output
0 2 4 6

■ for문 range 활용 IV

for i in range(100, 50, -10):
    print(i)

Output
100 90 80 70 60


No comments:

Post a Comment