πPython
Welcome to Python : ν¨μ
4:Bee
2023. 7. 13. 13:39
728x90
- ν¨μ : https://hi-code.tistory.com/119
- μ¬μ©μ μ μΆλ ₯ : https://hi-code.tistory.com/120
- νμΌ μ½κ³ μ°κΈ° : https://hi-code.tistory.com/121
- νλ‘κ·Έλ¨μ μ μΆλ ₯ : https://hi-code.tistory.com/122
Title | Code | Console |
ν¨μ | ||
ν¨μμ κΈ°λ³Έ ꡬ쑰 |
def add(a, b): return a+b
a = 3
b = 4
c = add(a, b)
print(c)
|
7 |
def add(a, b): print("%d, %dμ ν©μ %dμ
λλ€." % (a, b, a+b))
return a+b
add(3, 4)
|
3, 4μ ν©μ 7μ λλ€. | |
μ¬λ¬ κ°μ μ λ ₯κ°μ λ°λ ν¨μ λ§λ€κΈ° |
def add_many(*args): result = 0
for i in args:
result = result + i
return result
result = add_many(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(result)
|
55 |
def add_mul(choice, *args): if choice == "add":
result = 0
for i in args:
result = result + i
elif choice == "mul":
result = 1
for i in args:
result = result * i
return result
result_mul = add_mul('add', 1, 2, 3, 4, 5)
print(result_mul)
result_add = add_mul('mul', 1, 2, 3, 4, 5)
print(result_add)
|
15 120 |
|
ν€μλ λ§€κ°λ³μ. kwargs |
def print_kwargs(**kwargs): print(kwargs)
print_kwargs(a=1)
print_kwargs(name='foo', age=3)
|
{'a': 1} {'name': 'foo', 'age': 3} |
ν¨μμ 리ν΄κ°μ νμ© |
def add_and_mul(a, b):
return a+b, a*b
result = add_and_mul(3, 4) #ν¬ν ννλ‘ λ°ν
print(result)
|
(7, 12) |
def add_and_mul(a, b):
return a+b, a*b
result1, result2 = add_and_mul(3, 4)
print(result1, result2) # result1 7, result2 12
|
7 12 | |
def say_nick(nick): if nick == 'λ°λ³΄':
return print('λ°λ³΄λμ')
print("λμ λ³λͺ
μ %sμ
λλ€." % nick)
say_nick("μΌνΈ") # ifλ¬Έμ μ‘°κ±΄μ΄ λ§μ§ μμμ print
say_nick("λ°λ³΄") # ifλ¬Έμ μ‘°κ±΄μ΄ λ§μμ return
|
λμ λ³λͺ
μ μΌνΈμ
λλ€. λ°λ³΄λμ |
|
λ§€κ°λ³μ μ΄κΉκ° μ€μ νκΈ° |
def say_myself(name, age, man=True): print("λμ μ΄λ¦μ %sμ
λλ€." % name)
print("λμ΄λ %dμ΄μ
λλ€." % age)
if man:
print("λ¨μμ
λλ€.")
else:
print("μ¬μμ
λλ€.")
say_myself("λ°μμ©", 27)
say_myself("λ°μμ©", 27, True)
|
λμ μ΄λ¦μ λ°μμ©μ
λλ€. λμ΄λ 27μ΄μ λλ€. λ¨μμ λλ€. λμ μ΄λ¦μ λ°μμ©μ λλ€. λμ΄λ 27μ΄μ λλ€. λ¨μμ λλ€. |
def say_myself(name, man=True, age): #μ μλ λ§€κ°λ³μ λ€μ μΌλ° λ§€κ°λ³μκ° μ¬ μ μλ€.
print("λμ μ΄λ¦μ %sμ
λλ€." % name)
print("λμ΄λ %dμ΄μ
λλ€." % age)
if man:
print("λ¨μμ
λλ€.")
else:
print("μ¬μμ
λλ€.")
say_myself("λ°μμ©", 27)
|
File "...\Quiz.py", line 1 def say_myself(name, man=True, age): ^^^ SyntaxError: non-default argument follows default argument |
|
retrun μ¬μ©νκΈ° |
a = 1 def vartest(a):
a = a+1
return a
a = vartest(a)
print(a)
|
2 |
global μ¬μ©νκΈ° |
a = 1 def vartest():
global a
a = a+1
vartest()
print(a)
|
2 |
lambda μμ½μ΄ |
def add(a, b): return a+b result = add(3, 4)
print(result)
|
7 |
728x90