본문 바로가기
프로그램 개발해서 돈벌기/AI

파이션(python) 초등 완전 기초: 함수(Function)

by ubmuhan 2023. 2. 9.
반응형

함수 생성

"add_three" 함수 생성

# Define the function
def add_three(input_var):
    output_var = input_var + 3
    return output_var

 

그림 1. 함수 구조

 

함수 사용

# Run the function with 10 as input
new_number = add_three(10)

# Check that the value is 13, as expected
print(new_number)

결과: 13

그림 2. 함수 사용 구조

 

 

함수 멀티 파라미터(multiple arguments)

def get_pay_with_more_inputs(num_hours, hourly_wage, tax_bracket):
    # Pre-tax pay
    pay_pretax = num_hours * hourly_wage
    # After-tax pay
    pay_aftertax = pay_pretax * (1 - tax_bracket)
    return pay_aftertax
 
higher_pay_aftertax = get_pay_with_more_inputs(40, 24, .22)
print(higher_pay_aftertax)

결과: 748.8000000000001

 

 

함수 인자가 없는 경우 (no arguments)

# Define the function with no arguments and with no return
def print_hello():
    print("Hello, you!")
    print("Good morning!")
    
# Call the function
print_hello()

결과: Hello, you!
Good morning!

 

 

<< 참조 >>

https://www.kaggle.com/code/alexisbcook/functions

 

Functions

Explore and run machine learning code with Kaggle Notebooks | Using data from No attached data sources

www.kaggle.com

 

 

 

반응형

댓글