Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
Python
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
KimMinSeob
Python
Commits
e3589763
Commit
e3589763
authored
6 years ago
by
KimMinSeob
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
be4d7448
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
함수연습.py
+208
-0
208 additions, 0 deletions
함수연습.py
with
208 additions
and
0 deletions
함수연습.py
0 → 100644
+
208
−
0
View file @
e3589763
Python
3.7
.
1
(
v3
.
7.1
:
260
ec2c36a
,
Oct
20
2018
,
03
:
13
:
28
)
[
Clang
6.0
(
clang
-
600.0
.
57
)]
on
darwin
Type
"
help
"
,
"
copyright
"
,
"
credits
"
or
"
license()
"
for
more
information
.
>>>
def
sum
(
a
,
b
):
return
a
+
b
>>>
sum
(
3
,
4
)
7
>>>
a
=
3
>>>
b
=
4
>>>
c
=
sum
(
a
,
b
)
>>>
c
7
>>>
print
(
sum
(
3
,
4
))
7
>>>
sum
(
3
,
4
)
7
>>>
#함수의 형태는 입력값과 결과값의 존재 유무에 따라 4가지 유형으로 나뉜다.
>>>
#입력값이 있고 결과값이 있는 함수의 예
>>>
def
sum
(
a
,
b
):
result
=
a
+
b
return
result
>>>
a
=
sum
(
3
,
4
)
>>>
print
(
a
)
7
>>>
def
say
():
return
'
hi
'
>>>
say
()
'
hi
'
>>>
#위는 입력값이 없는 함수이다.
>>>
a
=
say
()
>>>
print
(
a
)
hi
>>>
type
(
a
)
<
class
'
str
'
>
>>>
#결과값이 없는 함수
>>>
def
sum
(
a
,
b
):
print
(
"
%d, %d의 합은 %d입니다.
"
%
(
a
,
b
,
a
+
b
))
>>>
sum
(
4
,
5
)
4
,
5
의
합은
9
입니다
.
>>>
a
=
sum
(
3
,
4
)
3
,
4
의
합은
7
입니다
.
>>>
type
(
a
)
<
class
'
NoneType
'
>
>>>
#함수의 결과값이 없기때문에 (return 해주는것이 없기때문에) a에 아무것도 들어가지않음
>>>
#입력값과 결과값이 모두 없는 함수
>>>
def
say
():
print
(
'
hi
'
)
>>>
say
()
hi
>>>
a
=
say
()
hi
>>>
type
(
a
)
<
class
'
NoneType
'
>
>>>
>>>
#함수를 호출할때 매개변수를 지정하여 호출할 수도 있다.
>>>
def
sum
(
a
,
b
):
return
a
+
b
>>>
sum
(
a
=
3
,
b
=
5
)
8
>>>
sum
(
b
=
5
,
a
=
3
)
8
>>>
#매개변수가 몇개가 입력될지 모를때는 *매개변수 로 쓸수있다.
>>>
def
sum_many
(
*
number
):
sum
=
0
for
i
in
number
:
sum
=
sum
+
i
return
sum
>>>
sum_many
(
1
,
2
,
3
,
4
,
5
)
15
>>>
# *매개변수와 일반 매개변수를 함꼐사용할 수 있다.
>>>
def
sum_many2
(
button
,
*
number
):
if
button
==
'
a
'
:
result
=
0
for
i
in
number
:
result
=
result
+
i
elif
button
==
'
b
'
:
result
=
1
for
i
in
nubmer
:
result
=
result
*
i
return
result
>>>
sum_many2
(
'
a
'
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
)
45
>>>
sum_many2
(
'
b
'
,
1
,
2
,
3
)
Traceback
(
most
recent
call
last
):
File
"
<pyshell#69>
"
,
line
1
,
in
<
module
>
sum_many2
(
'
b
'
,
1
,
2
,
3
)
File
"
<pyshell#67>
"
,
line
8
,
in
sum_many2
for
i
in
nubmer
:
NameError
:
name
'
nubmer
'
is
not
defined
>>>
def
sum_many2
(
button
,
*
number
):
if
button
==
'
a
'
:
result
=
0
for
i
in
number
:
result
=
result
+
i
elif
button
==
'
b
'
:
result
=
1
for
i
in
number
:
result
=
result
*
i
return
result
>>>
sum_many2
(
'
b
'
,
1
,
2
,
3
)
6
>>>
#함수의 결과값은 언제나 하나이다.
>>>
def
sum_and_mul
(
a
,
b
):
return
a
+
b
,
a
*
b
>>>
result
=
sum_and_mul
(
4
,
5
)
>>>
result
(
9
,
20
)
>>>
#합한거와 곱한거를 튜플형식으로 하나로 묶어서 저장해준다.
>>>
#만약 리턴문을 두개이상 넣으면 맨 첫 리턴문만 실행된다
>>>
def
sum_and_mul2
(
a
,
b
):
return
a
+
b
return
a
*
b
>>>
result
=
sum_and_mul2
(
3
,
4
)
>>>
result
7
>>>
#변수하나당 값을 하나씩 갖고싶다면 변수 여러개 로 함수를 받으면 된다.
>>>
sum
,
mul
=
sum_and_mul
(
3
,
4
)
>>>
sum
7
>>>
mul
12
>>>
def
say_nick
(
nick
):
if
nick
==
'
babo
'
:
return
print
(
"
nick
'
s nickname is %s
"
%
nick
)
>>>
say_nick
(
nick
)
Traceback
(
most
recent
call
last
):
File
"
<pyshell#96>
"
,
line
1
,
in
<
module
>
say_nick
(
nick
)
NameError
:
name
'
nick
'
is
not
defined
>>>
say_nick
(
'
babo
'
)
>>>
say_nick
(
'
mungchung
'
)
nick
'
s nickname is mungchung
>>>
#매개변수에 초깃값을 미리 설정할 수 있다.
>>>
def
say_myself
(
name
,
old
,
man
=
True
):
print
(
"
name is %s
"
%
name
)
print
(
"
age is %d
"
%
old
)
if
man
:
print
(
"
man
"
)
else
:
print
(
"
woman
"
)
>>>
saym_myself
(
'
kimminseob
'
,
24
,
man
)
Traceback
(
most
recent
call
last
):
File
"
<pyshell#108>
"
,
line
1
,
in
<
module
>
saym_myself
(
'
kimminseob
'
,
24
,
man
)
NameError
:
name
'
saym_myself
'
is
not
defined
>>>
say_myself
(
'
kimminseob
'
,
24
,
man
)
Traceback
(
most
recent
call
last
):
File
"
<pyshell#109>
"
,
line
1
,
in
<
module
>
say_myself
(
'
kimminseob
'
,
24
,
man
)
NameError
:
name
'
man
'
is
not
defined
>>>
say_myself
(
'
kimminseob
'
,
24
)
name
is
kimminseob
age
is
24
man
>>>
say_myself
(
'
kimminseob
'
,
24
,
False
)
name
is
kimminseob
age
is
24
woman
>>>
#매개변수를 초기화시킬때는 맨뒤에부터 위치시켜야한다.
>>>
#함수에서 변수의효력범위
>>>
a
=
1
>>>
def
vartest
(
a
):
a
=
a
+
1
>>>
vartest
(
a
)
>>>
print
(
a
)
1
>>>
#a의 값은 바뀌지 않았음
>>>
#a의 값을 바꾸는 2가지 방법 (1) return 이용 (2) global 명령어
>>>
a
=
1
>>>
def
vartest
():
global
a
a
=
a
+
1
>>>
a
1
>>>
vartest
()
>>>
a
2
>>>
a
=
1
>>>
def
vartest
(
a
):
a
=
a
+
1
return
a
>>>
a
=
vartest
(
a
)
>>>
a
2
>>>
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment