next up previous contents
Next: ºÎ¿ï Ç¥Çö½Ä Up: ÀϹÝÀÎÀ» À§ÇÑ ÆÄÀ̽ã Áöħ¼­ Previous: ¸®½ºÆ®   ¸ñ Â÷

For ȸµ¹ÀÌ

ÀÚ ¿©±â¿¡ ÀÌ ÀåÀ» À§ÇÑ »õ·Î¿î ŸÀÌÇÎ ¿¬½ÀÀÌ ÀÖ´Ù :
onetoten = range(1,11)
for count in onetoten:
        print count
±×¸®°í ¾ðÁ¦³ª¿Í °°ÀÌ ±× Ãâ·ÂÀº :
1
2
3
4
5
6
7
8
9
10
Ãâ·ÂÀº ´ë´ÜÈ÷ Ä£¼÷ÇÏÁö¸¸ ±× ÇÁ·Î±×·¥ÀÇ ÄÚµå´Â ´Ù¸¥°Ô º¸ÀδÙ. ù ¹ø° ¶óÀÎÀº range ÇÔ¼ö¸¦ »ç¿ëÇÑ´Ù. range ÇÔ¼ö´Â 'range(start,finish)' ¿Í °°Àº Çü½ÄÀ¸·Î µÎ °³ÀÇ Àμö¸¦ »ç¿ëÇÑ´Ù. start´Â »ý¼ºµÇ´Â ù ¹ø° ¼ýÀÚÀÌ´Ù. finish ´Â °¡Àå ¸¶Áö¸· ¼ýÀÚº¸´Ù ´õ Å« ¼ýÀÚÀÌ´Ù. ÀÌ ÇÁ·Î±×·¥Àº ´õ ªÀº ¹æ½ÄÀ¸·Î ½ÇÇàµÉ¼öµµ ÀÖ¾úÀ½À» ÁÖ¸ñÇ϶ó.:
for count in range(1,11):
        print count
range ¸í·É¾î·Î ¹«½¼ÀÏÀ» ÇÏ´ÂÁö º¸¿©ÁÖ´Â ¸î°¡Áö ¿¹Á¦°¡ ÀÖ´Ù:
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(-32, -20)
[-32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21]
>>> range(5,21)
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
>>> range(21,5)
[]
´ÙÀ½ ¶óÀÎ 'for count in onetoten:'´Â for Á¦¾î ±¸Á¶¸¦ »ç¿ëÇÑ´Ù. for Á¦¾î ±¸Á¶´Â 'for variable in list:' ÇüÅÂÀÌ´Ù. list ´Â ¸®½ºÆ®ÀÇ Ã¹ ¹ø° ¿ä¼Ò·Î ºÎÅÍ ½ÃÀÛÇÏ¿© ¸¶Áö¸·±îÁö °£´Ù. for ¹®Àº ¸®½ºÆ®ÀÇ °¢ ¿ä¼Ò¸¦ µû¶ó°¡¸é¼­ ±×°ÍÀ» º¯¼övariable¿¡ °¢°¢ Áý¾î ³Ö´Â´Ù. ±×·¸°Ô Çؼ­ for ȸµ¹ÀÌ°¡ ½ÇÇàµÉ ¶§¸¶´Ù °¢°¢ variableÀÌ »ç¿ëµÉ ¼ö ÀÖ´Ù. ¿©±â¿¡ ¶Ç Çϳª º¸¿©ÁÙ ¿¹Á¦°¡ ÀÖ´Ù.( ¿©·¯ºÐÀº ŸÀÌÇÎÇÒ ÇÊ¿ä°¡ ¾ø´Ù.):
demolist = ['life',42, 'the universe', 6,'and',7,'everything']
for item in demolist:
    print "The Current item is:",
    print item
±× Ãâ·ÂÀº :
The Current item is: life
The Current item is: 42
The Current item is: the universe
The Current item is: 6
The Current item is: and
The Current item is: 7
The Current item is: everything
for ȸµ¹ÀÌ´Â ÁøÇàÇϸ鼭 ¸®½ºÆ®¿¡ ÀÖ´Â °¢ ¿ä¼Ò¿¡ 'item'À» ¼³Á¤ÇÏ°í ÀÖ´Â °ÍÀ» ÁÖ¸ñÇ϶ó. (¿©·¯ºÐÀÌ print °¡ ´ÙÀ½¶óÀÎÀ¸·Î °¡±â¸¦ ¿øÄ¡ ¾Ê´Â´Ù¸é ±× ¼­¼ú¹®ÀÇ µÚ¿¡´Ù°¡ ÄÞ¸¶¸¦ Ãß°¡ Ç϶ó (Áï, ¿©·¯ºÐÀÌ ±× ¶óÀο¡ ´Ù¸¥ ¹«¾ð°¡¸¦ Ãâ·ÂÇÏ·Á°í ÇÑ´Ù¸é).) ±×·³, for´Â ¹«¾ùÀÌ À¯ÀÍÇÑ°¡? (À½~) ù ¹ø° »ç¿ë¹ýÀº ¸®½ºÆ®ÀÇ ¸ðµç ¿ä¼ÒµéÀ» ¼øȸÇÏ°í °¢°¢¿¡ ´ëÇÏ¿© ¹«¾ð°¡¸¦ ÇÏ´Â °ÍÀÌ´Ù. ¿©±â¿¡ ¸ðµç ¿ä¼ÒµéÀ» ´õÇÏ´Â °£°áÇÑ ¹æ¹ýÀÌ ÀÖ´Ù:
l = [2,4,6,8]
sum = 0
for num in l:
        sum = sum + num
print "The sum is: ",sum
±× Ãâ·ÂÀ» °£´ÜÈ÷ º¸ÀÌ¸é ´ÙÀ½°ú °°´Ù:
The sum is:  20
¶Ç´Â ¿©·¯ºÐÀº ´ÙÀ½ ÇÁ·Î±×·¥ÀÌ ÇÏ´Â ¹Ù¿Í °°ÀÌ ÇϳªÀÇ ¸®½ºÆ®¿¡¼­ ¾î¶² Áߺ¹ÀÌ ÀÖ´ÂÁö ¾Ë¾Æº¸´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇØ º¼¼öµµ ÀÖ´Ù.:
l = [4, 5, 7, 8, 9, 1,0,7,10]
l.sort()
prev = l[0]
del l[0]
for item in l:
        if prev == item:
                print "Duplicate of ",prev," Found"
        prev = item
ÁÁÀº ¹æ¹ý? and for a good measure:
Duplicate of  7  Found
ÁÁ´Ù, ±×·³ ±×°ÍÀº ¾î¶»°Ô ÀÛµ¿Çϴ°¡? ¿©±â¿¡ ¿©·¯ºÐÀÌ ÀÌÇظ¦ µµ¿ï, Ưº°ÇÑ µð¹ö±ë ¹öÀüÀÌ ÀÖ´Ù.(¿©·¯ºÐÀº ÀÌ°ÍÀ» ŸÀÌÇÁÇØ ³ÖÀ» ÇÊ¿ä°¡ ¾ø´Ù.):
l = [4, 5, 7, 8, 9, 1,0,7,10]
print "l = [4, 5, 7, 8, 9, 1,0,7,10]","\tl:",l
l.sort()
print "l.sort()","\tl:",l
prev = l[0]
print "prev = l[0]","\tprev:",prev
del l[0]
print "del l[0]","\tl:",l
for item in l:
        if prev == item:
                print "Duplicate of ",prev," Found"
        print "if prev == item:","\tprev:",prev,"\titem:",item
        prev = item
        print "prev = item","\t\tprev:",prev,"\titem:",item
Ãâ·ÂÀ» º¸ÀÌ¸é ´ÙÀ½°ú °°´Ù:
l = [4, 5, 7, 8, 9, 1,0,7,10]   l: [4, 5, 7, 8, 9, 1, 0, 7, 10]
l.sort()        l: [0, 1, 4, 5, 7, 7, 8, 9, 10]
prev = l[0]     prev: 0
del l[0]        l: [1, 4, 5, 7, 7, 8, 9, 10]
if prev == item:        prev: 0         item: 1
prev = item             prev: 1         item: 1
if prev == item:        prev: 1         item: 4
prev = item             prev: 4         item: 4
if prev == item:        prev: 4         item: 5
prev = item             prev: 5         item: 5
if prev == item:        prev: 5         item: 7
prev = item             prev: 7         item: 7
Duplicate of  7  Found
if prev == item:        prev: 7         item: 7
prev = item             prev: 7         item: 7
if prev == item:        prev: 7         item: 8
prev = item             prev: 8         item: 8
if prev == item:        prev: 8         item: 9
prev = item             prev: 9         item: 9
if prev == item:        prev: 9         item: 10
prev = item             prev: 10        item: 10
³»°¡ ÀÌ Äڵ忡 ÀÌ·¸°Ô ¸¹Àº print¼­¼ú¹®À» ¾²´Â ÀÌÀ¯´Â ¿©·¯ºÐÀÌ °¢¶óÀο¡¼­ ¹«¾ùÀÌ ÀϾ´ÂÁö¸¦ º¼ ¼ö ÀÖ°Ô Çϱâ À§Çؼ­ÀÌ´Ù. (±×·±µ¥, ¿Ö ÇÁ·Î±×·¥ÀÌ ÀÛµ¿ÇÏÁö ¾Ê´ÂÁö ÀÌÇØ°¡ ¾È°£´Ù¸é, print ¼­¼ú¹®À» ¸¹ÀÌ »ðÀÔÇÏ¿©¼­ ¹«½¼ÀÏÀÌ ÀϾ´ÂÁö º¸µµ·Ï Ç϶ó) ¸ÕÀú ÇÁ·Î±×·¥Àº ¿¹ÀÇ Áö°ãµµ·Ï Ä£¼÷ÇÑ ¸®½ºÆ®·Î ½ÃÀÛÇÑ´Ù. ´ÙÀ½À¸·Î ÇÁ·Î±×·¥Àº ¸®½ºÆ®¸¦ Á¤·ÄÇÑ´Ù. ÀÌ°ÍÀº Áߺ¹°ªµéÀÌ ¹Ù·Î ¿·¿¡ Ãâ·ÂÇϵµ·Ï Çϱâ À§ÇÑ °ÍÀÌ´Ù. ÇÁ·Î±×·¥Àº ±×¸®°í´Â prev(ious)º¯¼ö¸¦ ÃʱâÈ­ÇÑ´Ù.
´ÙÀ½À¸·Î ¸®½ºÆ®ÀÇ Ã¹ ¹ø° ¿ä¼Ò´Â »èÁ¦µÇ¾î¼­ ù ¹ø° Ç׸ñÀÌ Áߺ¹À̶ó°í ºÎÀûÀýÇÏ°Ô »ý°¢ÇÏÁö ¾Êµµ·Ï ÇÑ´Ù. ´ÙÀ½À¸·Î forȸµ¹ÀÌ´Â ¾ÈÂÊÀ¸·Î µé¾î°£´Ù. ¸®½ºÆ®ÀÇ °¢ ¿ä¼Ò´Â ±× ÀüÀÇ °ª°ú °°ÀºÁö Á¡°ËµÈ´Ù. ¸¸¾à Áߺ¹ÀÌ ¹ß°ßµÇ¸é, prev ÀÇ °ªÀº ±×·¯¸é º¯°æµÇ¾î¼­ ´ÙÀ½¹ø¿¡ for ȸµ¹ÀÌ°¡ ½ÇÇàµÉ ¶§ prev °¡ ÇöÀçÇ׸ñ¿¡ ´ëÇÏ¿© ÀÌÀüÀÇ Ç׸ñÀÌ µÇµµ·Ï ÇÑ´Ù.
ÀÚ ÀÌÁ¦, 7ÀÌ Áߺ¹À¸·Î ¹ß°ßµÈ´Ù. ( '\t' °¡ Åǹ®ÀÚ¸¦ Ãâ·ÂÇϱâ À§ÇØ »ç¿ëµÇ¾úÀ½À» ÁÖ¸ñÇ϶ó).

for ȸµ¹À̸¦ »ç¿ëÇÏ´Â ¶Ç ´Ù¸¥ ¹æ¹ýÀº ÀÏÁ¤ÇÑ È½¼öÀÇ ÀÛ¾÷À» ÇÏ´Â °ÍÀÌ´Ù. ¿©±â¿¡ ÇǺ¸³ªÄ¡ ¼ö¿­ÀÇ ¾ÕºÎºÐ 9°³ÀÇ(ÁÖÀÇ:¿øº»¿¡´Â 11·Î µÇ¾î ÀÖÀ½) ¼ýÀÚ¸¦ Ãâ·ÂÇÏ´Â Äڵ尡 ÀÖ´Ù:

a = 1
b = 1
for c in range(1,10):
        print a,
        n = a + b
        a = b
        b = n
±× ³î¶ó¿î Ãâ·ÂÀ» ¿©±â¿¡ º¸ÀδÙ.:
1 1 2 3 5 8 13 21 34
for ȸµ¹ÀÌ·Î ÇÒ¼ö ÀÖ´Â ¸ðµç °ÍÀº while ȸµ¹ÀÌ·Î ÇÒ ¼ö´Â ÀÖÁö¸¸, for ȸµ¹ÀÌ´Â ÇϳªÀÇ ¸®½ºÆ®¿¡ ÀÖ´Â ¸ðµç ¿ä¼ÒµéÀ» ¼øȸÇϰųª, ȤÀº ÀÏÁ¤ÇÑ È½¼öÀÇ ÀÛ¾÷À» Çϱâ À§ÇÑ ½¬¿î ¹æ¹ýÀ» Á¦°øÇØ ÁØ´Ù.


next up previous contents
Next: ºÎ¿ï Ç¥Çö½Ä Up: ÀϹÝÀÎÀ» À§ÇÑ ÆÄÀ̽ã Áöħ¼­ Previous: ¸®½ºÆ®   ¸ñ Â÷
Josh Cogliati jjc@iname.com