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

ÇϺΠ¸ñ·Ï

ÇÔ¼ö¸¦ Á¤ÀÇÇϱâ

ÇÔ¼ö¸¦ »ý¼ºÇϱâ

ÀÌ ÀåÀ» ½ÃÀÛÇϱâ À§ÇÏ¿© ³ª´Â ¿©·¯ºÐ¿¡°Ô ÇÒ¼ö´Â ÀÖÀ¸³ª Çؼ­´Â ¾ÈµÇ´Â ¿¹Á¦ Çϳª¸¦ ÁÖ·Á°í ÇÑ´Ù.(±×·¯´Ï±î ŸÀÌÇÁÇØ ³ÖÁö ¸¶¼¼¿ä):
a = 23
b = -23

if a < 0:
    a = -a

if b < 0:
    b = -b

if a == b:
    print "The absolute values of", a,"and",b,"are equal"
else:
    print "The absolute values of a and b are different"
Ãâ·ÂÀ» º¸¸é:
The absolute values of 23 and 23 are equal
ÇÁ·Î±×·¥Àº ¾à°£Àº ¹Ýº¹ÀûÀ¸·Î º¸ÀδÙ. (ÇÁ·Î±×·¡¸ÓµéÀº ¹Ýº¹µÇ´Â ÀϵéÀ» ¾ÆÁÖ ½È¾îÇÑ´Ù. ±×°ÍµéÀº ÄÄÇ»ÅÍ°¡ ÇؾßÇÒ ÀÏÀÌÁö ¾ÊÀº°¡ ±×·¸Áö¿ä?) ´ÙÇེ·´°Ôµµ ÆÄÀ̽㿡¼­ ¿©·¯ºÐÀº ÇÔ¼ö¸¦ »ý¼ºÇÏ¿© ¹Ýº¹À» Á¦°Å ÇÒ ¼ö ÀÖ´Ù. ¿©±â¿¡ ´Ù½Ã ¾º¿©Áø ¿¹Á¦¸¦ º¸Àδٸé:
a = 23
b = -23

def abs(num):
    if num < 0:
        num = -num
    return num

if abs(a) == abs(b):
    print "The absolute values of", a,"and",b,"are equal"
else:
    print "The absolute values of a and b are different"
Ãâ·ÂÀ» »ìÆ캸ÀÚ:
The absolute values of 23 and -23 are equal
ÀÌ ÇÁ·Î±×·¥ÀÇ °¡Àå Áß¿äÇÑ »ç¾çÀº def ¼­¼ú¹®ÀÌ´Ù. def (defineÀÇ ¾àÀÚ)´Â ÇÔ¼öÁ¤ÀǸ¦ ½ÃÀÛÇÑ´Ù. def ´ÙÀ½¿¡´Â abs¶ó´Â ÇÔ¼öÀÇ À̸§ÀÌ ¿Â´Ù. ´ÙÀ½¿¡´Â ( ÀÌ µû¸£°í ¸Å°³º¯¼öÀÎ numÀÌ ´ÙÀ½¿¡ ¿Â´Ù. (num´Â ÇÁ·Î±×·¥¿¡¼­ È£ÃâµÇ´Â ¾î´À°÷¿¡¼­³ª ÇÔ¼ö·Î ³Ñ°ÜÁø´Ù ). :µÚÀÇ ¼­¼ú¹®Àº ±× ÇÔ¼ö°¡ »ç¿ëµÉ ¶§ ½ÇÇàµÈ´Ù. ¼­¼ú¹®Àº µé¿©¾²±âµÈ ¼­¼ú¹®ÀÌ ³¡³ª°Å³ª ȤÀº ¸®ÅϹ®À» ¸¸³µÀ»¶§±îÁö Áö¼ÓµÈ´Ù. return ¹®Àº ÇϳªÀÇ °ªÀ» ±× ÇÔ¼ö°¡ È£ÃâµÈ °÷À¸·Î ¹ÝȯÇÑ´Ù.

a ¿Í bÀÇ °ªÀÌ º¯ÇÏÁö ¾Ê¾ÒÀ½À» ÁÖ¸ñÇ϶ó. ÇÔ¼ö´Â ¹°·Ð ¹Ýȯ°ªÀ» °¡ÁöÁö ¾Ê´Â ÀÛ¾÷À» ¹Ýº¹ÇÏ´Â µ¥¿¡µµ ¾²¿©Áú ¼ö ÀÖ´Ù. ¿©±â¿¡ ¿¹¸¦ µé¸é:

def hello():
    print "Hello"

def area(width,height):
    return width*height

def print_welcome(name):
    print "Welcome",name
    
hello()
hello()

print_welcome("Fred")
w = 4
h = 5
print "width =",w,"height =",h,"area =",area(w,h)
Ãâ·ÂÀº ´ÙÀ½°ú °°´Ù:
Hello
Hello
Welcome Fred
width = 4 height = 5 area = 20
ÀÌ ¿¹Á¦´Â ¿©·¯ºÐÀÌ ÇÔ¼ö·Î Á»´õ ÇÒ¼ö ÀÖ´Â °ÍÀÌ ÀÖ´Ù´Â °ÍÀ» º¸¿©ÁØ´Ù. ¿©·¯ºÐÀº Àμö ¾øÀÌ È¤Àº Çϳª ±×ÀÌ»óÀÇ Àμö¸¦ »ç¿ëÇÒ¼ö ÀÖ´Ù´Â °ÍÀ» ÁÖ¸ñÇ϶ó. ¶ÇÇÑ ¾î¶»°Ô return¹®ÀÌ ¼±ÅÃÀûÀ¸·Î »ç¿ëµÇ´ÂÁöµµ ÁÖ¸ñÇ϶ó.

ÇÔ¼ö´Â ¹Ýº¹µÇ´Â Äڵ带 Á¦°ÅÇϴµ¥¿¡µµ ¾²ÀÏ ¼ö ÀÖ´Ù.

¿¹ Á¦

factorial.py

#defines a function that calculates the factorial

def factorial(n):
    if n <= 1:
        return 1
    return n*factorial(n-1)

print "2! = ",factorial(2)
print "3! = ",factorial(3)
print "4! = ",factorial(4)
print "5! = ",factorial(5)

Ãâ·ÂÀº :

2! =  2
3! =  6
4! =  24
5! =  120

temperature2.py

#converts temperature to fahrenheit or celsius

def print_options():
    print "Options:"
    print " 'p' print options"
    print " 'c' convert from celsius"
    print " 'f' convert from fahrenheit"
    print " 'q' quit the program"

def celsius_to_fahrenheit(c_temp):
    return 9.0/5.0*c_temp+32

def fahrenheit_to_celsius(f_temp):
    return (f_temp - 32.0)*5.0/9.0

choice = "p"
while choice != "q":
    if choice == "c":
        temp = input("Celsius temperature:")
        print "Fahrenheit:",celsius_to_fahrenheit(temp)
    elif choice == "f":
        temp = input("Fahrenheit temperature:")
        print "Celsius:",fahrenheit_to_celsius(temp)
    elif choice != "q":
        print_options()
    choice = raw_input("option:")

»ùÇÃÀ» ½ÇÇàÇϸé:

> python temperature2.py
Options:
 'p' print options
 'c' convert from celsius
 'f' convert from fahrenheit
 'q' quit the program
option:c
Celsius temperature:30 
Fahrenheit: 86.0
option:f
Fahrenheit temperature:60
Celsius: 15.5555555556
option:q


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