Guidelines

ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜μ— 기본값을 λΆ€μ—¬ν•˜λŠ” 방법

파이썬 ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜μ—λŠ” 기본값을 μ„€μ •ν•  수 있으며, 이λ₯Ό κΈ°λ³Έ λ§€κ°œλ³€μˆ˜(default parameter)라고 ν•©λ‹ˆλ‹€.

κΈ°λ³Έ λ§€κ°œλ³€μˆ˜λŠ” ν•¨μˆ˜ 호좜 μ‹œ ν•΄λ‹Ή λ§€κ°œλ³€μˆ˜μ— 값을 μ „λ‹¬ν•˜μ§€ μ•Šμ„ 경우 기본값을 μ‚¬μš©ν•©λ‹ˆλ‹€.


κΈ°λ³Έ λ§€κ°œλ³€μˆ˜μ˜ μ •μ˜

ν•¨μˆ˜ μ •μ˜ μ‹œ λ§€κ°œλ³€μˆ˜μ— = μ—°μ‚°μžλ‘œ 기본값을 ν• λ‹Ήν•©λ‹ˆλ‹€.

κΈ°λ³Έ λ§€κ°œλ³€μˆ˜ μ˜ˆμ‹œ
def greet(name, message="μ•ˆλ…•ν•˜μ„Έμš”"): # message λ§€κ°œλ³€μˆ˜μ˜ 기본값을 "μ•ˆλ…•ν•˜μ„Έμš”"둜 μ„€μ • return f"{message}, {name}λ‹˜!" print(greet("μ½”λ“œν”„λ Œμ¦ˆ")) # "μ•ˆλ…•ν•˜μ„Έμš”, μ½”λ“œν”„λ Œμ¦ˆλ‹˜!"

κΈ°λ³Έ λ§€κ°œλ³€μˆ˜ μ‚¬μš© μ˜ˆμ‹œ

κΈ°λ³Έ λ§€κ°œλ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜λ©΄ ν•¨μˆ˜ 호좜 μ‹œ ν•΄λ‹Ή λ§€κ°œλ³€μˆ˜μ— 값을 μ „λ‹¬ν•˜μ§€ μ•Šμ•„λ„ λ©λ‹ˆλ‹€.

κΈ°λ³Έ λ§€κ°œλ³€μˆ˜ μ‚¬μš© μ˜ˆμ‹œ
print(greet("κΈ±ν•˜μš°μŠ€")) # "μ•ˆλ…•ν•˜μ„Έμš”, κΈ±ν•˜μš°μŠ€λ‹˜!" print(greet("κΈ±ν•˜μš°μŠ€", "λ°˜κ°‘μŠ΅λ‹ˆλ‹€")) # "λ°˜κ°‘μŠ΅λ‹ˆλ‹€, κΈ±ν•˜μš°μŠ€λ‹˜!"

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result