c++轉python知識小記之一

來源:互聯網
上載者:User

標籤: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 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()

c++轉python知識小記之一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.