๊ด€๋ฆฌ ๋ฉ”๋‰ด

C-log

Welcome to Python : ํ•จ์ˆ˜ ๋ณธ๋ฌธ

๐Ÿ“˜Python

Welcome to Python : ํ•จ์ˆ˜

4:Bee 2023. 7. 13. 13:39
728x90

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

'๐Ÿ“˜Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Welcome to Python : ํŒŒ์ผ ์ฝ๊ณ  ์“ฐ๊ธฐ  (0) 2023.07.13
Welcome to Python : ์‚ฌ์šฉ์ž ์ž…์ถœ๋ ฅ  (0) 2023.07.13
Welcome to Python : for๋ฌธ  (0) 2023.07.09
Welcome to Python : while๋ฌธ  (0) 2023.07.09
Welcome to Python : if๋ฌธ  (0) 2023.07.09
Comments