码迷,mamicode.com
首页 > 编程语言 > 详细

c++转python知识小记之一

时间:2015-05-09 15:06:19      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:python   lambda   utf-8   c++   

# -*- 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 a
args=[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+c
print f(2,3,5)
#输入前面的argument, 返回后面的值
 

#字典树
user={}
user['cs']={}
user[2]={}
user['cs']['bo']=1
user['cs']['co']=2
user[2][1]=1
print user.keys()
print user.values()
print user['cs'].values()
print user['cs'].keys()

c++转python知识小记之一

标签:python   lambda   utf-8   c++   

原文地址:http://blog.csdn.net/qq_21970857/article/details/45600611

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!