next up previous contents
Next: ÆÄÀÏ ÀÔÃâ·Â Up: ÀϹÝÀÎÀ» À§ÇÑ ÆÄÀ̽ã Áöħ¼­ Previous: ¸®½ºÆ®¸¦ ´õ ÀÚ¼¼È÷   ¸ñ Â÷

ÇϺΠ¸ñ·Ï

¹®ÀÚ¿­ÀÇ º¹¼ö

ÀÌÁ¦ ¹®ÀÚ¿­·Î ÇÒ¼ö ÀÖ´Â ±â°¡ ¸·Èù ²Ä¼öÇϳª¸¦ º¸ÀÌ°Ú´Ù.:

def shout(string):
    for character in string:
        print "Gimme a "+character
        print "'"+character+"'"

shout("Lose")

def middle(string):
    print "The middle character is:",string[len(string)/2]

middle("abcdefg")
middle("The Python Programming Language")
middle("Atlanta")

±× Ãâ·ÂÀº ´ÙÀ½°ú °°´Ù:

Gimme a L
'L'
Gimme a o
'o'
Gimme a s
's'
Gimme a e
'e'
The middle character is: d
The middle character is: r
The middle character is: a
ÀÌ ÇÁ·Î±×·¥ÀÌ º¸¿©ÁÖ´Â °ÍÀº ¹®ÀÚ¿­ÀÌ ¿©·¯ ¸éÀ¸·Î ¸®½ºÆ®¿Í ºñ½ÁÇÏ´Ù´Â °ÍÀÌ´Ù. shout ÇÁ·Î½ÃÁ®´Â forȸµ¹ÀÌ°¡ ¸®½ºÆ®¿¡ »ç¿ëµÉ ¼ö ÀÖ´Â °Í°ú ¸¶Âù°¡Áö·Î ¹®ÀÚ¿­¿¡ »ç¿ëµÉ ¼ö ÀÖ´Ù´Â °ÍÀ» º¸¿©ÁØ´Ù. middle ÇÁ·Î½ÃÁ®´Â ±× ¹®ÀÚ¿­ÀÌ lenÇÔ¼ö ±×¸®°í ¹è¿­ ÁöÇ¥ ±×¸®°í ½ä±â ¿ª½Ã »ç¿ëÇÒ ¼ö ÀÖÀ½À» º¸¿©ÁØ´Ù. ´ëºÎºÐÀÇ ¸®½ºÆ® »ç¾çÀº ¹®ÀÚ¿­¿¡µµ ¶ÇÇÑ ÀÛµ¿ÇÑ´Ù.

´ÙÀ½ÀÇ »ç¾çÀº ¹®ÀÚ¿­ÀÇ ¾î¶² Ưº°ÇÑ »ç¾çÀ» º¸¿©ÁØ´Ù:

def to_upper(string):
    ## Converts a string to upper case
    upper_case = ""
    for character in string:
        if 'a' <= character <= 'z':
            location = ord(character) - ord('a')
            new_ascii = location + ord('A')
            character = chr(new_ascii)
        upper_case = upper_case + character
    return upper_case

print to_upper("This is Text")
±× Ãâ·ÂÀ» ¿©±â¿¡ º¸À̸é:
THIS IS TEXT
ÄÄÇ»ÅÍ°¡ ¹®ÀÚ¿­ÀÇ ¹®ÀÚ¸¦ 0¿¡¼­ 255±îÁöÀÇ ¼ýÀÚ·Î ³ªÅ¸³»¹Ç·Î ÀÌ°ÍÀº Àß ÀÛµ¿ÇÑ´Ù. ÆÄÀ̽ãÀº ord( ordinalÀÇ ¾àÀÚ)¶ó ºÎ¸£´Â ÇÔ¼ö¸¦ °¡Áö°í À־ ¹®ÀÚ¸¦ ¼ýÀÚ·Î ¹ÝȯÇØ ÁØ´Ù. ¶ÇÇÑ ´ëÀÀµÇ´Â chr À̶ó ºÎ¸£´Â ÇÔ¼öµµ À־ ¼ýÀÚ¸¦ ¹®ÀÚ·Î ¹ÝȯÇØÁØ´Ù. ÀÌ°ÍÀ» ¿°µÎ¿¡ µÎ°í¼­ ÇÁ·Î±×·¥Àº ±ú²ýÇØÁö±â ½ÃÀÛÇؾ߸¸ ÇÑ´Ù. ù ¹ø° ¼¼ºÎ»çÇ×Àº ÀÌ ¶óÀÎÀÌ´Ù: 'if 'a' <= character <= 'z':' ÀÌ ¶óÀÎÀº ¹®ÀÚ°¡ ¼Ò¹®ÀÚÀΰ¡ Á¡°ËÇÑ´Ù. ¸¸¾à ±×·¸´Ù¸é ´ÙÀ½ÀÇ ¶óÀÎÀÌ »ç¿ëµÈ´Ù. ¸ÕÀú ±×°ÍÀº 'location = ord(character) - ord('a')' ¶óÀÎÀ» »ç¿ëÇÏ¿© À§Ä¡·Î º¯È¯µÇ¾î a=0,b=1,c=2 µîµîÀ¸·Î º¯È¯µÈ´Ù. ´ÙÀ½À¸·Î ±× »õ·Î¿î °ªÀº 'new_ascii = location + ord('A')'¿¡¼­ ¹ß°ßµÈ´Ù. ÀÌ °ªÀº ´Ù½Ã ¹®ÀÚ·Î º¯È¯µÇ¾î¼­ ÀÌÁ¦´Â ´ë¹®ÀÚÀÌ´Ù.

ÀÌÁ¦ ´õ ªÀº ŸÀÌÇÎ ¿¬½ÀÀ» À§ÇØ :

print "Integer to String"
print repr(2)
print repr(23445)
print repr(-23445)
print "String to Integer"
print int("14234")
print int("12345")
print int("-3512")
print "Float to String"
print repr(234.423)
print repr(62.562)
print repr(-134.5660)
print "Float to Integer"
print int(51.523)
print int(224.63)
print int(-1234.562)

Ä£¼÷ÇÏ°Ô º¸ÀÌ´Â Ãâ·ÂÀº ´ÙÀ½°ú °°´Ù:

Integer to String
2
23445
-23445
String to Integer
14234
12345
-3512
Float to String
234.423
62.562
-134.566
Float to Integer
51
224
-1234

¿©·¯ºÐÀÌ ¾ÆÁ÷±îÁö ÀÌÇØ°¡ °¡Áö ¾Ê´Â´Ù¸é (¿ªÁÖ: °¡¸£ÃÄ ÁÖ¸é) repr ÇÔ¼ö°¡ Á¤¼ö¸¦ ¹®ÀÚ¿­·Î, intÇÔ¼ö°¡ ¹®ÀÚ¿­À» Á¤¼ö·Î º¯È¯ÇÒ¼ö ÀÖ´Ù. repr ÇÔ¼ö´Â ¾î¶² °ÍÀÇ Ãâ·Â°¡´ÉÇÑ ÇüŸ¦ µ¹·ÁÁØ´Ù. ¿©±â¿¡ ÀÌ·¯ÇÑ ¾à°£ÀÇ ¿¹°¡ ÀÖ´Ù:

>>> repr(1)
'1'
>>> repr(234.14)
'234.14'
>>> repr([4,42,10])
'[4, 42, 10]'
int ÇÔ¼ö´Â ¹®ÀÚ¿­(ȤÀº ¼Ò¼öÇü)À» Á¤¼ö·Î º¯È¯ÇÑ´Ù. float ¶ó°í ºÒ¸®´Â ºñ½ÁÇÑ ÇÔ¼ö ¶ÇÇÑ À־ Á¤¼ö³ª ¹®ÀÚ¿­À» ¼Ò¼öÇüÀ¸·Î ¹Ù²Ù¾î ÁØ´Ù. ÆÄÀ̽ãÀÌ °¡Áö°í ÀÖ´Â ¶Ç ´Ù¸¥ ÇÔ¼ö´Â eval ÇÔ¼öÀÌ´Ù. eval ÇÔ¼ö´Â ¹®ÀÚ¿­À» ÃëÇØ ÆÄÀ̽ãÀÌ ÆÇ´ÜÇÏ´Â ÇüÅ·Πµ¥ÀÌŸ¸¦ ¹ÝȯÇÑ´Ù. ¿¹¸¦ µé¾î:
>>> v=eval('123')
>>> print v,type(v)
123 <type 'int'>  <- Á¤¼öÇü
>>> v=eval('645.123')
>>> print v,type(v)
645.123 <type 'float'>  <- ¼Ò¼öÇü
>>> v=eval('[1,2,3]')
>>> print v,type(v)
[1, 2, 3] <type 'list'> <- ¸®½ºÆ®Çü
¿©·¯ºÐÀÌ eval ÇÔ¼ö¸¦ »ç¿ëÇÑ´Ù¸é ±× ÇÔ¼ö°¡ ¿©·¯ºÐÀÌ ¿¹»óÇÑ ÇüŸ¦ ¹ÝȯÇÏ´ÂÁö Á¡°ËÇØ¾ß ÇÑ´Ù.

ÇϳªÀÇ À¯¿ëÇÑ ¹®ÀÚ¿­ ÇÔ¼ö´Â split ÇÔ¼öÀÌ´Ù. ¿©±â¿¡ ¿¹°¡ ÀÖ´Ù:

>>> import string
>>> string.split("This is a bunch of words")
['This', 'is', 'a', 'bunch', 'of', 'words']
>>> string.split("First batch, second batch, third, fourth",",")
['First batch', ' second batch', ' third', ' fourth']
splitÀÌ ¹®ÀÚ¿­À» ¹®ÀÚ¿­ÀÇ ¸®½ºÆ®·Î º¯È¯ÇÏ´Â °ÍÀ» ÁÖ¸ñÇ϶ó. ¹®ÀÚ¿­Àº ±âº»°ªÀ¸·Î °ø¹éÀ¸·Î ºÐ¸®µÇ°Å³ª ȤÀº ¼±ÅÃÀûÀ¸·Î µÎ ¹ø° Àμö¿¡ ÀÇÇؼ­ ºÐ¸®µÈ´Ù (ÀÌ°æ¿ì¿¡´Â ÄÞ¸¶).

¿¹ Á¦

#This program requires a excellent understanding of decimal numbers
def to_string(in_int):
    "Converts an integer to a string"
    out_str = ""
    prefix = ""
    if in_int < 0:
        prefix = "-"
        in_int = -in_int
    while in_int / 10 != 0:
        out_str = chr(ord('0')+in_int % 10) + out_str
        in_int = in_int / 10
    out_str = chr(ord('0')+in_int % 10) + out_str
    return prefix + out_str

def to_int(in_str):
    "Converts a string to an integer"
    out_num = 0
    if in_str[0] == "-":
        multiplier = -1
        in_str = in_str[1:]
    else:
        multiplier = 1
    for x in range(0,len(in_str)):
        out_num = out_num * 10 + ord(in_str[x]) - ord('0')
    return out_num * multiplier

print to_string(2)
print to_string(23445)
print to_string(-23445)
print to_int("14234")
print to_int("12345")
print to_int("-3512")
±× Ãâ·ÂÀº ´ÙÀ½°ú °°´Ù:
2
23445
-23445
14234
12345
-3512


next up previous contents
Next: ÆÄÀÏ ÀÔÃâ·Â Up: ÀϹÝÀÎÀ» À§ÇÑ ÆÄÀ̽ã Áöħ¼­ Previous: ¸®½ºÆ®¸¦ ´õ ÀÚ¼¼È÷   ¸ñ Â÷
Josh Cogliati jjc@iname.com