Python functions and modules, Python function modules

Source: Internet
Author: User
Tags floor function

Python functions and modules, Python function modules
Introduction

Function concepts also exist in python. Standard functions can be called built-in functions, which can be called directly by function names. However, some other built-in functions cannot be called directly through the function name, such as the floor function (rounded down). Then we need to use the module; this article mainly introduces math and cmath.

Python version: 3.4.4

 

Built-in functions (Standard Functions)

Let's take a look at the standard functions. If you use the IDLE program for testing, the color of the function name will change to purple when you enter the built-in function, but not the built-in function.

1. abs: absolute value

>>> abs(-5)5

2. pow (x, y): Power y of x

>>> pow(2,3)8

Non-built-in functions

Syntax

import math[cmath]math[cmath].function_name

Or

from math[cmath] improt function_name

Note: The usage of cmath is the same as that of math, but cmath is used to process plural numbers. For example, if you want to evaluate the square root of a negative number, you need to use cmath.

1. floor: round down

>>> floor(31.5)Traceback (most recent call last):  File "<pyshell#70>", line 1, in <module>    floor(31.5)NameError: name 'floor' is not defined

Because floor is not a standard function, an error is reported when calling it directly, and the module is required.

>>> import math>>> math.floor(31.5)31
>>> from math import floor>>> floor(31.5)31

2. sqrt: take the square root

>>> import math>>> math.sqrt(4)2.0>>> math.sqrt(-4)Traceback (most recent call last):  File "<pyshell#82>", line 1, in <module>    math.sqrt(-4)ValueError: math domain error

As you can see, if math is used to take the square root of a negative number, an error is returned. In this case, you need to use cmath.

>>> import cmath>>> cmath.sqrt(-4)2j
>>> from cmath import sqrt>>> sqrt(-4)2j

Other functions

 

Summary

The syntax of python changes in each new version. You need to pay attention to the changes made to each new version.

 

 

 

Note:

Author: pursuer. chen

Blog: http://www.cnblogs.com/chenmh

All essays on this site are original. You are welcome to repost them. However, you must indicate the source of the article and clearly give the link at the beginning of the article.

Welcome to discussion

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.