【Python編程:從入門到實踐】chapter4 巨集指令清單

來源:互聯網
上載者:User

標籤:自動   for迴圈   3.2   car   修改   避免   簡單的   表頭   操作   

 chapter4 巨集指令清單

4.1 遍曆整個列表
  magicians=[‘alice‘,‘david‘,‘carolina‘]
  for magician in magicians:
  print(magician)
4.1.1 深入地研究迴圈
4.1.2 在for迴圈中執行更多的操作
4.1.3 在for迴圈結束後執行一些操作
4.2 避免縮排錯誤
4.3 建立數值列表
4.3.1 使用函數range()
  Python函數rang()讓你能夠輕鬆地產生一系列的數字。
  for value in range(1,5):
  print(value)
4.3.2 使用range()建立數字列表
  numbers = list(range(1,6))
  print(numbers)

  使用函數range()時,還可以指定步長。例如,下面的代碼列印1~10內的偶數:
  event_numbers = list(range(2,11,2))
  print(event_number);
  squares = []
  for value in range(1,11)
  squares.append(value**2)
4.3.3 對數字列表執行簡單的統計計算
  digits = [1,2,3,4,5,6,7,8,9,0]
  min(digits)
  max(digits)
  sum(digits)
4.3.4 列表解析
  squares = [value**2 for value in range(1,11)]
  print(squares)
4.4 使用列表的一部分
  處理列表的部分元素-Python稱之為切片
4.4.1 切片
  要建立切片,可指定要使用的第一元素和最後一個元素的索引。
  players = [‘once‘,‘tow‘,‘treee‘]
  print(players[0:2])//從索引0開始,到索引2單不包括2
  輸出:[‘one‘,‘tow‘]

  如果你沒有指定第一個索引,Python將自動從列表頭開始:
  print(players[:3]//從頭到索引3單不包括3
  player[2:]//從2到結尾包括2

  無論列表多長,這種文法都能夠讓你輸出從特定位置到列表末尾IDE所有元素。負數索引返回離列表末尾相應距離的元素。
  eg:擷取輸出名單的最後三個隊員:players[-3:]
4.4.2 遍曆切片
4.4.3 複製列表
  my_foods = [‘pizza‘,‘falafel‘,‘carrot cake‘]
  friend_foods = my_foods[:]
4.5 元組
列表時可以修改的,不可變的列表被稱為元組。
4.5.1定義元組
  dimensions = (200,50)
  print(dimensions[0])
4.5.2 遍曆元組中的所有值
  for dimension in dimensions:
  print(demension)
4.5.3 修改元組變數
  dimensions = (200,50)
  dimensions = (200,220)
4.6 設定代碼格式

【Python編程:從入門到實踐】chapter4 巨集指令清單

聯繫我們

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