python學習day10

來源:互聯網
上載者:User

標籤:blog   常見   ble   gpo   code   class   style   執行函數   turn   

一:  list 產生式

#codeing:UTF-8#__author__:Duke#date:2018/3/9/009# list 產生器a = [x for x in range(10)]print(a)  #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]a = [x*x for x in range(10)]print(a)  #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]def f(n):    return n**3a = [f(x) for x in range(10)]print(a)  #[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]

二:產生器

產生器共兩種建立方法# 一: 小括弧法a = (x*2 for x in range(10))print(a)  #a  為 generator(產生器) objectprint(next(a))  # == print(a.__next__())print(next(a))  # == print(a.__next__())#genator  只能一個一個的接著讀## #產生器本來就是一個可迭代對象(iterable)for i in a:    print(i)#二:  關鍵字法def fun():    print("ok1")    yield  1   #這是next 時的傳回值    print("ok2")    yield 2a = fun()  #此時的fun()是一個產生器對象,不在是一個函數,它不會執行函數中的內容,next(a) # 完成第一個yield  的內容#print(next(a))##   完成第二個yield  的內容  ,先執行next(a)#    會列印OK2  ,並返回2 ,所以列印的 next(a)就是列印傳回值2#什麼是可迭代對象?  對象有 iter  方法的對象就是迭代對象#常見的有  list  tuple  dict  set# a = set("a")# a.__iter__()def  fib( num ):    n,a,b = 0,0,1    while n<=num:        print(b)        a,b = b,a+b        n= n+1fib(6)

 

python學習day10

聯繫我們

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