Python基礎點記錄2

來源:互聯網
上載者:User

標籤:python基礎   class   rom   記錄   res   min   div   接收   用法   

---- PygLatin 1 介紹函數的調用,就是直接函數名

  def square(n):

      squared = n**2

      print "%d squared is %d." % (n, squared)

      return squared

  square(10)

2 介紹函數可以使用參數的傳遞

  把函數的參數改為base,exponent。調用函數傳入37和4

  def power(base,exponent):

    result = base**exponent

    print "%d to the power of %d is %d." % (base, exponent, result)

  power(37,4)

3 介紹*的用法,比如我們傳入一個字串,那麼我們可以使用*name接收,然後可以利用name來輸出。不一定使用name,可以是任何的名字

  def favorite_actors(*name): 

        print "Your favorite actors are:" , name      

  favorite_actors("Michael Palin", "John Cleese", "Graham Chapman")  

4 介紹函數體裡面還可以調用另外的函數

  def fun_one(n):

      return n * 5

  def fun_two(m):

      return fun_one(m) + 7

5 介紹Python裡面可以匯入很多的系統的模組,就像 c語言 的include

     1 匯入math模組,import math

    Ps:假設我們沒有匯入math模組的時候,那麼執行print sqrt(25)的時候會報錯

     2 執行print math.sqrt(25),加了一個math說明調用系統的庫函數

    import math

    print math.sqrt(25)

6 Impor t 我們還可以只是單獨的匯入模組裡面的方法

  從math模組裡面值匯入sqrt函數

  from math import sqrt 

  print sqrt(25)

7   Import  使用from module import *   來表示從模組裡面匯入所有的函數 8 Import  from module import *方法的缺點就是,如果我們匯入了很多的模組,那麼可能導致出現相同的函數名,因此我們最好是使用import module,然後使用module.name

  1 介紹了第一個函數max(),比如max(1,2,3)返回的是3 (min函數是類似的)

  2 max()函數的參數是一個數組,返回數組中的最大值

  3 使用max函數來得到一個數組的最大值

  maximum = max(4,0,-3,78)  

  print maximum

9 介紹 絕對值 函數abs() 返回的值永遠是正數,比如abs(-5)返回的是5 

  absolute = abs(-42)  

  print absolute  

10 介紹type函數的使用,type函數返回的是當前這種資料的類型,比如int , float等

Python基礎點記錄2

聯繫我們

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