next up previous contents
Next: »çÀü Up: ÀϹÝÀÎÀ» À§ÇÑ ÆÄÀ̽ã Áöħ¼­ Previous: For ȸµ¹ÀÌ   ¸ñ Â÷

ºÎ¿ï Ç¥Çö½Ä

¿©±â¿¡ ºÎ¿ï Ç¥Çö½ÄÀÇ ¾à°£ÀÇ ¿¹°¡ ÀÖ´Ù. (¿©·¯ºÐÀº ŸÀÌÇÁÇØ ³ÖÁö ¸¶¼¼¿ä):
a = 6
b = 7
c = 42
print 1, a == 6
print 2, a == 7
print 3,a == 6 and b == 7
print 4,a == 7 and b == 7
print 5,not a == 7 and b == 7
print 6,a == 7 or b == 7
print 7,a == 7 or b == 6
print 8,not (a == 7 and b == 6)
print 9,not a == 7 and b == 6
Ãâ·ÂÀÌ ¿©±â¿¡ ÀÖ´Ù:
1 1
2 0
3 1
4 0
5 1
6 1
7 0
8 1
9 0
¹«½¼ÀÏÀÌ ÁøÇàµÇ´Â°¡? ÇÁ·Î±×·¥Àº ÀÏ´ÜÀÇ ¿ì½º²Î½º·¯¿ö º¸ÀÌ´Â print¼­¼ú¹®À¸·Î ±¸¼ºµÇ¾î ÀÖ´Ù. °¢°¢ÀÇprint ¼­¼ú¹®Àº ÇÑ°³ÀÇ ¼ýÀÚ¿Í ÇϳªÀÇ Ç¥Çö½ÄÀ» Ãâ·ÂÇÑ´Ù. ¼ýÀÚ´Â ³»°¡ ´Ù·ç°í ÀÖ´Â ¼­¼ú¹®ÀÇ ÃßÀûÀ» µµ¿Í ÁÖ±â À§ÇÑ °ÍÀÌ´Ù. °¢ Ç¥Çö½ÄµéÀÌ 0 ȤÀº 1·Î ³¡³ª°í ÀÖÀ½À» ÁÖ¸ñÇ϶ó. ÆÄÀ̽㿡¼­ °ÅÁþÀº 0 À¸·Î ¾º¿©Áö°í ÂüÀº 1·Î ¾º¿©Áø´Ù.
¶óÀεé :
print 1, a == 6
print 2, a == 7
ù¹ø°°¡ ÂüÀÌ°í µÎ ¹ø°°¡ °ÅÁþÀ̹ǷÎ, ¿¹»óÇÑ ¹Ù¿Í °°ÀÌ ÇÑ°³ÀÇ 1°ú ÇÑ°³ÀÇ 0À» °¢°¢ Ãâ·ÂÇÑ´Ù. ¼¼ ¹ø°ÀÇ Ãâ·Â, 'print 3,a == 6 and b == 7' Àº ¾à°£ ´Ù¸£´Ù ¿¬»êÀÚandÀÇ Àǹ̴ ¸¸¾à ¾Õ°ú µÚÀÇ ¼­¼ú¹®ÀÌ ¸ðµÎ ÂüÀ̶ó¸é Àüü Ç¥Çö½ÄÀº ÂüÀÌ°í ±×·¸Áö ¾ÊÀ¸¸é Àüü Ç¥Çö½ÄÀÌ °ÅÁþÀ̶ó´Â ¶æÀÌ´Ù. ´ÙÀ½ÀÇ ¶óÀÎ, 'print 4,a == 7 and b == 7' ¸¦ º¸¸é ¸¸¾à and Ç¥Çö½ÄÀÌ ÀϺΰ¡ °ÅÁþÀ̶ó¸é, Àüü´Â °ÅÁþÀ̶ó´Â °ÍÀ» ¾Ë¼ö ÀÖ´Ù. andÀÇ ÇàÀ§´Â ´ÙÀ½°ú °°ÀÌ ¿ä¾àµÈ´Ù:

Ç¥Çö½Ä °á°ú
true and true true
true and false false
false and won't check false

ù¹ø° Ç¥Çö½ÄÀÌ °ÅÁþÀ̶ó¸é Àüü Ç¥Çö½ÄÀÌ °ÅÁþÀ̶ó´Â °ÍÀ» ¾Ë°í ÀÖÀ¸¹Ç·Î ÆÄÀ̽ãÀº ¹Ù·Î ´ÙÀ½ÀÇ Ç¥Çö½ÄÀ» Á¡°ËÇÏÁö ¾Ê´Â´Ù´Â °ÍÀ» ÁÖ¸ñÇ϶ó.

´ÙÀ½ ¶óÀÎ, 'print 5,not a == 7 and b == 7' Àº not ¿¬»êÀÚ¸¦ »ç¿ëÇÑ´Ù. not Àº ´ÜÁö ±× Ç¥Çö½ÄÀÇ ¹Ý´ë°ªÀ» ÁØ´Ù. (±× Ç¥Çö½ÄÀº 'print 5,a != 7 and b == 7' ¿Í °°ÀÌ ´Ù½Ã ¾º¿©Áú ¼ö ÀÖ´Ù.)
¿©±â¿¡ ±× Ç¥¸¦ º¸ÀδÙ:

Ç¥Çö½Ä °á°ú
not true false
not false true

´ÙÀ½ÀÇ µÎ ¶óÀÎ, 'print 6,a == 7 or b == 7' ±×¸®°í 'print 7,a == 7 or b == 6' ´Â or ¿¬»êÀÚ¸¦ »ç¿ëÇÑ´Ù. or ¿¬»êÀڴ ù ¹ø° Ç¥Çö½ÄÀÌ ÂüÀ̰ųª, ȤÀº µÎ ¹ø° Ç¥Çö½ÄÀÌ ÂüÀ̰ųª ¾Æ´Ï¸é µÑ ´Ù ÂüÀ̶ó¸é ÂüÀ» ¹ÝȯÇÑ´Ù. ¾çÀÚÀÇ Ç¥Çö½Ä ¸ðµÎ°¡ ÂüÀÌ ¾Æ´Ï¶ó¸é °ÅÁþÀ» ¹ÝȯÇÑ´Ù. ¿©±â¿¡ ±× Ç¥°¡ ÀÖ´Ù:

Ç¥Çö½Ä °á °ú
true or won't check true
false or true true
false or false false

ù ¹ø° Ç¥Çö½ÄÀÌ ÂüÀ̶ó¸é ÆÄÀ̽ãÀº Àüü Ç¥Çö½ÄÀÌ ÂüÀ̶ó´Â °ÍÀ» ¾Ë°í ÀÖÀ¸¹Ç·Î ±× ´ÙÀ½ µÎ ¹ø°ÀÇ Ç¥Çö½ÄÀ» Á¡°ËÇÏÁö ¾Ê´Â´Ù. Àû¾îµµ Ç¥Çö½ÄÀÇ Àý¹ÝÀÌ ÂüÀ̶ó¸é or¹®Àº ÂüÀ̹ǷΠÀÌ°ÍÀº ÀÛµ¿ÇÑ´Ù. ù¹ø° ºÎºÐÀº ÂüÀÌ°í ±×¸®°í µÎ ¹ø° ºÎºÐÀº ÂüÀ̰ųª °ÅÁþÀÏ ¼ö ÀÖÁö¸¸, ±×·¯³ª Àüü Ç¥Çö½ÄÀº ¿©ÀüÈ÷ ÂüÀÌ´Ù.

´ÙÀ½ÀÇ µÎ ¶óÀÎ, 'print 8,not (a == 7 and b == 6)' ±×¸®°í 'print 9,not a == 7 and b == 6'À» º¸¸é °ýÈ£°¡ »ç¿ëµÇ¾î Ç¥Çö½ÄÀ» ¹«¸®¸¦ ÁöÀ»¼ö ÀÖÀ¸¸ç ÇÑ ºÎºÐÀ» ¸ÕÀú Æò°¡Çϵµ·Ï °­Á¦ÇÒ ¼ö ÀÖ´Ù. °ýÈ£°¡ Ç¥Çö½ÄÀ» °ÅÁþ¿¡¼­ ÂüÀ¸·Î º¯°æÇÏ¿´À½À» ÁÖ¸ñÇ϶ó. ÀÌ·¯ÇÑ ÀÏÀÌ ÀϾ °ÍÀº °ýÈ£¶§¹®¿¡ not ¿¬»êÀÚ°¡ 'a == 7'¶ó´Â ºÎºÐ´ë½Å¿¡ Àüü Ç¥Çö½Ä¿¡ Àû¿ëµÇ¾ú±â ¶§¹®ÀÌ´Ù.

¿©±â¿¡ ºÎ¿ï Ç¥Çö½ÄÀ» »ç¿ëÇÑ ¿¹Á¦°¡ Çϳª ÀÖ´Ù:

list = ["Life","The Universe","Everything","Jack","Jill","Life","Jill"]

#make a copy of the list
copy = list[:]
#sort the copy
copy.sort()
prev = copy[0]
del copy[0]

count = 0

#go through the list searching for a match
while count < len(copy) and copy[count] != prev:
    prev = copy[count]
    count = count + 1

#If a match was not found then count can't be < len
#since the while loop continues while count is < len
#and no match is found
if count < len(copy):
    print "First Match: ",prev

±×¸®°í ¿©±â¿¡ Ãâ·ÂÀÌ ÀÖ´Ù:

First Match:  Jill

ÀÌ ÇÁ·Î±×·¥Àº while count < len(copy and copy[count] ÀÎ µ¿¾È °è¼ÓÇÏ¿© ÀÏÄ¡ ¿©ºÎ¸¦ Á¡°ËÇÑ´Ù.
count°¡ copyÀÇ ¸¶Áö¸· ÁöÇ¥º¸´Ù Å©°Å³ª ÀÏÄ¡°¡ ¹ß°ßµÇ¸é and ´Â ´õ ÀÌ»ó ÂüÀÌ ¾Æ´Ï¹Ç·Î ·çÇÁ¸¦ ºüÁ®³ª¿Â´Ù. if ¹®Àº while¹®ÀÌ ÀÏÄ¡°¡ ¹ß°ßµÇ¾ú±â ¶§¹®¿¡ ºüÁ® ³ª°£°ÍÀÌ È®½ÇÇÑÁö¸¦ ´Ü¼øÈ÷ Á¡°ËÇÑ´Ù.

andÀÇ ¶Ç ´Ù¸¥ Æ®¸¯ÀÌ ÀÌ ¿¹Á¦¿¡¼­ »ç¿ëµÈ´Ù. ¿©·¯ºÐÀº and Å×À̺íÀ» º¼ ¶§ ¼¼¹ø° Ç׸ñÀÌ ``false and won't check''ÀÓÀ» ÁÖ¸ñÇ϶ó. ¸¸¾à count >= len(copy) (´Ù¸¥ ¸»·Î count < len(copy)ÀÌ °ÅÁþÀÌ) ¶ó¸é ±×·¯¸é copy[count] ÀüÇô ÂüÁ¶µÇÁö ¾Ê´Â´Ù. ÀÌ°ÍÀº ù¹ø°°¡ °ÅÁþÀÌ¸é ±×·¯¸é ±×µé µÑÀº ÂüÀϼö°¡ ¾ø´Ù´Â °ÍÀ» ÆÄÀ̽ãÀÌ ¾Ë±â ¶§¹®ÀÌ´Ù. ÀÌ°ÍÀº 'ªÀº ȸ·Î'ÀÇ Çϳª·Î ¾Ë·ÁÁ® Àִµ¥ ¹«¾ùÀΰ¡ À߸øµÇ¾î¼­ andÀÇ µÎ¹ø° ºÎºÐÀÌ ¿¡·¯¸¦ ¾ß±âÇÒ ¶§ À¯¿ëÇÏ°Ô ¾²ÀδÙ. count°¡ copy¿¡ ´ëÇÏ¿© À¯È¿ÇÑ ÁöÇ¥¿´´ÂÁö¸¦ Á¡°ËÇϱâ À§ÇÏ¿© ³ª´Â ù ¹ø° Ç¥Çö½Ä (count < len(copy)) À» »ç¿ëÇÏ¿´´Ù. (¹ÏÁö ¸øÇÏ°Ú´Ù¸é `Jill' °ú `Life' ÀÇ ÀÏÄ¡¸¦ Á¦°ÅÇÏ°í, ±×°ÍÀÌ ¿©ÀüÈ÷ Àß ÀÛµ¿Çϴ°¡¸¦ Á¡°ËÇ϶ó ±×¸®°í count < len(copy) and copy[count] != prevÀÇ ¼ø¼­¸¦ copy[count] != prev and count < len(copy)·Î µÚÁý¾îº¸¶ó.)

ºÎ¿ï Ç¥Çö½ÄÀº ¿©·¯ºÐÀÌ µÎ°³ ÀÌ»óÀÇ ´Ù¸¥ °ªµéÀ» ÇÑ ¹ø¿¡ Á¡°ËÇÏ°íÀÚ ÇÒ¶§ »ç¿ëµÉ ¼ö ÀÖ´Ù.


next up previous contents
Next: »çÀü Up: ÀϹÝÀÎÀ» À§ÇÑ ÆÄÀ̽ã Áöħ¼­ Previous: For ȸµ¹ÀÌ   ¸ñ Â÷
Josh Cogliati jjc@iname.com