Some Understanding of return and print in python, pythonreturnprint

Source: Internet
Author: User

Some Understanding of return and print in python, pythonreturnprint

Preface

I recently started to learn python. I only had some c basics and I forgot about it. Now I want to learn and share my ideas ~ I read the return usage while reading the book, but it is mixed with print later. I always feel that the return value of the function can be directly displayed, but this is not the case. Next, let's take a look at the understanding of return and print in python. Let's take a look at the detailed introduction.

See an example:

Code (1)

x = 1y = 2def add (x, y):  z = x + y  return zprint (add(x,y))

Code (2)

x = 1y = 2def add (x, y):  z = x + y  print zprint (add(x,y))

Output result (Same)

>>> 3

Here, the result of code 1 is obtained by printing the return value, and the result of Code 2 is printed in the functionadd (x, y),add (x, y)When the print z statement is executed, the returned value is None, so the output result should be

3None

The return value can only be printed. For example

def a():  print('ss')def b():  return 'ss'

Run directlya() And runs directly with results.b()There is no result, only runprint(b())To display 'ss '.

In addition, assign a function to a variable to obtain the return value.b() , Add

c=b()c

There is a result, because c getsb()The return value is 'ss '.

In the interactive mode, the return result is automatically printed, and the print function is required for the script to be run independently.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.