s=16 print(bin(s)) print(oct(s)) print(hex(s)) 运行结果: 0b10000 0o20 0x10
print('{0:o},{1:x},{2:b}'.format(16,16,16))
print('%o,%x,%X'%(16,16,16))
运行结果:
20,10,10000
20,10,10 a=int('0b10000',2)
b=int('0o20',8)
c=int('0x10',16)
print(a)
print(b)
print(c)
运行结果:
16
16
16 print(eval('16'))
print(eval('0b10000'))
print(eval('0o20'))
print(eval('0x10'))
运行结果:
16
16
16
16 python2 a = 1 > 'a' print a 运行结果: False
python3<br>a=1 > 'a' print(a) 运行结果: Traceback (most recent call last): File "C:/Users/jeff/PycharmProjects/python_file/practice/prac2.py", line 92, in <module> a=1 > 'a' TypeError: unorderable types: int() > str()
print(1+0.2) 运行结果: 1.2
在命令行中操作
>>>num = 1 / 3.0
>>>num
0.333333333333333333331
在pycharm中print操作
num = 1/3.0
print(num)
运行结果:
0.3333333333333333
num = 1/3.0
print('{0:4.2f}'.format(num))#4是前面空格格数,2是保留小数位
运行结果:
0.33 >>>num = 1/3.0 >>>repr(num) 0.333333333333333333331 >>>str(num) 0.3333333333333333
x=1 print(x<<2) print(x|2) print(x&2) print(x^2) 运行结果: 3 3
x=99 print(bin(x)) print(x.bit_length()) print(len(bin(x))-2) 运行结果: 0b1100011 7 7
import math print(math.pi) print(math.e) print(math.sin(110)) print(math.sqrt(144)) print(pow(2,3)) print(abs(-50)) print(sum((1,2,3))) print(max(1,2,3)) print(min(1,2,3)) 运行结果: 3.141592653589793 2.718281828459045 -0.044242678085070965 12.0 8 50 6 3 1
print(math.floor(2.577)) print(math.trunc(2.577)) print(round(2.577)) print(int(2.577)) 运行结果: 2 2 3 2
import random print(random.random()) print(random.randint(1,100)) 运行结果: 0.9534845221467178 79
print(0.1+0.1+0.1-0.3)输出结果: 5.551115123125783e-17
from decimal import Decimal
print(Decimal('0.1')+Decimal('0.10')+Decimal('0.10')-Decimal('0.30'))
运行结果:
0.00 from decimal import Decimal print(Decimal.from_float(1.88)) print(Decimal.from_float(1.25))输出结果: 1.87999999999999989341858963598497211933135986328125 1.25
from fractions import Fraction x=Fraction(1,3) y=Fraction(2,3) print(x+y)输出结果: 1
x={'a','c','b'}
y={'a','g','b'}
z={'a'}
print('a' in x)
print(x-y)
print(x|y)
print(x&y)
print(x^y)
print(z<y) x={'a','c','b'}
y={'a','g','b'}
z={'a'}
print(x.intersection(y))
print(x.union(y))
x.add('s')
print(x)
print(x.pop())
x.update({'w','e','o'})
print(x)
print(x)
运行结果:
{'a', 'b'}
{'c', 'a', 'b', 'g'}
{'a', 'b', 'c', 's'}
a
{'o', 'c', 's', 'w', 'b', 'e'}
{'o', 'c', 's', 'w', 'b', 'e'} x={'a','c','b'}
l=[1,2,3]
x.add(l)
print(x)
运行结果:
Traceback (most recent call last):
File "C:/Users/jeff/PycharmProjects/python_file/practice/prac2.py", line 111, in <module>
print(x.add(l))
TypeError: unhashable type: 'list' x={'a','c','b'}
l=(1,2,3)
x.add(l)
print(x)
运行结果:
{'c', 'b', 'a', (1, 2, 3)} x={1,2,3}
print({i ** 2 for i in x})
运行结果:
{1, 9, 4} print(type(True)) print(True == 1) print(True is 1) print(True + 1) 运行结果: <class 'bool'> True False 2
集合和bool值,还是比较常见的类型,在基础学习里也有涉及,在这里就不多写了。
python中的数字在程序编写时广泛使用,今后还会更深层次的学习python的扩展库。
以上就是python的数字类型的详细内容,更多请关注Gxl网其它相关文章!
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did81830