Chapter II digital type

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)) # generates a random number
randMat = MAT (random.rand (4,4 &))
Print (randMat) # conversion matrix
invRandMat = randMat.I # achieve matrix inversion
Print (randMat invRandMat ) # matrix and an inverse matrix multiplication result
myEye = randMat
invRandMat
Print (myEye - Eye (. 4)) # Eye (. 4) to create a unit matrix
'' '
Print (int (3.14)). 3 #
Print (a float (. 3)) 3.0 #
Print (STR ( 'A \ TB')) # ab &
Print (the 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}' the format (NUM)) # 0.33
Print (2. 1 == <. 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

The localgetcontext decimal decimal precision may be temporarily provided, in context manager

with decimal.localcontext() as ctx:

ctx.prec = 4

For temporary use

the fractions are Import
Print (fractions.Fraction (2.5)) is converted to a decimal fraction # 5/2
Print ((2.5) .as_integer_ratio ()) # 5,2

spliting()

x = set ( 'abcde') # five elements
Y = SET ( 'bdxyz')
Print (X) {# 'A', 'B', 'C', 'D', 'E'}
Print (XY ) # { 'a', 'C', 'E'}
a = { 'WER'} # has an element
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 ( 'from spam')
Print (Z) {# 'from 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

Guess you like

Origin blog.51cto.com/14418623/2416007