logo

بيان عودة بايثون

مقدمة

ال بايثون يتم استخدام عبارة الإرجاع لإرجاع قيمة من دالة. يمكن للمستخدم فقط استخدام عبارة الإرجاع في إحدى الوظائف. لا يمكن استخدامه خارج وظيفة بايثون. تتضمن عبارة الإرجاع الكلمة الأساسية return والقيمة التي سيتم إرجاعها بعد ذلك.

بناء جملة بيان الإرجاع:

 def funtion_name(): statements . . . return [expression] 

البرنامج 1

 def adding(x, y): i = x + y return i result = adding(16, 25) print(f'Output of adding(16, 25) function is {result}') 

انتاج |

بيان عودة بايثون

البرنامج 2

 def adding(a, b): # this function is return the value of (a + b) return a + b def boolean_function(a): # this function is return the Boolean value return bool(a) # calling function flag = adding(2, 3) print(&apos;Output of first function is {}&apos;.format(flag)) flag = boolean_function(9 <5) print('
output of second function is {}'.format(flag)) < pre> <p> <strong>Output.</strong> </p> <img src="//techcodeview.com/img/python-tutorial/18/python-return-statement-2.webp" alt="Python return statement"> <h3>Returning Multiple Values</h3> <p>In the Python programming language, a user can return multiple values from a function. The following are the various methods for this.</p> <p> <strong>1. Using Object:</strong> This method is similar to <a href="/c-programming-language-tutorial">C</a> / <a href="/c-tutorial">C ++</a> and <a href="/java-tutorial">Java</a> . A user can create a class to hold multiple values in a function and return an object of that class.</p> <pre> class a: def __init__(self): self.omg = &apos;javatpoint is the best website to learn&apos; self.i = 122 # This function will return an object of the class a def test(): return a() # Driver code to test the above method z = test() print(z.omg) print(z.i) </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/python-tutorial/18/python-return-statement-3.webp" alt="Python return statement"> <p> <strong>2. Using Tuple:</strong> The tuple is similar to a list, but there is a slight difference between tuple and list. In the tuple, the object values cannot be changed, while the object value can be changed in the list.</p> <pre> def test(): omg = &apos;javatpoint is the best website to learn&apos; i = 122 return omg, i; # Return tuple, we could also. # Driver code to test the above method. omg, i = test() # Assign return tuple print(omg) print(i) </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/python-tutorial/18/python-return-statement-4.webp" alt="Python return statement"> <p> <strong>3. Using list:</strong> The list is similar to the dynamically sized array. In the list, the user can store everything in a single variable.</p> <pre> def test(): omg = &apos;javatpoint&apos; i = 122 return [omg, i]; # Driver code to test the above method list = test() print(list) </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/python-tutorial/18/python-return-statement-5.webp" alt="Python return statement"> <p> <strong>4. Using dictionary:</strong> In the Python language, a dictionary is a collection of unstructured items that are used to store data values such as hash or map.</p> <pre> def test(): a = dict(); a[&apos;omg&apos;] = &apos;javatpoint&apos; a[&apos;i&apos;] = 122 return a # Driver code to test the above method a = test() print(a) </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/python-tutorial/18/python-return-statement-6.webp" alt="Python return statement"> <p> <strong>5. Using Data Class (Python 3.7+)</strong> </p> <pre> from dataclasses import dataclass @dataclass class Book_list: bookname: str cost: float quantity_of_book_available: int = 0 # This function is used to calculate the total cost of the books def total_cost_of_book(self) -&gt; float: return self.cost * self.quantity_of_book_available book = Book_list(&apos;Python programming language.&apos;, 499, 10) i = book.total_cost_of_book() # print the total cost print(i) # print the details of the book print(book) </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/python-tutorial/18/python-return-statement-7.webp" alt="Python return statement"> <h3>Function returning another function</h3> <p>In the Python programming language, a function is in the form of an object. Therefore, the user can return a function from another function.</p> <p>In the below program, The first_add function returns the second_add function.</p> <pre> def first_add(x): def second_add(y): return x + y return second_add i = first_add(20) print(&apos;The value of x + y is&apos;, i(10)) # second function def outer_func(x): return x * 5 def func(): # return the value in the different function return outer_func # storing the function in z z = func() print(&apos;
The value of x * y is&apos;, z(10)) </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/python-tutorial/18/python-return-statement-8.webp" alt="Python return statement"> <hr></5)>

انتاج |

رقم عشوائي بين 1 و 10
بيان عودة بايثون

2. استخدام الصفوف: الصف يشبه القائمة، ولكن هناك اختلاف طفيف بين الصف والقائمة. في الصف، لا يمكن تغيير قيم الكائن، بينما يمكن تغيير قيمة الكائن في القائمة.

 def test(): omg = &apos;javatpoint is the best website to learn&apos; i = 122 return omg, i; # Return tuple, we could also. # Driver code to test the above method. omg, i = test() # Assign return tuple print(omg) print(i) 

انتاج |

بيان عودة بايثون

3. استخدام القائمة: تشبه القائمة المصفوفة ذات الحجم الديناميكي. في القائمة، يمكن للمستخدم تخزين كل شيء في متغير واحد.

 def test(): omg = &apos;javatpoint&apos; i = 122 return [omg, i]; # Driver code to test the above method list = test() print(list) 

انتاج |

بيان عودة بايثون

4. استخدام القاموس: في لغة بايثون، القاموس عبارة عن مجموعة من العناصر غير المنظمة التي تُستخدم لتخزين قيم البيانات مثل التجزئة أو الخريطة.

 def test(): a = dict(); a[&apos;omg&apos;] = &apos;javatpoint&apos; a[&apos;i&apos;] = 122 return a # Driver code to test the above method a = test() print(a) 

انتاج |

بيان عودة بايثون

5. استخدام فئة البيانات (Python 3.7+)

 from dataclasses import dataclass @dataclass class Book_list: bookname: str cost: float quantity_of_book_available: int = 0 # This function is used to calculate the total cost of the books def total_cost_of_book(self) -&gt; float: return self.cost * self.quantity_of_book_available book = Book_list(&apos;Python programming language.&apos;, 499, 10) i = book.total_cost_of_book() # print the total cost print(i) # print the details of the book print(book) 

انتاج |

فئة الماسح الضوئي جافا
بيان عودة بايثون

دالة ترجع دالة أخرى

في لغة برمجة بايثون، تكون الدالة على شكل كائن. لذلك، يمكن للمستخدم إرجاع دالة من دالة أخرى.

في البرنامج أدناه، تقوم الدالة first_add بإرجاع الدالة Second_add.

 def first_add(x): def second_add(y): return x + y return second_add i = first_add(20) print(&apos;The value of x + y is&apos;, i(10)) # second function def outer_func(x): return x * 5 def func(): # return the value in the different function return outer_func # storing the function in z z = func() print(&apos;
The value of x * y is&apos;, z(10)) 

انتاج |

بيان عودة بايثون