Python Basic _ 4-2. 반복문(for)

WONY_yoon·2025년 10월 3일
post-thumbnail

Python for 문법 알아보기

# chapter04-2
# 파이썬 반복문
# for 실습

# 코딩의 핵심
# for in <collection>
#     <Loop Body>

for v1 in range(10):  # 0~9
    print('v1 is : ', v1)    # v1 is :  0 ... v1 is :  9

print()

for v2 in range(1, 11):  # 1~10
    print('v2 is : ', v2)    # v2 is :  1 ... v2 is :  10

print()

for v3 in range(1, 11, 2):  # 1~10 홀수
    print('v3 is : ', v3)    # v3 is :  1, 3, 5, 7, 9

print()

# 1~1000까지의 합
sum1 = 0
for v in range(1, 1001):
    sum1 += v

print('1~1000 sum : ', sum1)                        # 1~1000 sum :  500500
print('1~1000 sum : ', sum(range(1, 1001)))         # 1~1000 sum :  500500
print('1~1000 4의 배수의 합 : ', sum(range(4, 1001, 4)))  # 1~1000 4의 배수의 합 :  125500

# Iterables 자료형 반복
# 문자열, 리스트, 튜플, 집합, 사전(딕셔너리)
# iterable 리턴 함수 : range, reversed, enumerate, filter, map, zip

# 예제 1
names = ['Kim', 'Park', 'Cho', 'Lee', 'Choi', 'Yoon']
for n in names:
    print('You are : ', n)    # You are :  Kim ... You are :  Yoon

print()

# 예제 2
lotoo_numbers = [11, 17, 26, 35, 77, 89]
for n in lotoo_numbers:
    print("Current number : ", n)    # Current number :  11 ... Current number :  89

print()

# 예제 3
word = "Beautiful"
for m in word:
    print('word : ', m)    # word :  B ... word :  l

print()

# 예제 4
my_info = {
    'name': 'Lee',
    'age': 22,
    'city': 'Seoul'
}
for key in my_info:
    print('key :', my_info[key])    # key : Lee, key : 22, key : Seoul

for v in my_info.values():
    print('value :', v)    # value : Lee, value : 22, value : Seoul

print()

# 예제 5
name = 'PineAPpLe'

for n in name:
    if n.isupper():
        print(n)          # 대문자는 그대로 출력
    else:
        print(n.upper())  # 소문자는 대문자로 변환
# 출력: P, I, N, E, A, P, P, L, E

print()

# Break
numbers = [14, 3, 4, 7, 10, 24, 17, 2, 33, 15, 34, 36, 38, 67, 30]

for num in numbers:
    if num == 34:
        print("found : 34!")     # found : 34!
        break
    else:
        print("not found : ", num)  # not found : ... 출력 후 34에서 break

print()

# continue
It = ["1", 2, 5, True, 4.3, complex(4)]
# 숫자만 출력하고 싶음
for v in It:
    if type(v) is bool:
        continue
    print("current type :", v, type(v))
    print("multiply by 2", v*3)
# 출력: "1"(str), 2, 5, 4.3, (4+0j)

print()

# for - else
numbers = [14, 3, 4, 7, 10, 24, 17, 2, 33, 15, 34, 36, 38, 67, 30]
for num in numbers:
    if num == 44:
        print("Found : 24!")
        break
else:
    print("not found : 44!")   # not found : 44!

print()

# 구구단 출력
for i in range(2, 10):
    for j in range(1, 10):
        print('{:4d}'.format(i*j), end=' ')
    print()
# 2단부터 9단까지 구구단 형태 출력

# 변환 예제
name2 = 'Aceman'
print('Reversed', reversed(name2))               # Reversed <reversed object at ...>
print('List', list(reversed(name2)))             # List ['n', 'a', 'm', 'e', 'c', 'A']
print('Tuple', tuple(reversed(name2)))           # Tuple ('n', 'a', 'm', 'e', 'c', 'A')
print('Set', set(reversed(name2)))               # Set {'m', 'c', 'n', 'a', 'A', 'e'}

0개의 댓글