Jupyter notebook

함수

  • 반복되는 코드를 묶음으로 효율적인 코드를 작성하도록 해주는 기능
  • 기본 함수
  • 파라미터와 아규먼트
  • 리턴
  • ‘*args’, ‘**kwargs’
  • docstring
  • scope
  • inner function
  • lambda function
  • Map, Filter, Reduce
  • Decorlator

1. 기본함수

  • 함수의 선언과 호출
1
2
3
4
5
6
7
8
point = 88

if point >= 90:
print("A")
elif point >= 80:
print("B")
elif point >= 70:
print("C")
B
1
%reset
Once deleted, variables cannot be recovered. Proceed (y/[n])? y
1
%whos
Interactive namespace is empty.
1
2
3
4
5
6
7
8
# 함수 선언
def grade(point):
if point >= 90:
print("A")
elif point >= 80:
print("B")
elif point >= 70:
print("C")
1
2
a = 1
ls = [1, 2, 3]
1
%whos
Variable   Type        Data/Info
--------------------------------
a          int         1
grade      function    <function grade at 0x0000025592D26820>
ls         list        n=3
1
2
# 함수 호출
grade(88)
B
1
# code ...
1
grade(78)
C

2. 파라미터와 아규먼트

  • 파라미터 : 함수를 선언할때 호툴하는 부분에서 보내주는 데이터를 받는 변수
  • 아규먼트 : 함수를 호출할때 함수에 보내주는 데이터
1
2
def plus(num1, num2=10, num3=20): # 파라미터 : 디폴트 파라미터
print(num1 + num2 - num3)
1
plus(1, 2) # 아규먼트
3
1
plus(3) # 아규먼트
13
1
plus(3, num3=100) # 아규먼트 : 키워드 아큐먼트
-87
1
2
print(1, 2, end="-")
print(3)
1 2-3

3. 리턴

  • 함수를 실행한 결과를 저장하고 싶을때 사용
  • return
1
2
3
def plus(num1, num2):
print(num1+num2)
return num1+num2
1
2
result = plus(1, 2)
print(result)
3
3
1
2
3
data1 = "python"
result = data1.upper()
print(result)
PYTHON
1
2
3
data2 = [3, 1, 2]
result = data2.sort()
print(result)
None
1
2
3
4
5
# 함수에서 return 코드가 샐행되면 무조건 함수의 코드 실행이 종료
def echo(msg):
if msg == "quit":
return
print(msg)
1
echo("python")
python
1
echo("quit")

4. *args, **kwargs

  • 함수를 호출할때 아규먼트와 키워드 아규먼트의 갯수를 특정지을수 없을때 사용
1
2
3
4
def plus(*args, **kwargs):
print(type(args), args)
print(type(kwargs), kwargs)
return sum(args) + sum(list(kwargs.values()))
1
plus(1, 2, num1=3, num2=4)
<class 'tuple'> (1, 2)
<class 'dict'> {'num1': 3, 'num2': 4}





10
1
2
3
4
def func(num1, num2, num3):
return num1, num2, num3
data = [1, 2, 3]
func(*data) # func(1, 2, 3)
(1, 2, 3)
1
2
3
4
5
data = {
"num2": 100,
"num3": 200,
}
func(1, **data) # func(1, num2=100, num3=200)
(1, 100, 200)
1

Author

KangWon Seo

Posted on

2021-02-22

Updated on

2021-03-05

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.