logo

파이썬 반환문

반환 명세서 함수 호출의 실행을 종료하고 결과(return 키워드 다음에 오는 표현식의 값)를 호출자에게 반환하는 데 사용됩니다. return 문 뒤의 문은 실행되지 않습니다. return 문에 표현식이 없으면 특수 값 None이 반환됩니다. ㅏ 반품 성명 전달된 명령문이 실행될 수 있도록 함수를 호출하는 데 전반적으로 사용됩니다.

메모: return 문은 함수 외부에서 사용할 수 없습니다.



통사론:

def fun(): statements . . return [expression]>

예:

def cube(x): r=x**3 return r>

예:



파이썬3






# Python program to> # demonstrate return statement> def> add(a, b):> ># returning sum of a and b> >return> a>+> b> def> is_true(a):> ># returning boolean of a> >return> bool>(a)> # calling function> res>=> add(>2>,>3>)> print>(>'Result of add function is {}'>.>format>(res))> res>=> is_true(>2><>5>)> print>(>' Result of is_true function is {}'>.>format>(res))>

>

>

산출:

Result of add function is 5 Result of is_true function is True>

여러 값 반환

Python에서는 함수에서 여러 값을 반환할 수 있습니다. 다음은 다양한 방법입니다.

    객체 사용: 이는 C/C++ 및 Java와 유사하며, 여러 값을 보유하고 클래스의 객체를 반환하는 클래스(C, 구조체)를 생성할 수 있습니다.

파이썬3




# A Python program to return multiple> # values from a method using class> class> Test:> >def> __init__(>self>):> >self>.>str> => 'geeksforgeeks'> >self>.x>=> 20> > # This function returns an object of Test> def> fun():> >return> Test()> > # Driver code to test above method> t>=> fun()> print>(t.>str>)> print>(t.x)>

>

>

산출

geeksforgeeks 20>
    튜플 사용: 튜플은 쉼표로 구분된 항목 시퀀스입니다. ()가 있거나 없이 생성됩니다. 튜플은 변경할 수 없습니다. 보다 이것 자세한 내용은 튜플 .

파이썬3




mysql 사용자 생성

# A Python program to return multiple> # values from a method using tuple> > # This function returns a tuple> def> fun():> >str> => 'geeksforgeeks'> >x>=> 20> >return> str>, x;># Return tuple, we could also> ># write (str, x)> > # Driver code to test above method> str>, x>=> fun()># Assign returned tuple> print>(>str>)> print>(x)>

>

>

    산출:
geeksforgeeks 20>
    목록 사용: 목록은 대괄호를 사용하여 만든 항목 배열과 같습니다. 이는 다양한 유형의 항목을 포함할 수 있다는 점에서 배열과 다릅니다. 리스트는 변경 가능하다는 점에서 튜플과 다릅니다. list 에 대한 자세한 내용은 this을 참조하세요.

파이썬3




# A Python program to return multiple> # values from a method using list> > # This function returns a list> def> fun():> >str> => 'geeksforgeeks'> >x>=> 20> >return> [>str>, x];> > # Driver code to test above method> list> => fun()> print>(>list>)>

>

>

    산출:
['geeksforgeeks', 20]>
    사전 사용: 사전은 다른 언어의 해시 또는 맵과 유사합니다. 보다 이것 자세한 내용은 사전 .

파이썬3


문자열 연결



# A Python program to return multiple> # values from a method using dictionary> > # This function returns a dictionary> def> fun():> >d>=> dict>();> >d[>'str'>]>=> 'techcodeview.com'> >d[>'x'>]>=> 20> >return> d> > # Driver code to test above method> d>=> fun()> print>(d)>

>

>

    산출:
{'x': 20, 'str': 'techcodeview.com'}>

다른 함수를 반환하는 함수

Python에서 함수는 객체이므로 다른 함수에서 함수를 반환할 수 있습니다. 이는 Python에서 함수가 일급 객체로 처리되기 때문에 가능합니다. 일류 개체에 대한 자세한 내용을 보려면 여기를 클릭하십시오.

아래 예에서 create_adder 함수는 adder 함수를 반환합니다.

파이썬3




# Python program to illustrate functions> # can return another function> def> create_adder(x):> >def> adder(y):> >return> x>+> y> >return> adder> add_15>=> create_adder(>15>)> print>(>'The result is'>, add_15(>10>))> # Returning different function> def> outer(x):> >return> x>*> 10> def> my_func():> > ># returning different function> >return> outer> # storing the function in res> res>=> my_func()> print>(>' The result is:'>, res(>10>))>

>

>

산출:

The result is 25 The result is: 100>