printf("ho_tari\n");

함수 본문

Python

함수

호타리 2023. 10. 16. 11:25

<rectangle_area.py>

def calculate_rectangle_area(x, y):
    return x * y

rectangle_x = 10
rectangle_y = 20

print("사각형 x의 길이:", rectangle_x)
print("사각형 y의 길이:", rectangle_y)

# 넓이를 구하는 함수 호출
print("사각형의 얿이:", calculate_rectangle_area(rectangle_x, rectangle_y))

<compile 결과>

 

<function.py>

def f(x):
    return 2 * x + 7

def g(x):
    return x ** 2

x = 2

print(f(x) + g(x) + f(g(x)) + g(f(x)))

<compile 결과>

 

<function_type.py>

def a_rectangle_area():
    print(5 * 7)

def b_rectangle_area(x, y):
    print(x * y)

def c_rectangle_area():
    return(5 * 7)

def d_rectangle_area(x, y):
    return(x * y)

a_rectangle_area()
b_rectangle_area(5, 7)
print(c_rectangle_area())
print(d_rectangle_area(5, 7))

<compile 결과>

 

<call1.py>

def f(x):
    y = x
    x = 5
    return y * y

x = 3
print(f(x))
print(x)

<compile 결과>

 

<call2.py>

def spam(eggs):
    eggs.append(1)  # 기존 객체의 주소값에 [1] 추가
    eggs = [2, 3]   # 새로운 객체 생성

ham = [0]
spam(ham)
print(ham)

<compile 결과>

 

<local_variable.py>

def f():
    s = "I love Korea!"
    print(s)

s = "I love Hanguk!"
f()
print(s)

<compile 결과>

 

<global_variable.py>

def f():
    global s
    s = "I love Korea!"
    print(s)

s = "I love Hanguk!"
f()
print(s)

<compile 결과>

 

<scoping_rule.py>

def calculate(x, y):
    total = x + y   # 새로운 값이 할당되어 함수 내부 total은 지역 변수가 됨
    print("In Function")
    print("a:", str(a), "b:", str(b), "a+b:", str(a+b), "total:", str(total))
    return total

a = 5   # a와 b는 전역 변수
b = 7
total = 0   # 전역 변수 total

print("In Program -1")
print("a:", str(a), "b:", str(b), "a+b:", str(a+b))

sum = calculate (a, b)
print("After Calculation")
print("Total:", str(total), " Sum:", str(sum))  # 지역 변수는 전역 변수에 영향을 주지 않음

<compile 결과>

 

<factorial.py>

def factorial(n):
    if n == 1:
        return 1
    else:
        return n * factorial(n-1)
    
print(factorial(int(input("Input Number for Factorial Calculation: "))))

<compile 결과>

 

<keyword.py>

def print_something(my_name, your_name):
    print("Hello {0}, My name is {1}".format(your_name, my_name))

print_something("Hotari", "Python")
print_something(your_name = "Python", my_name = "Hotari")

<compile 결과>

 

<default.py>

def print_something_2(my_name, your_name = "Python"):
    print("Hello {0}, My name is {1}".format(your_name, my_name))

print_something_2("Hotari", "Python")
print_something_2("Hotari")

<compile 결과>

 

<asterisk1.py>

def asterisk_test(a, b, *args):
    return a + b + sum(args)

print(asterisk_test(1, 2, 3, 4, 5))

<compile 결과>

 

<asteirsk2.py>

def asterisk_test(a, b, *args):
    print(args)

print(asterisk_test(1, 2, 3, 4, 5))

<compile 결과>

 

<asterisk3.py>

def asterisk_test_2(*args):
    x, y, *z = args
    return x, y, z

print(asterisk_test_2(3, 4, 5))

<compile 결과>

 

<asterisk4.py>

def asterisk_test_2(*args):
    x, y, *z = args
    return x, y, z

print(asterisk_test_2(3, 4, 5, 10, 20))

<compile 결과>

 

<kwargs.py>

def kwargs_test(**kwargs):
    print(kwargs)
    print("First value is {first}".format(**kwargs))
    print("Second value is {second}".format(**kwargs))
    print("Third value is {third}".format(**kwargs))

kwargs_test(first = 3, second = 4, third = 5)

<compile 결과>

 

<hello.py>

def print_hello():
    print("Hello World")
    print("Hello Python")

a = 5

if a > 3:
    print_hello()

if a > 4:
    print_hello()

if a > 5:
    print_hello()

<compile 결과>

 

<math.py>

import math
def get_result_quadratic_equation(a, b, c):
    values = []
    values.append((-b + math.sqrt(b ** 2 - (4 * a * c))) / (2 * a))
    values.append((-b - math.sqrt(b ** 2 - (4 * a * c))) / (2 * a))
    return values

print(get_result_quadratic_equation(1, -2, 1))

<compile 결과>

 

 

'Python' 카테고리의 다른 글

자료구조  (0) 2023.10.16
문자열  (0) 2023.10.16
반복문  (0) 2023.10.12
조건문  (0) 2023.10.12
리스트  (0) 2023.10.12