Detailed explanation of python's numeric type variables and their methods

黄舟
Release: 2016-12-13 11:24:02
Original
1222 people have browsed it

Foreword

Python data types are not allowed to be changed, which means that if the value of the Number data type is changed, the memory space will be reallocated. Not much to say below, let’s take a look at the detailed introduction.

The following examples of Number objects will be created when variables are assigned:

var1 = 1
var2 = 10
Copy after login

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
Copy after login

Python supports four different numeric types:

Integer (Int) - often called an integer or integer, is a positive or Negative integer without decimal point.

Long integer (long) - an integer of infinite size. The integer ends with an uppercase or lowercase L, such as: 51924361L.

Floating point type (float) - The floating point type consists of an integer part and a decimal part. The floating point type can also be expressed using scientific notation
                 (2.5e2 = 2.5 x 10^2 = 250)

Complex number (complex) - A complex number consists of a real part and an imaginary part, you can use a + bj, or complex(a,b) means that the real part a and the imaginary part b of the complex number are both floating point types.

Python Number type conversion:

int(x [,base ]) Convert x to an integer


long(x [,base ]) Convert x to a long integer


float(x ) Convert x Convert to a float


complex(real [,imag ]) Create a complex number


str(x ) Convert object x to string


repr(x ) Convert object x to expression string


eval(str ) Used to evaluate a valid Python expression in a string and return an object


tuple(s ) Convert sequence s to a tuple


list(s ) Convert sequence s to A list


chr(x ) Convert an integer to a character


unichr(x ) Convert an integer to a Unicode character


ord(x ) Convert a character to its integer value


hex (x ) Convert an integer to a hexadecimal string


oct(x ) Convert an integer to an octal string

Python math functions:

function Return value (description)


abs( x) Returns the absolute value of the number, such as abs(-10) Returns 10


ceil(x) Returns the upward integer of the number, such as math.ceil(4.1) Returns 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 of e (ex), such as math.exp(1) returns 2.718281828459045


fabs(x) Returns the absolute value of the number, such as math.fabs(-10) returns 10.0


floor(x) Returns the rounded integer of the number, such as math.floor(4.9) returns 4


log(x ) For example, math.log(math.e) returns 1.0, math.log(100,10) returns 2.0


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 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 and decimal part of x. The numerical signs of the two parts are the same as x, and the integer part is expressed in floating point type.


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


round(x [,n]) Returns the rounded value of the floating point number x. If the n value is given, it represents the number of digits rounded to the decimal point.


sqrt(x) Returns the square root of the number To access, you need to import the math module and call this method through a static object.

Since I am not sure which method is required, it is best to introduce the math module when using python math functions in the future.


2. Mathematical functions that can be directly accessed:

abs(x)  返回数字的绝对值,如abs(-10) 返回 10
cmp(x, y) 如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1
max(x1, x2,...) 返回给定参数的最大值,参数可以为序列。 
min(x1, x2,...) 返回给定参数的最小值,参数可以为序列。 
round(x [,n]) 返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。
Copy after login

Example:

#!/usr/bin/python 
#coding:uft-8 import math # 导入 math 模块   
print "max(80, 100, 1000) : ", max(80, 100, 1000) 
print "min(80, 100, 1000) : ", min(80, 100, 1000) 
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)
Copy after login

Python random number function:

Function Description

choice(seq) 从序列的元素中随机挑选一个元素,比如random.choice(range(10)),从0到9中随机挑选一个整数。

randrange ([start,] stop [,step]) 从指定范围内,按指定基数递增的集合中获取一个随机数,基数缺省值为1

random() 随机生成下一个实数,它在[0,1)范围内。

seed([x]) 改变随机数生成器的种子seed。

shuffle(lst) 将序列的所有元素随机排序

uniform(x, y) 随机生成下一个实数,它在[x,y]范围内。

注意:

1、python的随机数函数是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。

实例:

#!/usr/bin/python 
# -*- coding: UTF-8 -*- 
import random   
print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9]) 
# 输出 100 <= number < 1000 间的偶数 
print "randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2) 
# 生成第一个随机数 
print "random() : ", random.random() 
# 生成同一个随机数 random.seed( 10 ) 
print "Random number with seed 10 : ", random.random() 
list = [20, 16, 10, 5]; 
random.shuffle(list) 
print "随机排序列表 : ", list
print "uniform(5, 10) 的随机数为 : ", random.uniform(5, 10)
Copy after login

Python三角函数:

函数 描述

acos(x) 返回x的反余弦弧度值。

asin(x) 返回x的反正弦弧度值。

atan(x) 返回x的反正切弧度值。

atan2(y, x) 返回给定的 X 及 Y 坐标值的反正切值。

cos(x) 返回x的弧度的余弦值。

hypot(x, y) 返回欧几里德范数 sqrt(x*x + y*y)。

sin(x) 返回的x弧度的正弦值。

tan(x) 返回x弧度的正切值。

degrees(x) 将弧度转换为角度,如degrees(math.pi/2) , 返回90.0

radians(x) 将角度转换为弧度

注意:

1、Python三角函数是不能直接访问的,需要导入 math 模块,然后通过 math 静态对象调用该方法。

实例:

#!/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) : ", 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)
Copy after login

Python数学常量:

常量 描述
pi 数学常量 pi(圆周率,一般以π来表示)
e 数学常量 e,e即自然常数(自然常数)。

注意:

1、Python数学常量也是不能直接访问的,需要导入 math 模块,然后通过 math 静态对象访问。

实例:

#!/usr/bin/python 
#coding: UTF-8 
import math   
print math.pi print math.e
Copy after login

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用python能有所帮助,如果有疑问大家可以留言交流。


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template