Describes python's numeric type variables and methods.

Source: Internet
Author: User
Tags acos asin hypot mathematical constants mathematical functions

Describes python's numeric type variables and methods.

Preface

The python data type cannot be changed, which means that if the value of the Number data type is changed, the memory space will be re-allocated. I will not talk about it below. Let's take a look at the detailed introduction.

The Number object will be created when the following instances assign values to variables:

var1 = 1var2 = 10

You can also use the del statement to delete some Number object references.

You can use the del statement to delete one or more objects, for example:

del vardel var_a, var_b

Python supports four different numeric types:

INTEGER (Int)-it is usually called an integer or integer. It is a positive or negative integer without a decimal point.

Long Integer (long)-an integer of an infinite size. The integer is an L in upper or lower case, for example, 51924361L.

Float: float is composed of integer and decimal parts. float can also be expressed by scientific notation.
(2.5e2 = 2.5x10 ^ 2 = 250)

A complex is composed of a real number and a virtual number. It can be expressed by a + bj or complex (a, B,
The real part a and virtual Part B of the plural are float.

Python Number type conversion:

Int (x [, base]) converts x to an integer.

Long (x [, base]) converts x to a long integer.

Float (x) converts x to a floating point number.

Complex (real [, imag]) creates a plural number

Str (x) converts object x to a string

Repr (x) converts object x to an expression string

Eval (str) is used to calculate a valid Python expression in a string and return an object.

Tuple (s) converts the sequence s into a tuples

List (s) converts sequence s into a list

Chr (x) converts an integer to a character

Unichr (x) converts an integer to a Unicode Character

Ord (x) converts a character into its integer

Hex (x) converts an integer into a hexadecimal string.

Oct (x) converts an integer into an octal string

Python mathematical functions:

Function return value (description)

Abs (x) returns the absolute value of a number, for example, abs (-10) returns 10

Ceil (x) returns the upper integer of the number, for example, math. ceil (4.1) returns 5

Cmp (x, y) returns-1 if x <y, 0 if x = y, and 1 if x> y

Exp (x) returns the x power of e (ex). For example, math. exp (1) returns 2.718281828459045

Fabs (x) returns the absolute value of a number, for example, math. fabs (-10) returns 10.0

Floor (x) returns the rounded down integer. For example, math. floor (4.9) returns 4.

Log (x) such as math. log (math. e) returns 1.0, math. log (2.0) returns

Log10 (x) returns the logarithm of x based on 10. For example, math. log10 (100) returns 2.0.

Max (x1, x2,...) returns the maximum value of a given parameter, which can be a sequence.

Min (x1, x2,...) returns the minimum value of a given parameter, which can be a sequence.

Modf (x) returns the integer and decimal parts of x. The numerical symbols of the two parts are the same as those of x, and the integer part is expressed as a floating point.

The value after the pow (x, y) x ** y operation.

Round (x [, n]) returns the rounding value of floating point x. If n is given, it indicates the number of digits rounded to the decimal point.

Sqrt (x) returns the square root of the number x. The number can be a negative number and the return type is a real number. For example, math. sqrt (4) returns 2 + 0j

Note:

1. Many python mathematical functions cannot be directly accessed. You need to import the math module and call this method through static objects.
Since you are not sure which method is required, it is best to introduce the math module when using python mathematical functions in the future.

2. mathematical functions that can be accessed directly:

Abs (x) returns the absolute value of a number, for example, abs (-10) returns 10cmp (x, y) If x <y returns-1, if x = y returns 0, if x> y returns 1max (x1, x2 ,...) returns the maximum value of a given parameter, which can be a sequence. Min (x1, x2,...) returns the minimum value of a given parameter, which can be a sequence. Round (x [, n]) returns the rounding value of floating point x. If n is given, it indicates the number of digits rounded to the decimal point.

Instance:

#! /Usr/bin/python # coding: uft-8import math # import math module print "max (80,100,100 0):", max (80,100,100 0) print "min (80,100,100 0 ):", min (80,100,100 0) print "round (80.23456, 2):", round (80.23456, 2) print "math. exp (-45.17): ", math. exp (-45.17) print "math. pow (100, 2): ", math. pow (100, 2)

Python random number function:

Function Description

Choice (seq) randomly selects an element from the element of the sequence, such as random. choice (range (10), and randomly selects an integer from 0 to 9.

Randrange ([start,] stop [, step]) obtains a random number from a set in the specified range that increments by the specified base number. The default value of the base number is 1.

Random () randomly generates the next real number within the range of [0, 1.

Seed ([x]) changes the seed of the random number generator.

Shuffle (lst) sorts all elements of the sequence randomly.

Uniform (x, y) randomly generates the next real number within the range of [x, y.

Note:

1. the random number function of python cannot be directly accessed. You need to import the random module and call this method through the random static object.

Instance:

#! /Usr/bin/python #-*-coding: UTF-8-*-import randomprint "choice ([1, 2, 3, 5, 9]):", random. choice ([1, 2, 3, 5, 9]) # output an even number between 100 <= number <1000 "randrange (100,100 0, 2):", random. randrange (100,100 0, 2) # generate the first random Number print "random ():", random. random () # generate the same random number random. seed (10) print "Random number with seed 10:", random. random () list = [20, 16, 10, 5]; random. shuffle (list) print "random sorting list:", listprint "random Number of uniform (5, 10):", random. uniform (5, 10)

Python trigonometric function:

Function Description

Acos (x) returns the arc cosine radians of x.

Asin (x) returns the arc sine radians of x.

Atan (x) returns the arc tangent radians of x.

Atan2 (y, x) returns the arc tangent of the given X and Y coordinate values.

Cos (x) returns the cosine of the radians of x.

Hypot (x, y) returns the euclidean norm sqrt (x * x + y * y ).

Returns the sine of x radians returned by sin (x.

Tan (x) returns the tangent of x radians.

Degrees (x) converts radians to degrees. For example, degrees (math. pi/2) returns 90.0

Radians (x) converts degrees to radians

Note:

1. Python trigonometric functions cannot be directly accessed. You need to import the math module and call this method through the math static object.

Instance:

#!/usr/bin/python#coding: UTF-8import mathprint "degrees(3) : ", math.degrees(3)print "radians(-3) : ", math.radians(-3)print "sin(3) : ", math.sin(3)print "cos(3) : ", math.cos(3)print "tan(3) : ", math.tan(3)print "acos(0.64) : ", math.acos(0.64)print "asin(0.64) : ", math.asin(0.64)print "atan(0.64) : ", math.atan(0.64)print "atan2(-0.50,-0.50) : ", math.atan2(-0.50,-0.50)print "hypot(0, 2) : ", math.hypot(0, 2)

Python mathematical constants:

Constant description
Pi mathematical constant pi (circumference rate, usually expressed as π)
E mathematical constant e, e is the natural constant (natural constant ).

Note:

1. Python mathematical constants cannot be directly accessed. You need to import the math module and then access it through the math static object.

Instance:

#!/usr/bin/python#coding: UTF-8import mathprint math.piprint math.e

Summary

The above is all the content of this article. I hope the content of this article will be helpful for everyone to learn or use python. If you have any questions, please leave a message.

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.