Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use return in Python

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to use return in Python". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use return in Python" can help you solve your doubts.

Usage of return in python 1. The return statement returns the execution result to the place where it is called, and returns the control of the program together.

When the program runs to the first return it encounters, it returns (exits the def block) and will not run the second return.

For example:

Def (XQuery y): if xsancyprint ( (1)) has been corrected: result: this kind of return parameter will return tuple (1) 2, but it does not mean that there can be only one return statement in a function body, for example: def test_return (x): if x > 0: return x else: return 0print (test_return (2)) 3, the function has no return, and the default return is a None object.

Cases where there is no return in the recursive function:

Def recurve (aformab): if a%b==0: return b else: gcd (bmaine a% b)

Analysis: if there is no return in else, there is no exit. This program runs internally and the program does not return a value.

4. In interactive mode, the results of return will be printed automatically, while when running as a script alone, you need the print function to display.

What is the interaction mode in python: it ends with three > symbols (>). > is called the Python command prompt (prompt)

Enter a line of python code to execute the code, which is called Python interaction mode (interactive mode)

5. By default, the return function is returned to the caller when it is encountered, but try Except in the case of finally: def func (): try: print (666) return "ok" finally: print (666) print (func ()) 6. The function is returned as a return value: (actually a closure function) def sum1 (* args): def sum2 (): x=x+i return x return sum2sum1 0 for i in args: x=x+i return x return sum2sum1 3) print (a ()) result: 6: 1 / 2 / 37. Return a list of functions: def count (): fs = [] for i in range (1jol 4): def f (): return Isimi fs.append (f) return fsf1, f2, f3 = count () print (F1 ()) print (f2 ()) print (f3 ()) output: 999 here This article "how to use return in Python" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report