python全棧開發14

來源:互聯網
上載者:User

標籤:相關   false   ctrl   其他   推導式   判斷   函數   col   迴圈   

 

迭代器的優點:
  1.節省記憶體
  2.惰性機制
  3.單向,無法復原
如何判斷它是否是可迭代對象和迭代器

  可迭代對象->迭代器

  可迭代對象__iter__():就會變成迭代器
    1.‘__iter__‘in dir(obj):True或Flase,返回True就代表他是可迭代對象
    2.引入模組
      from collections import Iterator(迭代器)
      print(isinstance(參數名,Iterator)
      from collections import Iterable(迭代對象)
      print(isinstance(參數名,Itera)ble
  產生器:
    自己用python代碼寫的迭代器就是產生器
    產生器本身就是迭代器
產生器的運算式和列表推倒式
  1.列表推導式
    [變數(加工後的變數) for 變數 in iterable(迭代對象)] # 迴圈模式
    [變數(加工後的變數) for 變數 in iterable if 條件]# 篩選模式
  2.產生器運算式:和列表運算式一樣只是括弧不一樣

              (變數(加工後的變數) for 變數 in iterable(迭代對象)) # 迴圈模式
    (變數(加工後的變數) for 變數 in iterable if 條件) # 篩選模式

    優點:1.節省代碼
    缺點:不好排錯
    整體:凡是用列表推導式構造的,用其它方式也可以,非常複雜的列表,列表推導式是構造不出來的

內建函數

 

1.1範圍相關
a =33b = 22def func():    a = 1    print(globals(),‘\n‘,locals())func()
1.21其他相關
字串類型代碼的執行 eval,exec,complie
print(eval(‘1*2*3*4*5*6*7*8*9‘))li = """for i in range(10):    print(i)"""exec(li)
1.22輸入輸出相關print 和 input
k = print(1,2,4,6,sep=‘******‘,end=‘‘)with open(‘實驗品.txt‘,‘w‘,encoding=‘utf-8‘) as f:    print(‘111111111111‘,1111,file=f,sep=‘****‘)    print(‘111111111111‘,1111,file=f,sep=‘****‘)    print(‘111111111111‘,1111,file=f,sep=‘****‘)
1.23記憶體相關hash():擷取一個可雜湊對象的雜湊值,id(返回記憶體位址)
print(hash(1020000000000000000))# 數字最多19個才能保證可雜湊值和數值一樣,超過的話就會變成別的print(hash(‘1111111111111111111‘)) # 雜湊字串print(hash((1,2,3,4))) # 雜湊元組print(hash(True)) # 雜湊bool值
1.24檔案開啟和模組相關操作open()和import()函數用於動態載入類和函數 
open:函數用於開啟一個檔案,建立一個 file 對象,相關的方法才可以調用它進行讀寫1.25 help():協助a = 1help(a)#查看協助
1.26 調用相關 callable()能調用返回True 不能調用返回False
按 Ctrl+C 複製代碼按 Ctrl+C 複製代碼

python全棧開發14

聯繫我們

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