Search
Duplicate

Functions

νƒœκ·Έ
ν•¨μˆ˜
Defining a Function
ꡬ문
def function_name(): # functions tasks go here
Python
볡사
def λŠ” ν•¨μˆ˜μ˜ μ‹œμž‘μ„ μ•Œλ¦¬λŠ” ν‚€μ›Œλ“œμ΄λ‹€. ν•¨μˆ˜μ˜ 이름은 snake_case ν˜•μ‹μ„ λ”°λ₯΄λ©°, μˆ˜ν–‰ν•  κΈ°λŠ₯을 μ•Œλ¦°λ‹€.
() μ•ˆμ—λŠ” νŒŒλΌλ―Έν„°κ°€ λ“€μ–΄κ°„λ‹€.
: ν•¨μˆ˜ ν—€λ”μ˜ 끝을 μ•Œλ¦¬λŠ” ν‚€μ›Œλ“œμ΄λ‹€.
ν˜ΈμΆœν•  λ•Œμ—λŠ” ν•¨μˆ˜μ˜ μ΄λ¦„λ§Œ 적으면 λœλ‹€.
Parameters & Arguments
νŒŒλΌλ―Έν„°μ™€ arguments 의 차이λ₯Ό μ•Œμ•„λ³΄μž
def [ν•¨μˆ˜λͺ…]([parameters]): functions . . . print(”. . .” + parameters) # parameters λŠ” ν•¨μˆ˜ μ•ˆμ—μ„œ λ³€μˆ˜μ²˜λŸΌ μ‚¬μš©μ΄ κ°€λŠ₯ν•˜λ‹€.
Python
볡사
ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  λ•Œ λ“€μ–΄κ°€λŠ” μΈμžλŠ” argument 라고 ν•œλ‹€.
[ν•¨μˆ˜λͺ…]([argument])
이 argument ν•¨μˆ˜ λ‚΄μ—μ„œ λ³€μˆ˜μ²˜λŸΌ μ‚¬μš©μ΄ 될 것이닀.
Built in function
function
ꡬ문
μ—­ν• 
max
max(λ³€μˆ˜1, λ³€μˆ˜2 . . .)
μ΅œλŒ“κ°’μ„ μ°Ύμ•„μ€€λ‹€.
min
min(λ³€μˆ˜1, λ³€μˆ˜2 . . .)
μ΅œμ†Ÿκ°’μ„ μ°Ύμ•„μ€€λ‹€.
round
round(λ³€μˆ˜)
λ°˜μ˜¬λ¦Όμ„ ν•΄μ€€λ‹€.
Return
리턴값이 μ—¬λŸ¬κ°œμΈ 경우λ₯Ό 예제둜 λŒ€μ²΄ν•œλ‹€.
[예제 μ½”λ“œ]
def top_tourist_locations_italy(): first = "Rome" second = "Venice" third = "Florence" return first, second, third most_popular1, most_popular2, most_popular3 = top_tourist_locations_italy() print(most_popular1) print(most_popular2) print(most_popular3)
Python
볡사
[κ²°κ³Ό]
Rome
Venice
Florence