c++转python知识小记之一

来源:互联网 发布:php长连接框架 编辑:程序博客网 时间:2024/06/08 14:22
# -*- coding: utf-8 -*- #utf-8支持中文编码 words=['cat','dog','chicken']for w in words[:]:  #words[:]复制了原本的list    words.insert(0, w)print words a = range(0,10,4)print aargs=[3,10,3]print range(*args)#[3, 6, 9]#我们还可以把range的argument储存在list或tuple中 def f(a, L=[]):    L.append(a)    return L print f(1)print f(2)print f(3)[1][1, 2][1, 2, 3]'''函数形参的默认值只初始化第一次,即这是静态变量'''def a(a=0):    a=a+1    print a a()a()'''但是这里面显示的只是1因为python 中的 mutable object 是list,dictionary,instances'''def aa(*args,**keys):    for a in args:        print a    for d in keys:        print d, ':', keys[d]      aa(1,2,3,a=1,b=2,c=3)#类似cpp的*arg 加*表示它会接受arbitrary个arg,**表示接受arbitrary个dict def parrot(voltage, state='a stiff', action='voom'):    print "-- This parrot wouldn't", action,    print "if you put", voltage, "volts through it.",    print "E's", state, "!"  d = {"state": "bleedin' demised","voltage": "four million", "action": "VOOM"}parrot(**d)#-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !#我们可以把参数保存在字典中,如果key与argument对应的化.  f=lambda a,b,c:a*b+cprint f(2,3,5)#输入前面的argument, 返回后面的值 #字典树user={}user['cs']={}user[2]={}user['cs']['bo']=1user['cs']['co']=2user[2][1]=1print user.keys()print user.values()print user['cs'].values()print user['cs'].keys()

0 0