色彩时光 | 记录程序员世界的点点滴滴

python学习日志_数字


python学习手册第五章_数字,里面的除法,真除法,数字截断,集合的添加,去重,加减,异或操作,随机模块等。



# -*- coding: cp936 -*-
import math
import sys
print sys.version

print "===除法==="
print "7/3.0:",7/3.0
print "7/3:",7/3
print "7//3.0:",7//3.0

print "===小数截取==="
v=1.2345
print "round(%s,2):"%v,round(v,2)
print "'%.2f'%v:",'%.2f'%v
print '{0:.2f}'.format(v)

#其他的内置数学工具
print "===其他的内置数学工具==="
print "data is  2.56 -2.56"
print "floor:",math.floor(2.56),math.floor(-2.56)
print "trunc:",math.trunc(2.56),math.trunc(-2.56)
print "int:",int(2.56),int(-2.56)
print "round",round(2.56),round(-2.56)

print "===平方根计算==="
print "data is 144"
print "sqrt:",math.sqrt(144)
print "**.5:",144**.5
print "pow:",pow(144,0.5)
print "7//3:",7//3

print "===随机模块==="
import random
print "random:",random.random()
print "randint:",random.randint(1,10)
print "choice:",random.choice(["www","rgbtime","licai528","com"])

print "===集合==="
x=set('abcdef')
y=set('bdxyz')
print "集合x:",x,"集合y:",y
print "e in x",'e' in x
print "x-y",x-y,"x:",x,"y:",y
print "x|y",x|y
print "x&y",x&y
print "x^y",x^y
print "x<y,x>y",x<y,x>y
print "===集合遍历==="
for item in x:
    print item*3
l=[1,2,3,2,4,3,5]
l1=list(set(l))
print "集合的作用去重 %s,%s"%(l,l1)

print "===布尔===="
print "type(True):",type(True)
print "isinstance(True,int):",isinstance(True,int)
print "True==1:",True==1
print "True is 1:",True is 1
print "True or False:",True or False
print "True+4:",True+4

python学习日志_列表解析


您可能也对下面文章感兴趣:

Write a Comment


* Content (required) 10~500s

分类

热门标签

友情链接