Skip to content

30 Days Of Python: Day 10 - Loops

Twitter Follow

Author: Asabeneh Yetayeh
Second Edition: July, 2021

<< Day 9 | Day 11 >>

30DaysOfPython

πŸ“˜ Day 10

Loops

인생은 μΌμƒμœΌλ‘œ 가득 μ°¨ μžˆμŠ΅λ‹ˆλ‹€. ν”„λ‘œκ·Έλž˜λ°μ—μ„œ μš°λ¦¬λŠ” λ˜ν•œ λ§Žμ€ 반볡 μž‘μ—…μ„ μˆ˜ν–‰ν•©λ‹ˆλ‹€. 반볡 μž‘μ—…μ„ μ²˜λ¦¬ν•˜κΈ° μœ„ν•΄ ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄λŠ” 루프λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. Python ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄λŠ” λ˜ν•œ λ‹€μŒ μœ ν˜•μ˜ 두 루프λ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€.

  1. while loop
  2. for loop

While 루프

μš°λ¦¬λŠ” while 루프λ₯Ό λ§Œλ“€κΈ° μœ„ν•΄ μ˜ˆμ•½μ–΄ while 을 μ‚¬μš©ν•©λ‹ˆλ‹€. 주어진 쑰건이 만쑱될 λ•ŒκΉŒμ§€ λ¬Έ 블둝을 반볡적으둜 μ‹€ν–‰ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€. 쑰건이 거짓이 되면 루프 λ’€μ˜ μ½”λ“œ 행이 계속 μ‹€ν–‰λ©λ‹ˆλ‹€.

  # syntax
while condition:
    code goes here

μ˜ˆμ‹œ:

count = 0
while count < 5:
    print(count)
    count = count + 1
#prints from 0 to 4

μœ„μ˜ while λ£¨ν”„μ—μ„œ countκ°€ 5일 λ•Œ 쑰건이 falseκ°€ λ©λ‹ˆλ‹€. μ΄λ•Œ 루프가 μ€‘μ§€λ©λ‹ˆλ‹€. 쑰건이 더 이상 참이 아닐 λ•Œ μ½”λ“œ 블둝을 μ‹€ν–‰ν•˜κ³  μ‹Άλ‹€λ©΄ else λ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

  # syntax
while condition:
    code goes here
else:
    code goes here

μ˜ˆμ‹œ:

count = 0
while count < 5:
    print(count)
    count = count + 1
#prints from 0 to 4

μœ„μ˜ 루프 쑰건은 countκ°€ 5이고 루프가 μ€‘μ§€λ˜κ³  싀행이 else 문을 μ‹œμž‘ν•˜λ©΄ 거짓이 λ©λ‹ˆλ‹€. 결과적으둜 5κ°€ μΈμ‡„λ©λ‹ˆλ‹€.

Break κ³Ό Continue - Part 1

  • 쀑단: λ£¨ν”„μ—μ„œ λ²—μ–΄λ‚˜κ±°λ‚˜ μ€‘λ‹¨ν•˜κ³  싢을 λ•Œ 쀑단을 μ‚¬μš©ν•©λ‹ˆλ‹€.
# syntax
while condition:
    code goes here
    if another_condition:
        break

μ˜ˆμ‹œ:

count = 0
while count < 5:
    print(count)
    count = count + 1
    if count == 3:
        break

μœ„μ˜ while λ£¨ν”„λŠ” 0, 1, 2만 μΈμ‡„ν•˜μ§€λ§Œ 3에 λ„λ‹¬ν•˜λ©΄ μ€‘μ§€ν•©λ‹ˆλ‹€.

  • 계속: continue 문을 μ‚¬μš©ν•˜λ©΄ ν˜„μž¬ λ°˜λ³΅μ„ κ±΄λ„ˆλ›°κ³  λ‹€μŒμ„ 계속할 수 μžˆμŠ΅λ‹ˆλ‹€.
  # syntax
while condition:
    code goes here
    if another_condition:
        continue

μ˜ˆμ‹œ:

count = 0
while count < 5:
    if count == 3:
        continue
    print(count)
    count = count + 1

μœ„μ˜ while λ£¨ν”„λŠ” 0, 1, 2 및 4만 μΈμ‡„ν•©λ‹ˆλ‹€(3을 κ±΄λ„ˆλœλ‹ˆλ‹€).

For 루프

for ν‚€μ›Œλ“œλŠ” λ‹€λ₯Έ ν”„λ‘œκ·Έλž˜λ° 언어와 μœ μ‚¬ν•˜μ§€λ§Œ ꡬ문이 μ•½κ°„ λ‹€λ₯Έ for 루프λ₯Ό λ§Œλ“œλŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€. λ£¨ν”„λŠ” μ‹œν€€μŠ€(즉, λͺ©λ‘, νŠœν”Œ, 사전, 집합 λ˜λŠ” λ¬Έμžμ—΄)λ₯Ό λ°˜λ³΅ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€.

  • For loop with list
# syntax
for iterator in lst:
    code goes here

μ˜ˆμ‹œ:

numbers = [0, 1, 2, 3, 4, 5]
for number in numbers: # number is temporary name to refer to the list's items, valid only inside this loop
    print(number)       # the numbers will be printed line by line, from 0 to 5
  • For loop with string
# syntax
for iterator in string:
    code goes here

μ˜ˆμ‹œ:

language = 'Python'
for letter in language:
    print(letter)


for i in range(len(language)):
    print(language[i])
  • For loop with tuple
# syntax
for iterator in tpl:
    code goes here

μ˜ˆμ‹œ:

numbers = (0, 1, 2, 3, 4, 5)
for number in numbers:
    print(number)
  • 사전을 μ‚¬μš©ν•œ For 루프 사전을 ν†΅ν•œ λ£¨ν”„λŠ” μ‚¬μ „μ˜ ν‚€λ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€.
  # syntax
for iterator in dct:
    code goes here

μ˜ˆμ‹œ:

person = {
    'first_name':'Asabeneh',
    'last_name':'Yetayeh',
    'age':250,
    'country':'Finland',
    'is_marred':True,
    'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
    'address':{
        'street':'Space street',
        'zipcode':'02210'
    }
}
for key in person:
    print(key)

for key, value in person.items():
    print(key, value) # this way we get both keys and values printed out
  • Loops in set
# syntax
for iterator in st:
    code goes here

μ˜ˆμ‹œ:

it_companies = {'Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'}
for company in it_companies:
    print(company)

Break κ³Ό Continue - Part 2

짧은 μ•Œλ¦Ό: 쀑단 : 루프가 μ™„λ£Œλ˜κΈ° 전에 μ€‘λ‹¨ν•˜κ³  싢을 λ•Œ 쀑단을 μ‚¬μš©ν•©λ‹ˆλ‹€.

# syntax
for iterator in sequence:
    code goes here
    if condition:
        break

μ˜ˆμ‹œ:

numbers = (0,1,2,3,4,5)
for number in numbers:
    print(number)
    if number == 3:
        break

μœ„μ˜ μ˜ˆμ—μ„œ λ£¨ν”„λŠ” 3에 λ„λ‹¬ν•˜λ©΄ μ€‘μ§€λ©λ‹ˆλ‹€.

계속: 루프 λ°˜λ³΅μ—μ„œ 일뢀 단계λ₯Ό κ±΄λ„ˆλ›°κ³  싢을 λ•Œ 계속을 μ‚¬μš©ν•©λ‹ˆλ‹€.

  # syntax
for iterator in sequence:
    code goes here
    if condition:
        continue

μ˜ˆμ‹œ:

numbers = (0,1,2,3,4,5)
for number in numbers:
    print(number)
    if number == 3:
        continue
    print('Next number should be ', number + 1) if number != 5 else print("loop's end") # for short hand conditions need both if and else statements
print('outside the loop')

μœ„μ˜ μ˜ˆμ—μ„œ μˆ«μžκ°€ 3이면 쑰건 λ‹€μŒ 단계(루프 λ‚΄λΆ€)λ₯Ό κ±΄λ„ˆλ›°κ³  반볡이 남아 있으면 루프 싀행이 κ³„μ†λ©λ‹ˆλ‹€.

λ²”μœ„ κΈ°λŠ₯

range() ν•¨μˆ˜λŠ” 숫자 λͺ©λ‘μ— μ‚¬μš©λ©λ‹ˆλ‹€. λ²”μœ„(μ‹œμž‘, 끝, 단계) λŠ” μ‹œμž‘, μ’…λ£Œ 및 μ¦λΆ„μ˜ μ„Έ 가지 λ§€κ°œλ³€μˆ˜λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. 기본적으둜 0λΆ€ν„° μ‹œμž‘ν•˜κ³  증뢄은 1μž…λ‹ˆλ‹€. λ²”μœ„ μ‹œν€€μŠ€μ—λŠ” μ΅œμ†Œ 1개의 인수(μ’…λ£Œ)κ°€ ν•„μš”ν•©λ‹ˆλ‹€. λ²”μœ„λ₯Ό μ‚¬μš©ν•˜μ—¬ μ‹œν€€μŠ€ λ§Œλ“€κΈ°

lst = list(range(11))
print(lst) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
st = set(range(1, 11))    # 2 arguments indicate start and end of the sequence, step set to default 1
print(st) # {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

lst = list(range(0,11,2))
print(lst) # [0, 2, 4, 6, 8, 10]
st = set(range(0,11,2))
print(st) #  {0, 2, 4, 6, 8, 10}
# syntax
for iterator in range(start, end, step):

μ˜ˆμ‹œ:

for number in range(11):
    print(number)   # prints 0 to 10, not including 11

쀑첩 For 루프

루프 μ•ˆμ— 루프λ₯Ό μž‘μ„±ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

# syntax
for x in y:
    for t in x:
        print(t)

μ˜ˆμ‹œ:

person = {
    'first_name': 'Asabeneh',
    'last_name': 'Yetayeh',
    'age': 250,
    'country': 'Finland',
    'is_marred': True,
    'skills': ['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
    'address': {
        'street': 'Space street',
        'zipcode': '02210'
    }
}
for key in person:
    if key == 'skills':
        for skill in person['skills']:
            print(skill)

For Else

루프가 끝날 λ•Œ λ©”μ‹œμ§€λ₯Ό μ‹€ν–‰ν•˜λ €λ©΄ elseλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

# syntax
for iterator in range(start, end, step):
    do something
else:
    print('The loop ended')

μ˜ˆμ‹œ:

for number in range(11):
    print(number)   # prints 0 to 10, not including 11
else:
    print('The loop stops at', number)

Pass

Pythonμ—μ„œ when 문이 ν•„μš”ν•˜μ§€λ§Œ(μ„Έλ―Έμ½œλ‘  뒀에) μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λŠ” 것을 μ’‹μ•„ν•˜μ§€ μ•ŠμœΌλ―€λ‘œ 였λ₯˜λ₯Ό ν”Όν•˜κΈ° μœ„ν•΄ pass λΌλŠ” 단어λ₯Ό μ“Έ 수 μžˆμŠ΅λ‹ˆλ‹€. λ˜ν•œ ν–₯ν›„ μ§„μˆ μ„ μœ„ν•΄ 자리 ν‘œμ‹œμžλ‘œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

μ˜ˆμ‹œ:

for number in range(6):
    pass

πŸŒ• 당신은 큰 μ΄μ •ν‘œλ₯Ό μ„Έμ› κ³ , 당신은 멈좜 수 μ—†μŠ΅λ‹ˆλ‹€. κ³„μ†ν•˜μ„Έμš”! 10일차 μ±Œλ¦°μ§€λ₯Ό 방금 μ™„λ£Œν–ˆμœΌλ©° μœ„λŒ€ν•¨μ„ ν–₯ν•œ 10단계λ₯Ό μ•žλ‘κ³  μžˆμŠ΅λ‹ˆλ‹€. 이제 λ‡Œμ™€ κ·Όμœ‘μ„ μœ„ν•œ λͺ‡ 가지 μš΄λ™μ„ ν•˜μ‹­μ‹œμ˜€.

πŸ’» Exercises: Day 10

Exercises: Level 1

  1. for 루프λ₯Ό μ‚¬μš©ν•˜μ—¬ 0μ—μ„œ 10κΉŒμ§€ λ°˜λ³΅ν•˜κ³  while 루프λ₯Ό μ‚¬μš©ν•˜μ—¬ λ™μΌν•œ μž‘μ—…μ„ μˆ˜ν–‰ν•©λ‹ˆλ‹€.

  2. for 루프λ₯Ό μ‚¬μš©ν•˜μ—¬ 10μ—μ„œ 0κΉŒμ§€ λ°˜λ³΅ν•˜κ³  while 루프λ₯Ό μ‚¬μš©ν•˜μ—¬ λ™μΌν•œ μž‘μ—…μ„ μˆ˜ν–‰ν•©λ‹ˆλ‹€.

  3. print()λ₯Ό 7번 ν˜ΈμΆœν•˜λŠ” 루프λ₯Ό μž‘μ„±ν•˜μ—¬ λ‹€μŒ μ‚Όκ°ν˜•μ„ 좜λ ₯ν•©λ‹ˆλ‹€.

      #
      ##
      ###
      ####
      #####
      ######
      #######
    
  4. 쀑첩 루프λ₯Ό μ‚¬μš©ν•˜μ—¬ λ‹€μŒμ„ λ§Œλ“­λ‹ˆλ‹€.

    # # # # # # # #
    # # # # # # # #
    # # # # # # # #
    # # # # # # # #
    # # # # # # # #
    # # # # # # # #
    # # # # # # # #
    # # # # # # # #
    
  5. λ‹€μŒ νŒ¨ν„΄μ„ μΈμ‡„ν•©λ‹ˆλ‹€.

    0 x 0 = 0
    1 x 1 = 1
    2 x 2 = 4
    3 x 3 = 9
    4 x 4 = 16
    5 x 5 = 25
    6 x 6 = 36
    7 x 7 = 49
    8 x 8 = 64
    9 x 9 = 81
    10 x 10 = 100
    
  6. for 루프λ₯Ό μ‚¬μš©ν•˜μ—¬ ['Python', 'Numpy','Pandas','Django', 'Flask'] λͺ©λ‘μ„ λ°˜λ³΅ν•˜κ³  ν•­λͺ©μ„ 좜λ ₯ν•©λ‹ˆλ‹€.

  7. for 루프λ₯Ό μ‚¬μš©ν•˜μ—¬ 0μ—μ„œ 100κΉŒμ§€ λ°˜λ³΅ν•˜κ³  짝수만 좜λ ₯

  8. for 루프λ₯Ό μ‚¬μš©ν•˜μ—¬ 0μ—μ„œ 100κΉŒμ§€ λ°˜λ³΅ν•˜κ³  ν™€μˆ˜λ§Œ 좜λ ₯

Exercises: Level 2

  1. for 루프λ₯Ό μ‚¬μš©ν•˜μ—¬ 0μ—μ„œ 100κΉŒμ§€ λ°˜λ³΅ν•˜κ³  λͺ¨λ“  숫자의 합계λ₯Ό μΈμ‡„ν•©λ‹ˆλ‹€.
The sum of all numbers is 5050.
  1. for 루프λ₯Ό μ‚¬μš©ν•˜μ—¬ 0μ—μ„œ 100κΉŒμ§€ λ°˜λ³΅ν•˜κ³  λͺ¨λ“  짝수의 ν•©κ³Ό λͺ¨λ“  μŠΉμ‚°μ˜ 합을 μΈμ‡„ν•©λ‹ˆλ‹€.

    The sum of all evens is 2550. And the sum of all odds is 2500.
    

Exercises: Level 3

  1. 데이터 ν΄λ”λ‘œ μ΄λ™ν•˜μ—¬ countries.py νŒŒμΌμ„ μ‚¬μš©ν•©λ‹ˆλ‹€. κ΅­κ°€λ₯Ό μˆœν™˜ν•˜κ³  단어 land λ₯Ό ν¬ν•¨ν•˜λŠ” λͺ¨λ“  κ΅­κ°€λ₯Ό μΆ”μΆœν•©λ‹ˆλ‹€.
  2. 이것은 과일 λͺ©λ‘μž…λ‹ˆλ‹€. ['banana', 'orange', 'mango', 'lemon'] 루프λ₯Ό μ‚¬μš©ν•˜μ—¬ μˆœμ„œλ₯Ό λ’€μ§‘μŠ΅λ‹ˆλ‹€.
  3. 데이터 ν΄λ”λ‘œ μ΄λ™ν•˜μ—¬ countries_data.py νŒŒμΌμ„ μ‚¬μš©ν•©λ‹ˆλ‹€.
    1. λ°μ΄ν„°μ˜ 총 μ–Έμ–΄ μˆ˜λŠ” μ–Όλ§ˆμž…λ‹ˆκΉŒ?
    2. λ°μ΄ν„°μ—μ„œ κ°€μž₯ 많이 μ‚¬μš©λ˜λŠ” 10개 μ–Έμ–΄ μ°ΎκΈ°
    3. μ„Έκ³„μ—μ„œ 인ꡬ가 κ°€μž₯ λ§Žμ€ 10개 κ΅­κ°€ μ°ΎκΈ°

πŸŽ‰ μΆ•ν•˜ν•©λ‹ˆλ‹€! πŸŽ‰

<< Day 9 | Day 11 >>