python基礎操作_方法(函數)

來源:互聯網
上載者:User

標籤:參數   法拉利   *args   name   字典   局部變數   dir   傳回值   預設   

#函數,方法
#普通方法
def hello():
print(‘hello‘)
hello()
#帶形參的方法
def hello1(name):
print(‘hello%s‘%name)
hello1(‘布拉德皮特‘)
#多個參數的方法
def hello2(name1,name2):
print(‘hello%s和%s‘%(name1,name2))
hello2(‘布拉德皮特‘,‘dirk‘)
#帶預設值參數的方法
def hello3(name1,name2,name3=‘法拉利‘):
print(‘hello%s%s%s‘%(name1,name2,name3))
hello3(‘皮特‘,‘dirk‘,)
#可變參數的方法
def hello4(*args):
print(args)
hello4(1,2,3,4)
#關鍵字參數,key v 接受進來是一個字典
def hello5 (**kwargs):
print(kwargs)
hello5(name=‘qiao‘,age=18)
a={‘name‘:‘q‘,‘age‘:12}
hello5(**a)

#關鍵字傳值
#關鍵字調用參數不用排序。因為指定了關鍵字
def hello6(name,age):
print(name,age)
hello6(age=1,name=‘qiao‘)
#函數 方法的傳回值
def plus(num1,num2):
return num1,num2,num1+num2#遇到return立即結束,後面的代碼不走了
print(plus(10,2))
a=plus(12,3)
print(a[2])#傳回值是個元組
#函數沒有傳回值時,返回的是None
#傳什麼類型,返回什麼類型

#局部變數,全域變數
#如果在函數裡修改全域變數,需要用先聲明,用global聲明

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.