数字类型

from numpy import *
import operator
def spliting():
print('--'*20)
'''
def greatDataSet():
group = array([(1.0,1.1),(1.0,1.0),(0,0),(0,0.1)])
labels = ['A','A','B','B']
return group, labels

print(random.rand(4,4)) # 生成随机数
randMat = mat(random.rand(4,4))
print(randMat) # 转化为矩阵
invRandMat = randMat.I # 实现矩阵求逆
print(randMat * invRandMat) # 矩阵与逆矩阵相乘的结果
myEye = randMat * invRandMat
print(myEye - eye(4)) # eye(4)创建单位矩阵
'''
print(int(3.14)) # 3
print(float(3)) # 3.0
print(str('a\tb')) # a b
print(repr('a\tb')) # 'a\tb'
num = 1/3.0
print('%e' % num) # 3.333333e-01
print('%4.2f' % num) # 0.33
print('{0:4.2f}'.format(num)) # 0.33
print(1 == 2 < 3) # False
print(1 == 1 < 3) # True

from future import devision

print(10/4) # 2.5
print(10//4) # 2
print(10/4.0) # 2.5
print(10//4.0) # 2.0
import math
print(math.floor(4.2)) # 4
print(math.floor(4.6)) # 4
print(math.floor(-4.6)) # -5
print(math.floor(-4.2)) # -5
print(math.trunc(4.2)) # 4
print(math.trunc(4.6)) # 4
print(math.trunc(-4.6)) # -4
print(math.trunc(-4.2)) # -4
print(round(4.2)) # 4
print(round(4.6)) # 5
print(round(-4.2)) # -4
print(round(-4.6)) # -5

spliting()

print(0o1,0o20) # 1,16
print(0x01,0x10) # 1,16
print(0b01,0b10000) # 1,16
print(oct(64)) # 0o100
print(bin(64)) # 0b1000000
print(hex(64)) # 0x40
print(int('64')) # 64
print(int('100',8)) # 64
print(int('1000000',2))# 64
print(eval('64')) # 64
print(eval('0o100')) # 64
print(eval('0x40')) # 64
print('{0:o},{1:x},{2:b}'.format(64,64,64)) # 100,40,1000000
print('%x %o %X' %(255,64,255))
x = 1
print(bin(x)) # 0b1
print(bin(2)) # 0b10
print(x<<2,bin(x<<2)) # 4, 0b100
print(x | 2 ,bin(x | 2)) # 3 0b11
print(x & 2,bin(x & 2)) # 0 0b0
x = 0xff
print(bin(x)) # 0b11111111
print(x^0b10101010) # 85
print(bin(x^0b10101010)) # 0b1010101
print(int('1010101',2)) # 85
print(hex(85)) # 0x55
x = 99
print(bin(99),x.bit_length()) # 0b1100011 7
print(bin(256),(256).bit_length()) # 0b100000000 9
print(len(bin(256))) # 11
print(len(bin(99))) # 9
print((99).bit_length()) # 7

spliting()

import math
print(math.pi,math.e) # 3.141592653589793 2.718281828459045
print(math.sin(2 * math.pi /180)) # 0.03489949670250097
print(math.sqrt(144),math.sqrt(2)) # 12.0 1.4142135623730951
print(pow(2,4),2**4)# 16 16
print(abs(-45.4),sum((3,4,5))) # 45.4 12 sum作用一个数字序列
print(min(1,3,4,2),max(3,1,4,2)) # 1 4
print(0.1+0.1+0.1-0.3) # 5.551115123125783e-17
import decimal
print(decimal.Decimal('0.1')+decimal.Decimal('0.1')+decimal.Decimal('0.1')-decimal.Decimal('0.3')) # 0.0
print(decimal.Decimal(1)/decimal.Decimal(7)) # 0.1428571428571428571428571429
decimal.getcontext().prec = 2
print(decimal.Decimal(1)/decimal.Decimal(7)) # 0.14
with decimal.localcontext() as ctx:
ctx.prec = 4
print(decimal.Decimal(1)/decimal.Decimal(7)) # 0.1429
print(decimal.Decimal(1)/decimal.Decimal(7)) # 0.14

decimal 的localgetcontext可以临时设置小数的精度,结合上下文管理器

with decimal.localcontext() as ctx:

ctx.prec = 4

来临时使用

import fractions
print(fractions.Fraction(2.5)) # 小数转化为分数 5/2
print((2.5).as_integer_ratio()) # 5,2

spliting()

x = set('abcde') # 有五个元素
y = set('bdxyz')
print(x) # {'a', 'b', 'c', 'd', 'e'}
print(x-y) # {'a', 'c', 'e'}
a = {'wer'} # 有一个元素
print(x | y) # {'e', 'd', 'a', 'x', 'y', 'b', 'z', 'c'}
print(x & y) # {'b', 'd'}
print(x^y) # {'e', 'z', 'y', 'a', 'x', 'c'}
print(x > y , x < y) # False False
z =(x.intersection(y))
print(z) # {'d', 'b'}
z.add('spam')
print(z) # {'spam', 'd', 'b'}
z.update(set(('x','y')))
print(z) # {'b', 'y', 'x', 'spam', 'd'}
z.remove('b')
print(z) # {'spam', 'x', 'd', 'y'}
s = set([1,2,3])
print(s.union(set([4,5]))) # {1, 2, 3, 4, 5}
print(s.issubset(range(-5,5))) # True

spliting()

s = {1.23}

print(s.add([1,2,3])) error

print(s.add({1:34})) error

s.add((1.24))
print(s) # {1.23, 1.24}
l = [1,1,2,3,4,5,5,6]
print(list(set(l))) # [1, 2, 3, 4, 5, 6]
a = {1,2,3,4}
b = {2,3,5,6}
print(2 in a) # True
print(a & b) # {2, 3}
print(a.intersection(b)) # {2, 3}
print(a | b) # {1, 2, 3, 4, 5, 6}
print(a.union(b)) # {1, 2, 3, 4, 5, 6}
print(a-b) # {1, 4}
print(a.difference(b)) # {1, 4}
print(a^b) # {1, 4, 5, 6}
print(a.symmetric_difference(b)) # {1, 4, 5, 6}

spliting()

print(type(True)) # <class 'bool'>
print(isinstance(True,int)) # True
print(True + 1) # 2

posted on 2019-07-04 08:28  幻嘤剑舞  阅读(134)  评论(0编辑  收藏  举报

导航