Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Dalguring
PythonStudy
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
PythonStudy
/
function
/
function_2.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
67 lines (54 loc) · 1.94 KB
master
Breadcrumbs
PythonStudy
/
function
/
function_2.py
Copy path
Top
File metadata and controls
Code
Blame
67 lines (54 loc) · 1.94 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 함수의 리턴값은 언제나 하나이다
def
add_and_mul
(
a
,
b
):
return
a
+
b
,
a
*
b
"""
위와 같은 함수는 자바에서는 허용되지 않는다.
리턴값이 복수 개이기 때문인데, 파이썬에서도 리턴값은 하나여야 한다.
다만 파이썬은 위와 같은 형태일 때 튜플값으로 리턴한다.
"""
print
(
add_and_mul
(
3
,
4
))
# 또는 튜플 값을 분리해서 받고 싶다면 튜플 내 값의 수만큼 변수를 할당하면 된다.
result1
,
result2
=
add_and_mul
(
3
,
4
)
print
(
result1
)
print
(
result2
)
# return 문을 통한 함수 종료
def
say_nick
(
nick
):
if
nick
==
"바보"
:
return
print
(
"나의 별명은 %s 입니다."
%
nick
)
say_nick
(
'야호'
)
say_nick
(
'바보'
)
# 매개변수 초기값 미리 설정하기
def
say_myself
(
name
,
age
,
man
=
True
):
print
(
"나의 이름은 %s 입니다."
%
name
)
print
(
"나이는 %d살 입니다."
%
age
)
if
man
:
print
(
"남자입니다."
)
else
:
print
(
"여자입니다."
)
say_myself
(
"서성민"
,
28
)
say_myself
(
"조민지"
,
27
,
False
)
# 함수 내 변수의 효력 범위
a
=
1
def
vartest
(
a
):
a
+=
1
vartest
(
a
)
print
(
a
)
# a = 1 <- 함수 내에서 사용하는 매개변수는 함수 안에서만 사용하는 지역변수. a 값에는 변함 없음
# 해결방법 1 : return 사용
a
=
1
def
vartest
(
a
):
a
+=
1
return
a
a
=
vartest
(
a
)
print
(
a
)
# 해결방법 2 : global 사용 / global a 는 함수 내에서 함수 밖의 a 변수를 직접 사용 하겠다는 뜻이다. 하지만 global 명령어는 사용하지 않는 것이 좋다(종속성) return 권장!
a
=
1
def
vartest
():
global
a
a
+=
1
vartest
()
print
(
a
)
# lambda 예약어 / 함수를 한 줄로 간결하게 만들 때 사용.
# 함수_이름 = lambda 매개변수1, 매개변수2, ... : 매개변수를_이용한_표현식 형태로 작성
add
=
lambda
a
,
b
:
a
+
b
print
(
add
(
3
,
4
))
# lambda 함수는 return 명령어가 없어도 결괏값을 리턴함.
You can’t perform that action at this time.