python에서 함수를 정의하기 위해서는 def 키워드를 사용한다.
# test.py
def hello_world():
print("welcome to python world!!")
hello_world()
# test.py
def calcualte(a, b):
return a+b, a*b
sum, mul = calcualte(2, 3)
print("sum={0}, mul={1}".format(sum, mul))
또 다른 예제를 살펴보도록 하자
# test.py
def calcualte(msg, x = 1, *nums, **key_nums):
print('msg = ', msg)
print('x = ',x)
print('nums = ', nums)
print('key_nums = ', key_nums)
total = x
for n in nums:
total += n
for key, value in key_nums.items():
print("key {0}, value {1}".format(key, value))
total += value
return msg + " = " + str(total)
ret = calcualte("add", 3, 4, 5, 6, a = 1, b = 2, c = 3)
print( ret )
[파이썬(Python)] #7. string (0) | 2021.08.17 |
---|---|
[파이썬(Python)] #5. 파일 입/출력 (0) | 2021.08.17 |
[파이썬(Python)] #3. os 모듈 (0) | 2021.08.13 |
[파이썬(Python)] #2. argparse 사용하기 (0) | 2021.08.12 |
[파이썬(Python)] #1. python3 설치 (0) | 2021.08.11 |