next up previous contents
Next: 10±îÁö ¼¼±â Up: ÀϹÝÀÎÀ» À§ÇÑ ÆÄÀ̽ã Áöħ¼­ Previous: Hello, World   ¸ñ Â÷

ÇϺΠ¸ñ·Ï

´©°¡ °Å±â¿¡ °¡Áö?

ÀԷ°ú º¯¼ö

ÀÌÁ¦ ³ª´Â Á¤¸» º¹ÀâÇÑ ÇÁ·Î±×·¥À» ´Ù·ê ¶§¶ó°í ´À³¤´Ù. ¹Ù·Î ¿©±â¿¡ :
print "Halt!"
s = raw_input("Who Goes there? ")
print "You may pass,", s

³»°¡ ±×°ÍÀ» ½ÇÇà½ÃÄÑ ¿©±â¿¡ ³ªÀÇ È­¸éÀ» º¸Àδٸé:

Halt!
Who Goes there? Josh
You may pass, Josh

¹°·Ð ¿©·¯ºÐÀÌ ±× ÇÁ·Î±×·¥À» ½ÇÇà½Ãų ¶§ ¿©·¯ºÐÀÇ È­¸éÀº raw_input¼­¼ú¹® ¶§¹®¿¡ ´Ù¸£°Ô º¸ÀÏ °ÍÀÌ´Ù. ¿©·¯ºÐÀÌ ±× ÇÁ·Î±×·¥À» ½ÇÇà½ÃŲ´Ù¸é, ¿©·¯ºÐÀº ¾Æ¸¶µµ ¾î¶»°Ô ¿©·¯ºÐÀÇ À̸§À» ŸÀÌÇÁÇÏ°í ±×¸®°í ¿£Å͸¦ ´­·¯¾ß¸¸ Çϴ°¡¸¦ ±ú´Ý¾ÒÀ» °ÍÀÌ´Ù( ¼³¸¶ ÇÁ·Î±×·¥Àº ½ÇÇà½ÃÄ×°ÚÁö¿ä? ±×·¸Áö¿ä?). ±×·¯¸é ±× ÇÁ·Î±×·¥Àº ¾à°£ÀÇ ÅؽºÆ®¿Í ¶ÇÇÑ ¿©·¯ºÐÀÇ À̸§À» Ãâ·ÂÇØ ³¾ °ÍÀÌ´Ù. ÀÌ°ÍÀº ÀÔ·ÂÀÇ ¿¹ÀÌ´Ù. ±× ÇÁ·Î±×·¥Àº ¾î¶² ÁöÁ¡¿¡ ´Ù´Ù¸£¸é ±×¸®°í »ç¿ëÀÚ°¡ ¾î¶² ÀڷḦ ÀÔ·ÂÇØÁֱ⸦ ±â´Ù¸°´Ù. ±× ÀÚ·á´Â ³ªÁß¿¡ ÇÁ·Î±×·¥ÀÌ »ç¿ëÇÏ°Ô µÈ´Ù.

¹°·Ð, ¿ì¸®°¡ ±× Á¤º¸¸¦ µÎ¾î¾ß ÇÒ ¾î¶² °÷µµ ¾ø´Ù¸é, »ç¿ëÀڷκÎÅÍ ¾ò´Â Á¤º¸°¡ ¾µ¸ð°¡ ¾øÀ» °ÍÀÌ´Ù. ÀÌ°÷ÀÌ ¹Ù·Î º¯¼ö°¡ ÀÖ´Â °÷ÀÌ´Ù. ÀÌÀüÀÇ ÇÁ·Î±×·¥¿¡¼­ s´Â º¯¼öÀÌ´Ù. º¯¼ö´Â ¿©·¯Á¶°¢ÀÇ µ¥ÀÌŸ¸¦ ´ãÀ» ¼ö ÀÖ´Â »óÀÚ¿Í °°Àº °ÍÀÌ´Ù. ¿©±â¿¡ º¯¼öÀÇ ¿¹¸¦ º¸¿©ÁÖ´Â ÇÁ·Î±×·¥ÀÌ ÀÖ´Ù :

a = 123.4
b23 = 'Spam'
first_name = "Bill"
b = 432
c = a + b
print "a + b is", c
print "first_name is", first_name
print "Sorted Parts, After Midnight or",b23

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

a + b is 555.4
first_name is Bill
Sorted Parts, After Midnight or Spam

º¯¼ö´Â µ¥ÀÌŸ¸¦ ÀúÀåÇÑ´Ù. À§ÀÇ ÇÁ·Î±×·¥¿¡¼­ º¯¼ö´Â a, b23, first_name, b, ±×¸®°í cÀÌ´Ù. ±âº»ÀûÀÎ Çü µÎ°¡Áö´Â ¹®ÀÚ¿­°ú ¼ýÀÚÀÌ´Ù. ¹®ÀÚ¿­Àº ¹®ÀÚµé, ¼ýÀÚ ±×¸®°í ´Ù¸¥ ¹®ÀÚµéÀÇ ¿¬¼Ó¿­ÀÌ´Ù. ÀÌ ¿¹Á¦¿¡¼­ b23 ¿Í first_nameÀº ¹®ÀÚ¿­À» ´ã°íÀÖ´Â º¯¼öÀÌ´Ù. Spam, Bill, a + b is, ±×¸®°í first_name is ´Â ÀÌÇÁ·Î±×·¥¿¡¼­ ¹®ÀÚ¿­ÀÌ´Ù.¹®ÀÚ´Â " ¶Ç´Â 'À¸·Î µÑ·¯ ÃÄÁø´Ù. ´Ù¸¥ ŸÀÔÀÇ º¯¼ö´Â ¼ýÀÚÀÌ´Ù.

ÁÁ´Ù, ÀÚ ¿ì¸®´Â ÀÌ·¯ÇÑ º¯¼öµéÀ̶ó ºÎ¸£´Â »óÀÚµé°ú ¶ÇÇÑ ´Ù¸¥ º¯¼ö·Î °¥¼ö ÀÖ´Â µ¥ÀÌŸ¸¦ °¡Áö°í ÀÖ´Ù. ÄÄÇ»ÅÍ´Â first_name = "Bill"¿Í °°Àº ¶óÀÎÀ» º¸°í¼­ ±×°ÍÀ» Àб⸦ 'BillÀ̶ó´Â ½ºÆ®¸µÀ» first_name¶ó´Â »óÀÚ(ȤÀº º¯¼ö)¿¡ Áý¾î ³Ö¾î¶ó'¶ó°í ÀÐÀ» °ÍÀÌ´Ù. ±×¸®°í´Â c = a + b¶ó´Â ¼­¼ú¹®À» º¸°ÔµÇ°í Àб⸦ 'a + bȤÀº 123.4 + 432 ¶Ç´Â 555.4¸¦ c¿¡ Áý¾î ³Ö¾î¶ó'¶ó°í ÀÐÀ» °ÍÀÌ´Ù.

¿©±â¿¡ º¯¼ö »ç¿ë¹ý¿¡ ´ëÇÑ ¶Ç ´Ù¸¥ ¿¹Á¦°¡ ÀÖ´Ù:

a = 1
print a
a = a + 1
print a
a = a * 2
print a

±×¸®°í ¹°·Ð ÀÌ°ÍÀÌ ±× Ãâ·ÂÀÌ´Ù:

1
2
4

¾çÂÊ ¸ðµÎ¿¡¼­ ¶È °°Àº º¯¼öÀÓ¿¡µµ ºÒ±¸ÇÏ°í ÄÄÇ»ÅÍ´Â ¿©ÀüÈ÷ ±×°ÍÀ» ´ÙÀ½°ú °°ÀÌ Àд´٠: ¸ÕÀú ÀúÀåÇؾßÇÒ µ¥ÀÌŸ¸¦ ã¾Æ¶ó ±×¸®°í µ¥ÀÌŸ°¡ ¾îµð·Î °¡´ÂÁö¸¦ ã¾Æ¶ó

ÀÌ ÀåÀ» ¶°³ª±â Àü¿¡ ÇÁ·Î±×·¥À» Çϳª ´õ »ìÆ캸ÀÚ :

num = input("Type in a Number: ")
str = raw_input("Type in a String: ")
print "num =", num
print "num is a ",type(num)
print "num * 2 =",num*2
print "str =", str
print "str is a ",type(str)
print "str * 2 =",str*2

³»°¡ ¸ÂÀÌÇÑ Ãâ·ÂÀº ´ÙÀ½°ú °°´Ù:

Type in a Number: 12.34
Type in a String: Hello
num = 12.34
num is a  <type 'float'>
num * 2 = 24.68
str = Hello
str is a  <type 'string'>
str * 2 = HelloHello

num´Â input¹®À¸·Î ¹Ý¸é¿¡ strÀº raw_input¹®À¸·Î ÇÒ´çµÇ°í ÀÖÀ½À» ÁÖ¸ñÇ϶ó. raw_inputÀº ¹®ÀÚ¿­À» ¹ÝȯÇÏ´Â ¹Ý¸é¿¡inputÀº ¼ýÀÚ¸¦ ¹ÝȯÇÑ´Ù. »ç¿ëÀÚ°¡ ¼ýÀÚÀÇ ÇüÅ·ΠÀÔ·ÂÇϱ⸦ ¿øÇÑ´Ù¸éinputÀ» »ç¿ëÇ϶ó ±×·¯³ª »ç¿ëÀÚ°¡ ¹®ÀÚ¿­ÀÇ ÇüÅ·ΠÀÔ·ÂÇϱ⸦ ¿øÇÑ´Ù¸é raw_inputÀ» »ç¿ëÇ϶ó.

ÇÁ·Î±×·¥ÀÇ ÈĹݺο¡¼­ type¸¦ »ç¿ëÇϴµ¥ ±×°ÍÀº º¯¼öÀÇ ÇüÀ» ¸»ÇØ ÁØ´Ù. ¼ýÀÚµéÀº Á¤¼öÇüint À̰ųª ¼Ò¼öÇüfloatÀÌ´Ù (±×°ÍµéÀº °¢°¢ integer ¿Í floating pointÀÇ ¾àÀÚÀÌ´Ù). ¹®ÀÚ¿­Àº stringÀÌ´Ù. ¾ðÁ¦ ¾î¶»°Ô ÆÄÀ̽ãÀÌ ÇϳªÀÇ ¼öÄ¡¸¦ Á¤¼ö·Î °ö¼ÀÀ» ÇØ¾ß ¿¹»óÇÑ ¹Ù°¡ ÀϾ´ÂÁö¸¦ ¶ÇÇÑ ÁÖ¸ñÇ϶ó. ±×·¸Áö¸¸ ÇϳªÀÇ ¹®ÀÚ¿­ÀÌ Á¤¼ö¿¡ ÀÇÇÏ¿© °öÇÏ¿©Á³À»¶§ ±× ¹®ÀÚ¿­Àº ÀÚ½ÅÀÇ ¸¹Àº º¹»çº»ÀÌ ´õÇØÁö°Ô µÈ´Ù.
´Ù½Ã ¸»Çϸé str * 2 = HelloHello.

¿¹ Á¦

Rate_times.py

#This programs calculates rate and distance problems
print "Input a rate and a distance"
rate = input("Rate:")
distance = input("Distance:")
print "Time:",distance/rate

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

> python rate_times.py
Input a rate and a distance
Rate:5
Distance:10
Time: 2
> python rate_times.py 
Input a rate and a distance
Rate:3.52
Distance:45.6
Time: 12.9545454545

Area.py

#This program calculates the perimeter and area of a rectangle
print "Calculate information about a rectangle"
length = input("Length:")
width = input("Width:")
print "Area",length*width
print "Perimeter",2*length+2*width

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

> python area.py
Calculate information about a rectangle
Length:4
Width:3
Area 12
Perimeter 14
> python area.py
Calculate information about a rectangle
Length:2.53
Width:5.2
Area 13.156
Perimeter 15.46

temperature.py

#Converts fahrenheit to celcius
temp = input("Farenheit temperature:")
print (temp-32.0)*5.0/9.0

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

> python temperature.py
Farenheit temperature:32
0.0
> python temperature.py
Farenheit temperature:-40
-40.0
> python temperature.py
Farenheit temperature:212
100.0
> python temperature.py
Farenheit temperature:98.6
37.0


next up previous contents
Next: 10±îÁö ¼¼±â Up: ÀϹÝÀÎÀ» À§ÇÑ ÆÄÀ̽ã Áöħ¼­ Previous: Hello, World   ¸ñ Â÷
Josh Cogliati jjc@iname.com