Detailed Python numerical type variable and its method _python

Source: Internet
Author: User
Tags abs acos asin constant hypot shuffle sin square root

Objective

Python data types are not allowed to change, which means that if you change the value of the Number data type, the memory space will be reassigned. The following words do not say more, to see the detailed introduction bar.

The following instance will be created when a variable is assigned a value:

var1 = 1
var2 = 10

You can also use the DEL statement to delete some number object references.

You can delete single or multiple objects by using the DEL statement, for example:

del var
del var_a, Var_b

Python supports four different numeric types:

Integer (INT)-is often referred to as an integral or integer, is a positive or negative integer, with no decimal points.

Long integer-an infinite number of integers, and an integer that is the last uppercase or lowercase l, for example: 51924361L.

Floating-point (float)-floating-point types are made up of integer and fractional parts, and floating-point types can also be represented by scientific notation
(2.5e2 = 2.5 x 10^2 = 250)

Complex numbers (complex)-complex numbers are made up of real parts and imaginary parts, which can be represented by a + BJ, or complex (A,B),
Real part A and imaginary part B of a complex number are floating-point types.

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 complex number

STR (x) converts an object x to a string

REPR (x) converts an object x to an expression string

Eval (str) is used to compute a valid Python expression in a string and returns an object

Tuple (s) converts the sequence s to a tuple

List (s) converts sequence s to a list

Chr (x) converts an integer to a character

UNICHR (x) converts an integer to a Unicode character

Ord (x) converts a character to its integer value

Hex (x) converts an integer to a hexadecimal string

Oct (x) converts an integer to a octal string

Python math function:

function return value (description)

ABS (x) returns the absolute value of the number, such as ABS (-10) return 10

Ceil (x) returns the upper integer of a number, such as Math.ceil (4.1) Returning 5

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

EXP (x) returns the X Power (ex) of E, as MATH.EXP (1) returns 2.718281828459045

Fabs (x) returns the absolute value of the number, as Math.fabs (-10) returns 10.0

Floor (x) returns the lower integer of the number, as Math.floor (4.9) returns 4

Log (x), such as Math.log (MATH.E) return 1.0,math.log (100,10) return 2.0

LOG10 (x) returns the logarithm of x with a base of 10, such as MATH.LOG10 (100), returning 2.0

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

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

MODF (x) returns the integer part of x and the decimal part, and the numeric symbol in two parts is the same as x, and the integral part is represented by a floating-point type.

Pow (x, y) x**y the value after the operation.

Round (x [, N]) returns the rounded value of the floating-point number x, such as the value of N, which represents the digits rounded to the decimal point.

SQRT (x) returns the square root of the number x, the number can be negative, and the return type is real, as MATH.SQRT (4) returns 2+0J

Attention:

1, a lot of Python mathematical functions can not be directly accessed, you need to import the math module, through the static object call the method.
Because you're not sure which method is needed, it's a good idea to introduce the math module later in the Python math function.

2, direct access to the mathematical functions:

ABS (x)  returns the absolute value of the number, such as ABS (-10) back to ten
cmp (x, y) if x < y returns 1, if x = = y returns 0, if x > y returns 1
max (x1, x2,...) Returns the maximum value of the given parameter, which can be a sequence.
min (x1, x2,...) returns the minimum value of the given parameter, which can be a sequence.
round (x [, N]) returns the rounded value of the floating-point number x, such as the value of N, which represents the digits rounded to the decimal point.

Instance:

#!/usr/bin/python
#coding: uft-8
Import Math # importing Math module

print "Max (1000):", Max (MB, 1000) 
   
    print "min (1000):", min (MB, 1000)
print "Round (80.23456, 2):", Round (80.23456, 2)
print "MATH.E XP ( -45.17): ", Math.exp ( -45.17)
print" Math.pow (2): ", Math.pow (100, 2)
   

Python Random number function:

Function description

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

Randrange ([Start,] stop [, step]) gets a random number from the specified range, in a collection that is incremented by the specified cardinality, with a cardinality default of 1

Random () randomly generates the next real number, which is in the range of [0,1].

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

Shuffle (LST) sorts all the elements of a sequence randomly

Uniform (x, y) randomly generates the next real number, which is within the [X,y] range.

Attention:

1, Python's random number function is not directly accessible, you need to import the random module, and then through the random static object call the method.

Instance:

#!/usr/bin/python
#-*-coding:utf-8-*-
import random

print "Choice ([1, 2, 3, 5, 9]):", Random.choice ([1, 2, 3, 5, 9])
# output <= number < 1000 even
print "Randrange (M, 1000, 2):", Random.randrange (100, 1000, 2)
# Generate the first random number
print "random ():", Random.random ()
# generates the same random number
random.seed
print ' random Number with seed: ", random.random ()
list = [5];
Random.shuffle (list)
print "Random sorted list:", List
print "Uniform (5, 10) The random number is:", Random.uniform (5, 10)

Python Trigonometric functions:

Function description

ACOs (x) returns the inverse cosine radian value of x.

ASIN (x) returns the sine-arc value of X.

Atan (x) returns the tangent radian value of x.

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

COS (x) returns the cosine of the radian of X.

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

The sine value of the x Radian returned by sin (x).

Tan (x) returns the tangent of x radians.

Degrees (x) converts radians to angles, such as degrees (MATH.PI/2), and returns 90.0

radians (x) converts an angle to radians

Attention:

1, Python trigonometric functions are not directly accessible, you need to import the math module, and then call the method through the math static object.

Instance:

#!/usr/bin/python
#coding: UTF-8
import math

print 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): ", M Ath.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.h Ypot (0, 2)

Python Math constants:

Constant description
Pi Mathematical constant Pi (pi, usually represented by PI)
e Mathematical constant e,e is the natural constant (natural constant).

Attention:

1, Python math constants are also not directly accessible, you need to import the math module, and then through the math static object access.

Instance:

#!/usr/bin/python
#coding: UTF-8
import Math

print Math.PI
print MATH.E

Summarize

The above is the entire content of this article, I hope this article content for you to learn or use Python can help, if you have questions you can message exchange.

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.