생능출판사 (가칭)"데이터과학 파이썬" 코드 5장

5.1 왜 반복이 중요한가

In [1]:
print("ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!") 
print("ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!") 
print("ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!") 
print("ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!") 
print("ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!")
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
In [2]:
for i in range(100): 
    print("ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!") 
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!

5.3 Ƚ¼ö¸¦ Á¤Çسõ°í ¹Ýº¹½ÃÅ°ÀÚ

In [3]:
for i in [1, 2, 3, 4, 5]:    # ³¡¿¡ :ÀÌ ÀÖ¾î¾ß ÇÔ
    print("¹æ¹®À» ȯ¿µÇÕ´Ï´Ù.")   # µé¿©¾²±âÇÏ¿©¾ß ÇÔ
¹æ¹®À» ȯ¿µÇÕ´Ï´Ù.
¹æ¹®À» ȯ¿µÇÕ´Ï´Ù.
¹æ¹®À» ȯ¿µÇÕ´Ï´Ù.
¹æ¹®À» ȯ¿µÇÕ´Ï´Ù.
¹æ¹®À» ȯ¿µÇÕ´Ï´Ù.

5.4 for - in ´ÙÀ½¿¡´Â ¸®½ºÆ®³ª ¹®ÀÚ¿­µµ ¿Ã ¼ö ÀÖ´Ù

In [4]:
for i in [1, 2, 3, 4, 5]:
    print("i=", i)
i= 1
i= 2
i= 3
i= 4
i= 5
In [5]:
for i in "Hello":      # ³¡¿¡ :ÀÌ ÀÖ¾î¾ß ÇÔ 
    print("i =", i)    # i °ªÀ» Ãâ·ÂÇغ¸ÀÚ
i = H
i = e
i = l
i = l
i = o
In [ ]:
 
In [6]:
for i in [1, 2, 3, 4, 5]:
    print("9*", i, "=", 9*i)
9* 1 = 9
9* 2 = 18
9* 3 = 27
9* 4 = 36
9* 5 = 45

5.5 for ¹®°úÀÇ Âû¶± ±ÃÇÕÀÎ range() ÇÔ¼ö

In [7]:
for i in range(5):            # (1)
    print("¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!")   # (2)
¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!
In [8]:
list(range(10))
Out[8]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

5.6 range() ÇÔ¼ö´Â ¼ýÀÚ¸¦ »ý»êÇÏ´Â °øÀåÀÌ´Ù

In [9]:
for i in range(1, 6, 1):
    print(i, end=" ")
1 2 3 4 5 
In [10]:
for i in range(10, 0, -1):
    print(i, end=" ")
10 9 8 7 6 5 4 3 2 1 

LAB5-1 ÅÍƲ ±×·¡ÇÈÀ¸·Î ¿©·¯°³ÀÇ ¿øÀ» ±×·Á º¸ÀÚ

In [11]:
import turtle
t = turtle.Turtle()

t.circle(100)       # ¹ÝÁö¸§ÀÌ 100ÀÎ ¿øÀ» ±×¸°´Ù. 
t.left(60)          # 60µµ ¸¸Å­ ÅÍƲÀ» ¿ÞÂÊÀ¸·Î ȸÀü½ÃŲ´Ù. 
t.circle(100) 
t.left(60)    
t.circle(100) 
t.left(60)    
t.circle(100) 
t.left(60)    
t.circle(100) 
t.left(60)    
t.circle(100) 

turtle.done()
try:
    turtle.bye()
except:
    print("bye") 
bye
In [12]:
import turtle
t = turtle.Turtle()

for count in range(6):
    t.circle(100)
    t.left(360/6)
    
turtle.done()
try:
    turtle.bye()
except:
    print("bye")     
bye

LAB5-2 ¹Ýº¹À» »ç¿ëÇÏ¿© µµÇüÀ» ±×¸®ÀÚ

In [13]:
import turtle 
t = turtle.Turtle() 
t.shape("turtle") 
 
# Á¤»ï°¢Çü ±×¸®±â 
for i in range(3): 
    t.forward(100) 
    t.left(360/3)   # 360/3À» ÅëÇØ 120µµ ¿ÞÂÊÀ¸·Î Ʋ±â
 
# Á¤»ç°¢ÇüÀ» ±×¸®±â À§ÇÏ¿© ÅÍƲÀ» À̵¿Çϱâ 
t.penup()       # Ææ ÀÚ±¹ÀÌ ³²Áö ¾Êµµ·Ï ÆæÀ» µé¾î¼­ À̵¿ÇÑ´Ù
t.goto(200, 0) 
t.pendown()     # À̵¿À» ¸¶Ä¡¸é ÆæÀº ³»¸®µµ·Ï ÇÑ´Ù
 
# Á¤»ç°¢Çü ±×¸®±â 
for i in range(4): 
    t.forward(100) 
    t.left(360/4)   # 360/4¸¦ ÅëÇØ 90µµ ¿ÞÂÊÀ¸·Î Ʋ±â
    
turtle.done()
try:
    turtle.bye()
except:
    print("bye")     
bye

LAB5-3 N-°¢Çü ±×¸®±â

In [14]:
import turtle
t = turtle.Turtle()
t.shape("turtle")

s = turtle.textinput("", "¸î°¢ÇüÀ» ¿øÇϽóª¿ä?:")
n=int(s)

for i in range(n):
    t.forward(100)
    t.left(360/n)
    
turtle.done()
try:
    turtle.bye()
except:
    print("bye")     
bye

LAB5-4 ¼ú¿¡ ÃëÇÑ °ÅºÏÀ̸¦ ±×¸®´Â ·£´ýÇÔ¼ö

In [15]:
import random 
number = random.randint(1, 100)
In [16]:
import turtle
import random
t = turtle.Turtle()
t.shape("turtle")
 
for i in range(30):
    length = random.randint(1, 100)
    t.forward(length)
    angle = random.randint(-180, 180)
    t.right(angle) 
    
turtle.done()
try:
    turtle.bye()
except:
    print("bye")     
bye

LAB5-5 ¹Ýº¹À» ÀÌ¿ëÇÏ¿© ÆÑÅ丮¾óÀ» °è»êÇϱâ

In [17]:
n = int(input("Á¤¼ö¸¦ ÀÔ·ÂÇϽÿÀ: "))
fact = 1
for i in range(1, n+1):
    fact = fact * i
print(n, "!Àº", fact, "ÀÌ´Ù.")
Á¤¼ö¸¦ ÀÔ·ÂÇϽÿÀ: 8
8 !Àº 40320 ÀÌ´Ù.

5.7 Á¶°Ç¿¡ µû¶ó ¹Ýº¹Çؼ­ ½ÇÇàÇÏ´Â while ¹®

In [18]:
under_construction = True 
while under_construction:
    response = input("°ø»ç°¡ ¿Ï·áµÇ¾ú½À´Ï±î?"); 
    if response == "¿¹" :
        under_construction = False

print("·çÇÁ¸¦ Å»ÃâÇß½À´Ï´Ù.")  
°ø»ç°¡ ¿Ï·áµÇ¾ú½À´Ï±î?¿¹
·çÇÁ¸¦ Å»ÃâÇß½À´Ï´Ù.

LAB5-6 »ç¿ëÀڷκÎÅÍ ¾ÏÈ£¸¦ ¹Þ¾Æ ·Î±×ÀÎÇϱâ

In [19]:
password = "" 
while password != "pythonisfun": 
    password = input("¾ÏÈ£¸¦ ÀÔ·ÂÇϽÿÀ: ") 
print("** ·Î±×ÀÎ ¼º°ø **") 
¾ÏÈ£¸¦ ÀÔ·ÂÇϽÿÀ: pythonisfun
** ·Î±×ÀÎ ¼º°ø **

5.8 ÀÏÁ¤ÇÑ È½¼ö ¹Ýº¹¿¡ while »ç¿ëÇϱâ

In [20]:
count = 1
sum = 0
while count <= 10 :
    sum = sum + count
    count = count + 1
print("ÇÕ°è´Â", sum)
ÇÕ°è´Â 55
In [21]:
import turtle
t = turtle.Turtle()

i = 0
while i < 4:
    t.forward(100)
    t.right(90)
    i = i + 1
    
turtle.done()
try:
    turtle.bye()
except:
    print("bye")     
bye

LAB5-7 ÀԷ¹ÞÀº ¼ö¸¦ »ç¿ëÇÏ´Â ±¸±¸´Ü Ãâ·Â

In [22]:
dan = int(input("¿øÇÏ´Â ´ÜÀº:  "))
i = 1

while i <= 9:
    print("%s*%s=%s" % (dan, i, dan*i))
    i = i + 1
¿øÇÏ´Â ´ÜÀº:  7
7*1=7
7*2=14
7*3=21
7*4=28
7*5=35
7*6=42
7*7=49
7*8=56
7*9=63

LAB5-8 while ¹®À¸·Î º° ±×¸®±â¸¦ Çغ¸ÀÚ

In [23]:
import turtle
t = turtle.Turtle()
t.shape("turtle")
i = 0
while i < 5:
    t.forward(50)
    t.right(144)
    i = i + 1
    
turtle.done()
try:
    turtle.bye()
except:
    print("bye")     
bye

LAB5-9 °£´ÜÇÑ ÄÚµå·Î ¸ÚÁø ³ª¼±Çü µµÇü ±×·Áº¸±â

In [24]:
import turtle

t = turtle.Turtle()
# °ÅºÏÀÌÀÇ ¼Óµµ´Â 0À¸·Î ¼³Á¤Çϸé ÃÖ´ë°¡ µÈ´Ù. 
t.speed(0)
t.width(3)

length = 10	  # Ãʱ⠼±ÀÇ ±æÀÌ´Â 10À¸·Î ÇÑ´Ù. 
# while ¹Ýº¹¹®ÀÌ´Ù. ¼±ÀÇ ±æÀÌ°¡ 500º¸´Ù ÀÛÀ¸¸é ¹Ýº¹ÇÑ´Ù.  
while length < 500:	
    t.forward(length)             # length¸¸Å­ ÀüÁøÇÑ´Ù. 
    t.right(89)                  # 89µµ ¿À¸¥ÂÊÀ¸·Î ȸÀüÇÑ´Ù. 
    length += 5			   # ¼±ÀÇ ±æÀ̸¦ 5¸¸Å­ Áõ°¡½ÃŲ´Ù.
    
turtle.done()
try:
    turtle.bye()
except:
    print("bye")     
bye

LAB5-9 °£´ÜÇÑ ÄÚµå·Î ¸ÚÁø ³ª¼±Çü µµÇü ±×·Áº¸±â

In [39]:
total = 0 
answer = 'yes' 
while answer == 'yes': 
    number = int(input('¼ýÀÚ¸¦ ÀÔ·ÂÇϽÿÀ: ')) 
    total = total + number 
    answer = input('°è¼Ó?(yes/no): ') 

print('ÇÕ°è´Â : ', total) 
¼ýÀÚ¸¦ ÀÔ·ÂÇϽÿÀ: 10
°è¼Ó?(yes/no): yes
¼ýÀÚ¸¦ ÀÔ·ÂÇϽÿÀ: 20
°è¼Ó?(yes/no): yes
¼ýÀÚ¸¦ ÀÔ·ÂÇϽÿÀ: 30
°è¼Ó?(yes/no): no
ÇÕ°è´Â :  60

LAB5-11 ¹«ÇÑ ¹Ýº¹¹®À¸·Î ¼ýÀÚ ¸ÂÃ߱⠰ÔÀÓÀ» ¸¸µéÀÚ

In [26]:
import random 
 
tries = 0 
guess = 0; 
answer = random.randint(1, 100) 
print("1ºÎÅÍ 100 »çÀÌÀÇ ¼ýÀÚ¸¦ ¸ÂÃ߽ÿÀ") 
while guess != answer: 
    guess = int(input("¼ýÀÚ¸¦ ÀÔ·ÂÇϽÿÀ: ")) 
    tries = tries + 1 
    if guess < answer: 
        print("³·À½!") 
    elif guess > answer: 
        print("³ôÀ½!") 
 
print("ÃàÇÏÇÕ´Ï´Ù. ÃÑ ½ÃµµÈ½¼ö=", tries) 
1ºÎÅÍ 100 »çÀÌÀÇ ¼ýÀÚ¸¦ ¸ÂÃ߽ÿÀ
¼ýÀÚ¸¦ ÀÔ·ÂÇϽÿÀ: 90
³ôÀ½!
¼ýÀÚ¸¦ ÀÔ·ÂÇϽÿÀ: 50
³ôÀ½!
¼ýÀÚ¸¦ ÀÔ·ÂÇϽÿÀ: 20
³ôÀ½!
¼ýÀÚ¸¦ ÀÔ·ÂÇϽÿÀ: 10
³ôÀ½!
¼ýÀÚ¸¦ ÀÔ·ÂÇϽÿÀ: 5
³·À½!
¼ýÀÚ¸¦ ÀÔ·ÂÇϽÿÀ: 8
³ôÀ½!
¼ýÀÚ¸¦ ÀÔ·ÂÇϽÿÀ: 6
ÃàÇÏÇÕ´Ï´Ù. ÃÑ ½ÃµµÈ½¼ö= 7

LAB5-12 ¾Ï»ê ¹®Á¦¸¦ ¸¸µé¾îº¸ÀÚ

In [27]:
import random 
 
i=0    
while True: 
    x = random.randint(1, 100) 
    y = random.randint(1, 100) 
    print(x, '+', y, '=', end = ' ') 
    answer = int(input()) 
    if answer == x + y: 
        print('ÀßÇß¾î¿ä!!') 
    else: 
        print('Á¤´äÀº',x+y,'ÀÔ´Ï´Ù. ´ÙÀ½¹ø¿¡´Â ÀßÇÒ ¼ö ÀÖÁÒ?') 
    i+=1
    if(i==4):
        break;
76 + 67 = 143
ÀßÇß¾î¿ä!!
7 + 25 = 30
Á¤´äÀº 32 ÀÔ´Ï´Ù. ´ÙÀ½¹ø¿¡´Â ÀßÇÒ ¼ö ÀÖÁÒ?
49 + 95 = 130
Á¤´äÀº 144 ÀÔ´Ï´Ù. ´ÙÀ½¹ø¿¡´Â ÀßÇÒ ¼ö ÀÖÁÒ?
64 + 2 = 66
ÀßÇß¾î¿ä!!

LAB5-13 â¾÷ÀÚ¸¦ À§ÇÑ ±â´É : ¸ðµç »÷µåÀ§Ä¡ Á¾·ù Ãâ·ÂÇϱâ

In [28]:
breads = ["È£¹Ð»§", "À§Æ®", "È­ÀÌÆ®" ]
meats = ["¹ÌÆ®º¼", "¼Ò½ÃÁö", "´ß°¡½¿»ì"]
vegis = ["¾ç»óÃß", "Å丶Åä", "¿ÀÀÌ"]
sauces = ["¸¶¿ä³×Áî", "Çã´Ï ¸Ó½ºÅ¸µå", "Ä¥¸®"]

print('´Þ¼ö³× »÷µåÀ§Ä¡ °¡°ÔÀÇ °¡´ÉÇÑ Á¶ÇÕ')
for b in breads:
    for m in meats:
        for v in vegis:
            for s in sauces:
                print(b+" + "+m+" + "+v+" + "+s)
´Þ¼ö³× »÷µåÀ§Ä¡ °¡°ÔÀÇ °¡´ÉÇÑ Á¶ÇÕ
È£¹Ð»§ + ¹ÌÆ®º¼ + ¾ç»óÃß + ¸¶¿ä³×Áî
È£¹Ð»§ + ¹ÌÆ®º¼ + ¾ç»óÃß + Çã´Ï ¸Ó½ºÅ¸µå
È£¹Ð»§ + ¹ÌÆ®º¼ + ¾ç»óÃß + Ä¥¸®
È£¹Ð»§ + ¹ÌÆ®º¼ + Å丶Åä + ¸¶¿ä³×Áî
È£¹Ð»§ + ¹ÌÆ®º¼ + Å丶Åä + Çã´Ï ¸Ó½ºÅ¸µå
È£¹Ð»§ + ¹ÌÆ®º¼ + Å丶Åä + Ä¥¸®
È£¹Ð»§ + ¹ÌÆ®º¼ + ¿ÀÀÌ + ¸¶¿ä³×Áî
È£¹Ð»§ + ¹ÌÆ®º¼ + ¿ÀÀÌ + Çã´Ï ¸Ó½ºÅ¸µå
È£¹Ð»§ + ¹ÌÆ®º¼ + ¿ÀÀÌ + Ä¥¸®
È£¹Ð»§ + ¼Ò½ÃÁö + ¾ç»óÃß + ¸¶¿ä³×Áî
È£¹Ð»§ + ¼Ò½ÃÁö + ¾ç»óÃß + Çã´Ï ¸Ó½ºÅ¸µå
È£¹Ð»§ + ¼Ò½ÃÁö + ¾ç»óÃß + Ä¥¸®
È£¹Ð»§ + ¼Ò½ÃÁö + Å丶Åä + ¸¶¿ä³×Áî
È£¹Ð»§ + ¼Ò½ÃÁö + Å丶Åä + Çã´Ï ¸Ó½ºÅ¸µå
È£¹Ð»§ + ¼Ò½ÃÁö + Å丶Åä + Ä¥¸®
È£¹Ð»§ + ¼Ò½ÃÁö + ¿ÀÀÌ + ¸¶¿ä³×Áî
È£¹Ð»§ + ¼Ò½ÃÁö + ¿ÀÀÌ + Çã´Ï ¸Ó½ºÅ¸µå
È£¹Ð»§ + ¼Ò½ÃÁö + ¿ÀÀÌ + Ä¥¸®
È£¹Ð»§ + ´ß°¡½¿»ì + ¾ç»óÃß + ¸¶¿ä³×Áî
È£¹Ð»§ + ´ß°¡½¿»ì + ¾ç»óÃß + Çã´Ï ¸Ó½ºÅ¸µå
È£¹Ð»§ + ´ß°¡½¿»ì + ¾ç»óÃß + Ä¥¸®
È£¹Ð»§ + ´ß°¡½¿»ì + Å丶Åä + ¸¶¿ä³×Áî
È£¹Ð»§ + ´ß°¡½¿»ì + Å丶Åä + Çã´Ï ¸Ó½ºÅ¸µå
È£¹Ð»§ + ´ß°¡½¿»ì + Å丶Åä + Ä¥¸®
È£¹Ð»§ + ´ß°¡½¿»ì + ¿ÀÀÌ + ¸¶¿ä³×Áî
È£¹Ð»§ + ´ß°¡½¿»ì + ¿ÀÀÌ + Çã´Ï ¸Ó½ºÅ¸µå
È£¹Ð»§ + ´ß°¡½¿»ì + ¿ÀÀÌ + Ä¥¸®
À§Æ® + ¹ÌÆ®º¼ + ¾ç»óÃß + ¸¶¿ä³×Áî
À§Æ® + ¹ÌÆ®º¼ + ¾ç»óÃß + Çã´Ï ¸Ó½ºÅ¸µå
À§Æ® + ¹ÌÆ®º¼ + ¾ç»óÃß + Ä¥¸®
À§Æ® + ¹ÌÆ®º¼ + Å丶Åä + ¸¶¿ä³×Áî
À§Æ® + ¹ÌÆ®º¼ + Å丶Åä + Çã´Ï ¸Ó½ºÅ¸µå
À§Æ® + ¹ÌÆ®º¼ + Å丶Åä + Ä¥¸®
À§Æ® + ¹ÌÆ®º¼ + ¿ÀÀÌ + ¸¶¿ä³×Áî
À§Æ® + ¹ÌÆ®º¼ + ¿ÀÀÌ + Çã´Ï ¸Ó½ºÅ¸µå
À§Æ® + ¹ÌÆ®º¼ + ¿ÀÀÌ + Ä¥¸®
À§Æ® + ¼Ò½ÃÁö + ¾ç»óÃß + ¸¶¿ä³×Áî
À§Æ® + ¼Ò½ÃÁö + ¾ç»óÃß + Çã´Ï ¸Ó½ºÅ¸µå
À§Æ® + ¼Ò½ÃÁö + ¾ç»óÃß + Ä¥¸®
À§Æ® + ¼Ò½ÃÁö + Å丶Åä + ¸¶¿ä³×Áî
À§Æ® + ¼Ò½ÃÁö + Å丶Åä + Çã´Ï ¸Ó½ºÅ¸µå
À§Æ® + ¼Ò½ÃÁö + Å丶Åä + Ä¥¸®
À§Æ® + ¼Ò½ÃÁö + ¿ÀÀÌ + ¸¶¿ä³×Áî
À§Æ® + ¼Ò½ÃÁö + ¿ÀÀÌ + Çã´Ï ¸Ó½ºÅ¸µå
À§Æ® + ¼Ò½ÃÁö + ¿ÀÀÌ + Ä¥¸®
À§Æ® + ´ß°¡½¿»ì + ¾ç»óÃß + ¸¶¿ä³×Áî
À§Æ® + ´ß°¡½¿»ì + ¾ç»óÃß + Çã´Ï ¸Ó½ºÅ¸µå
À§Æ® + ´ß°¡½¿»ì + ¾ç»óÃß + Ä¥¸®
À§Æ® + ´ß°¡½¿»ì + Å丶Åä + ¸¶¿ä³×Áî
À§Æ® + ´ß°¡½¿»ì + Å丶Åä + Çã´Ï ¸Ó½ºÅ¸µå
À§Æ® + ´ß°¡½¿»ì + Å丶Åä + Ä¥¸®
À§Æ® + ´ß°¡½¿»ì + ¿ÀÀÌ + ¸¶¿ä³×Áî
À§Æ® + ´ß°¡½¿»ì + ¿ÀÀÌ + Çã´Ï ¸Ó½ºÅ¸µå
À§Æ® + ´ß°¡½¿»ì + ¿ÀÀÌ + Ä¥¸®
È­ÀÌÆ® + ¹ÌÆ®º¼ + ¾ç»óÃß + ¸¶¿ä³×Áî
È­ÀÌÆ® + ¹ÌÆ®º¼ + ¾ç»óÃß + Çã´Ï ¸Ó½ºÅ¸µå
È­ÀÌÆ® + ¹ÌÆ®º¼ + ¾ç»óÃß + Ä¥¸®
È­ÀÌÆ® + ¹ÌÆ®º¼ + Å丶Åä + ¸¶¿ä³×Áî
È­ÀÌÆ® + ¹ÌÆ®º¼ + Å丶Åä + Çã´Ï ¸Ó½ºÅ¸µå
È­ÀÌÆ® + ¹ÌÆ®º¼ + Å丶Åä + Ä¥¸®
È­ÀÌÆ® + ¹ÌÆ®º¼ + ¿ÀÀÌ + ¸¶¿ä³×Áî
È­ÀÌÆ® + ¹ÌÆ®º¼ + ¿ÀÀÌ + Çã´Ï ¸Ó½ºÅ¸µå
È­ÀÌÆ® + ¹ÌÆ®º¼ + ¿ÀÀÌ + Ä¥¸®
È­ÀÌÆ® + ¼Ò½ÃÁö + ¾ç»óÃß + ¸¶¿ä³×Áî
È­ÀÌÆ® + ¼Ò½ÃÁö + ¾ç»óÃß + Çã´Ï ¸Ó½ºÅ¸µå
È­ÀÌÆ® + ¼Ò½ÃÁö + ¾ç»óÃß + Ä¥¸®
È­ÀÌÆ® + ¼Ò½ÃÁö + Å丶Åä + ¸¶¿ä³×Áî
È­ÀÌÆ® + ¼Ò½ÃÁö + Å丶Åä + Çã´Ï ¸Ó½ºÅ¸µå
È­ÀÌÆ® + ¼Ò½ÃÁö + Å丶Åä + Ä¥¸®
È­ÀÌÆ® + ¼Ò½ÃÁö + ¿ÀÀÌ + ¸¶¿ä³×Áî
È­ÀÌÆ® + ¼Ò½ÃÁö + ¿ÀÀÌ + Çã´Ï ¸Ó½ºÅ¸µå
È­ÀÌÆ® + ¼Ò½ÃÁö + ¿ÀÀÌ + Ä¥¸®
È­ÀÌÆ® + ´ß°¡½¿»ì + ¾ç»óÃß + ¸¶¿ä³×Áî
È­ÀÌÆ® + ´ß°¡½¿»ì + ¾ç»óÃß + Çã´Ï ¸Ó½ºÅ¸µå
È­ÀÌÆ® + ´ß°¡½¿»ì + ¾ç»óÃß + Ä¥¸®
È­ÀÌÆ® + ´ß°¡½¿»ì + Å丶Åä + ¸¶¿ä³×Áî
È­ÀÌÆ® + ´ß°¡½¿»ì + Å丶Åä + Çã´Ï ¸Ó½ºÅ¸µå
È­ÀÌÆ® + ´ß°¡½¿»ì + Å丶Åä + Ä¥¸®
È­ÀÌÆ® + ´ß°¡½¿»ì + ¿ÀÀÌ + ¸¶¿ä³×Áî
È­ÀÌÆ® + ´ß°¡½¿»ì + ¿ÀÀÌ + Çã´Ï ¸Ó½ºÅ¸µå
È­ÀÌÆ® + ´ß°¡½¿»ì + ¿ÀÀÌ + Ä¥¸®

5.9 ¹«ÇÑ ·çÇÁ¿Í break·Î ºüÁ®³ª°¡±â

In [29]:
while True:
   light = input('½ÅÈ£µî »ö»óÀ» ÀÔ·ÂÇϽÿÀ:')
   if light == 'green':
       break
print('ÀüÁø!!')
½ÅÈ£µî »ö»óÀ» ÀÔ·ÂÇϽÿÀ:green
ÀüÁø!!

5.10 ·çÇÁ¸¦ Á¦¾îÇÏ´Â °í±Þ ±â¹ý : continue¿Í break

In [30]:
st = 'I love Python Programming'  # Ãâ·ÂÀ» À§ÇÑ ¹®ÀÚ¿­
for ch in st: 
    if ch in ['a','e','i','o','u', 'A','E','I','O','U']: 
        continue      # ¸ðÀ½ÀÏ °æ¿ì ¾Æ·¡ Ãâ·ÂÀ» °Ç³Ê¶Ú´Ù 
    print(ch, end='')
 lv Pythn Prgrmmng

5.11 Ãâ·ÂÀ» ¿¹»Ú°Ô ¸¸µå´Â Æ÷¸ÅÆÃ

In [31]:
'{} Python!'.format('Hello')
Out[31]:
'Hello Python!'
In [32]:
 'I like {} and {}'.format('Python', 'Java')
Out[32]:
'I like Python and Java'
In [33]:
'I like {0} and {1}'.format('Python', 'Java')
Out[33]:
'I like Python and Java'
In [34]:
'I like {1} and {0}'.format('Python', 'Java')
Out[34]:
'I like Java and Python'
In [35]:
'I like {0}, {0}, {0}'.format('Python', 'Java')
Out[35]:
'I like Python, Python, Python'
In [36]:
'¼Ò¼öÁ¡ ¾Æ·¡ µÎÀÚ¸® Á¤¹Ðµµ : {0:.2f}, ¼¼ÀÚ¸® Á¤¹Ðµµ {0:.3f}'.format(1/3)
Out[36]:
'¼Ò¼öÁ¡ ¾Æ·¡ µÎÀÚ¸® Á¤¹Ðµµ : 0.33, ¼¼ÀÚ¸® Á¤¹Ðµµ 0.333'
In [37]:
'Àüü 10Ä­À» Â÷ÁöÇÏ´Â ½Ç¼ö : {0:10.3f}, {0:10.4f}'.format(3.1415926)
Out[37]:
'Àüü 10Ä­À» Â÷ÁöÇÏ´Â ½Ç¼ö :      3.142,     3.1416'
In [38]:
for i in range(2, 11, 2):
 print('{0:3d} {1:4d} {2:5d}'.format(i, i*i, i*i*i))
  2    4     8
  4   16    64
  6   36   216
  8   64   512
 10  100  1000