1. def 함수명(인자이름): <- 콜론!
# Define your spam function starting on line 5. You
# can leave the code on line 11 alone for now--we'll
# explain it soon!
def spam():
"""printing eggs! in console"""
print "Eggs!"
# Define the spam function above this line.
spam()
2. 함수 호출하기
def square(n):
"""Returns the square of a number."""
squared = n**2
print "%d squared is %d." % (n, squared)
return squared
# Call the square function on line 9! Make sure to
# include the number 10 between the parentheses.
square(10)
3. 함수 인자 사용방법
def power(base, exponent): # Add your parameters here!
result = base**exponent
print "%d to the power of %d is %d." % (base, exponent, result)
power(37,4) # Add your arguments here!
4. 함수 안에서 함수 사용하기
def one_good_turn(n):
return n + 1
def deserves_another(n):
return one_good_turn(n) + 2
5. 함수 만들기 연습
def cube(number):
return number**3
def by_three(number):
if number%3 == 0:
return cube(number)
else :
return False
'Python' 카테고리의 다른 글
Mac에서 Python 개발 환경 구축하기 (0) | 2016.02.18 |
---|---|
import (0) | 2016.02.08 |
Pig Latin (0) | 2016.02.08 |
Conditional and control flow (0) | 2016.02.05 |
datetime library (0) | 2016.02.04 |