您的位置:首页 > 编程语言 > Python开发

python学习记录 — (6)数字

2018-02-01 22:11 148 查看
Numbers.py

#coding=utf-8

#################################### 数字类型 ####################################
"""
改变数据类型,意味着重新分配内存空间;
Numbers是不可更改对象,改变其值,相当于换个引用;
"""

#### 整形(Int)
intVar = 10
print type(intVar),id(intVar)                                   ## <type 'int'> 14646908
intVar = -33
print type(intVar),id(intVar)                                   ## <type 'int'> 42826200

#### 长整形(long integers)
longVar = 123L
print type(longVar),id(longVar)                                 ## <type 'long'> 57643104

#### 浮点型(float)
floatVar = 3.3
print type(floatVar),id(floatVar)                               ## <type 'float'> 55181616

#### 复数(complex)
cplxVar = 1+2j
print type(cplxVar),id(cplxVar)                                 ## <type 'complex'> 56181008
cplxVar = complex(1,2)
print(cplxVar)                                                  ## (1+2j)

#### Numbers类型转换
newInt = int("123")
print type(newInt),newInt                                       ## <type 'int'> 123
newLong = long(intVar)
print type(newLong),newLong                                     ## <type 'long'> -33
newFloat = float(newInt)
print type(newFloat),newFloat                                   ## <type 'float'> 123.0
newCplx = complex(newInt,newLong)
print type(newCplx),newCplx                                     ## <type 'complex'> (123-33j)
newStr = str(newCplx)
print type(newStr),newStr                                       ## <type 'str'> (123-33j)
newStr = repr(newLong)
print type(newStr),newStr                                       ## <type 'str'> -33L
val = eval("1+2+3+4+5")
prin
4000
t type(val),val                                             ## <type 'int'> 15
tpl = tuple("abc 123")
print type(tpl),tpl                                             ## <type 'tuple'> ('a', 'b', 'c', ' ', '1', '2', '3')
lst = list("abc 123")
print type(lst),lst                                             ## <type 'list'> ['a', 'b', 'c', ' ', '1', '2', '3']
char = chr(97)
print type(char),char                                           ## <type 'str'> a
uchar = unichr(97)
print type(uchar),uchar                                         ## <type 'unicode'> a
val = ord("a")
print type(val),val                                             ## <type 'int'> 97
hVal = hex(123)
print type(hVal),hVal                                           ## <type 'str'> 0x7b
oVal = oct(123)
print type(oVal),oVal                                           ## <type 'str'> 0173

#################################### 数字函数 ####################################
import math

#### 一般数学函数
## abs(x):绝对值
print(abs(1+1j))                                                ## 1.41421356237
## fabs(x):绝对值(只适用于float,integer)
print math.fabs(-2.5)                                           ## 2.5
## ceil(x):获取上入整数
print(math.ceil(2.1))                                           ## 3.0
print(math.ceil(-2.9))                                          ## -2.0
## floor(x):获取舍整数
print math.floor(2.9)                                           ## 2.0
print math.floor(-2.1)                                          ## -3.0
## round(x[,n]):返回浮点数的四舍五入值(n表示小数位数)
print round(33.486,2)                                           ## 33.49
print round(33.485)                                             ## 33.0
## log(x,base):base为底x的对数(base默认为e)
print math.log(25,5)                                            ## 2.0 (即5的2次方为25)
print math.log(math.e * math.e)                                 ## 2.0
## log10(x):10为底x的对数
print math.log10(100)                                           ## 2.0
## pow(x,y):x**y的值
print pow(2,2),2**2                                             ## 4 4
## sqrt(x):x的平方根
print math.sqrt(25)                                             ## 5.0
## exp(x):e的x次幂
print math.exp(1)                                               ## 2.71828182846
## max(x1,x2...):返回参数中最大的值(参数可以为序列)
print max(2,4,1,22,9)                                           ## 22
## min(x1,x2...):返回参数中最小的值(参数可以为序列)
print min([2,4,1,22,9])                                         ## 1
## modf(x):获取x的整数和小数部分
print math.modf(33.33)                                          ## (0.3299999999999983, 33.0)
## cmp(x,y):x<y 返回-1; x==y 返回0; x>y 返回1
print cmp(1,2),cmp(1,1),cmp(2,1)                                ## -1 0 1

#### 三角函数
## cos(x):x弧度的余弦值
print math.cos(math.pi/3)                                       ## 0.5
print math.cos(math.pi)                                         ## -1
## acos(x):x的反余弦弧度值
print math.acos(0.5)/math.pi*180                                ## 60.0
print math.acos(-1)                                             ## 3.14159265359
## sin(x):x弧度的正弦值
print math.sin(math.pi/3)                                       ## 0.866025403784
print math.sin(0)                                               ## 0.0
## asin(x):x的反正弦弧度值
print math.asin(0.866025403784)/math.pi*180                     ## 59.9999999999
print math.asin(0)                                              ## 0.0
## tan(x):x弧度的正切值
print math.tan(math.pi/3)                                       ## 1.73205080757
## atan(x):x的反正切弧度值
print math.atan(1.73205080757)/math.pi*180                      ## 60.0
## atan2(y,x):给定x和y坐标值的反正切值
print math.atan2(math.sqrt(3)/2.0,1.0/2.0)/math.pi*180          ## 60.0
## hypos(x,y):欧几里范数 sqrt(x*x+y*y)
print math.hypot(3,4)                                           ## 5.0
## degrees(x):弧度转角度
print math.degrees(math.pi/3)                                   ## 60.0
## radians(x):角度转弧度
print math.radians(60.0)                                        ## 1.0471975512

#### 随机数
import random
## random():随机生成一个实数(0至1范围)
print random.random()                                           ## 0.705278124462
print random.random()                                           ## 0.523791423616
## randrange([start,]stop[,step]):返回递增计数集合中的一个随机数(基数默认为1)
print random.randrange(0,100,2)                                 ## 16 8 22
print random.randrange(0,100,3)                                 ## 21 21 93
## uniform(x,y):从范围内随机生成一个元素
print random.uniform(50,60)                                     ## 51.6452326583
## choice():从序列中随机挑选一个元素
lst = list("abcdefg")
print random.choice(lst)                                        ## g
print random.choice("abcdefg")                                  ## d
print random.choice(('a','b','c','d'))                          ## c
## seed([[x]]):改变随机数生成器的种子
## shuffle(lst):将序列的所有元素随机排序
lst = list("abcdefg")
print lst                                                       ## ['a', 'b', 'c', 'd', 'e', 'f', 'g']
random.shuffle(lst)
print lst                                                       ## ['d', 'a', 'f', 'c', 'b', 'e', 'g']

#### 数学常量
## 常量π
print math.pi                                                   ## 3.14159265359
## 常量e
print math.e                                                    ## 2.71828182846
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: