2017 Winter Vacation 0 Basic Learning the function of the Python series returns multiple values

Source: Internet
Author: User
Tags sin square root

Python, like C, has its own standard library, but in Python it's called a module, which is similar to a header file in C and a package in Java, where math is one of them, and the sin () and cos () functions are available in the Math module
The format for referencing modules in Python (in reference to the math example) is: Import Math

For an example that calculates the coordinates in the game:

Import Math def  = x + Step *= y-step * math.sin (angle)return nx,xy

This allows the return value to be obtained at the same time:

X, y = Move (100,100,60,MATH.PI/6)print  x, y>>>151.961524227 70.0

Do you feel this aspect is much stronger than C language?!

In fact, this is an "illusion", the return in Python is still a value!

R = Move (100,100,60,MATH.PI/6)print  r>>>151.96152422706632, 70.0

The truth is that the returned result is printed with print and the returned value is a tuple!

Summary: Python's function returns a multivalued value that returns a tuple, but is easier to write.

Small problem:
The definition of a unary two-second equation is: ax2 + bx + c = 0

Write a function that returns the two solutions of the two-second equation.

Note: The Python math package provides the SQRT () function for calculating the square root.

Code:

Import Math def   = (-B + math.sqrt (B*B-4*A*C))/(= (-b-math.sqrt (b*b-4*a*c))/(a)return  X1,X2Print quadratic_equation (2, 3, 0)print quadratic_equation (1,-6, 5)

2017 Winter Vacation 0 Basic Learning the function of the Python series returns multiple values

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.