Python--函數1

來源:互聯網
上載者:User

標籤:put   隨機數   模組   求和   import   gets   代碼塊   pytho   pre   

1概念

   一般,一個小的功能模組,會做成一個函數。

    一般情況下,某段代碼需要反覆使用多次,而且這段代碼又具備特定的功能,我們會把這段代碼

組織成為單獨的功能模組,這個功能模組就可以叫做函數。

2.函數的定義和調用

  定義:函數不能重複定義

   def 函數名():

     功能實現的代碼塊...

 調用:函數名()

    函數可以重複調用一定是先定義,後調用

 1 #用函數列印99乘法表 2 def show99(): 3     i = 1 4     while i <10: 5         j = 1 6         while j <=i: 7             print("%d * %d = %d\t"%(j,i,i*j)) 8             j +=1 9         i += 110         print("")11 show99()

3.函數的參數,傳回值

  參數   

    有參數,無參數

  傳回值

    有傳回值,無傳回值

4. 封裝一個函數,完成兩個數位求和運算

  參數 :2個參數

  傳回值return 和

 

  import random

  random.randint(1,6)

 1 # 返回一個隨機數函數的調用 2 import random 3 #調用random帶有傳回值的函數 4 random = random.randint(1,6) 5  6 #定義一個求兩個數字和的函數 7 def getsum(a,b): 8     #sum1 = a + b 9     #return sum110     return a + b11 num1 = int(input("輸入第一個數"))12 num2 = int(input("輸入第二個數"))13 #調用自訂函數,得到兩個數的和,儲存到resul中14 result = getsum(num1,num2)15 #求num1與result的和16 result1 = getsum(num1,result)17     

 

Python--函數1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.