python基础练习2

来源:互联网 发布:网约车软件下载排行 编辑:程序博客网 时间:2024/06/04 17:49
####函数#调用print abs(-100)print cmp(190,100)#数据类型转换print int("76")print int(12.33)print float("11.2")print str(9.2)print unicode(100)print bool(1)print bool("")####定义函数def my_abs(x):    if not isinstance(x,(int,float)):    raise TypeError("bad operand type")    if x >= 0:    return x    else:    return -x#print my_abs(-65)def nop():    pass###import mathdef move(x,y,step,angle=0):    nx =x+step*math.cos(angle)    ny =y-step*math.sin(angle)    return nx,ny#####返回的其实是一个tuple类型x,y=move(100,100,60,math.pi / 6 )print x,yr = move(100,100,60,math.pi / 6 )print r####def power(x,n=2):    s = 1    while n > 0:        n = n - 1        s = s * x    return s#print power(2),power(2,3)####def enroll(name,gender,age=6,city="Beijing"):    print "name:",name    print "gender:",gender    print "age:",age    print "city:",city#enroll("Rod","M",7)enroll("Tid","M",city="Tianjin")####def add_end(L=[]):    L.append("END")    return L##print add_end([1,2,3])print add_end(["x","y"])print add_end()print add_end()print add_end()print add_end()####def add_end(L=None):    if L is None:        L = []    L.append("END")    return L####print add_end()print add_end()print add_end()####可变参数def calc(*number):    sum = 0     for n in number:        sum = sum + n * n    return sum###print calc(1,2,1)####关键字参数def person(name,age,**kw):    print "name:",name,"age:",age,"other:",kw###print person("meiny",30)print person("Teidy",30,city="D")kw = {"city":"beijing","job":"Engineer"}print person("Yorki",20,city=kw["city"],job=kw["job"])

0 0
原创粉丝点击