40923235 cp2020

  • Home
    • Site Map
    • reveal
    • blog
  • 首頁
  • HW1
    • UNIT 1
      • Strategies for Learning學習策略
      • What You Need to Know About the Course您需要了解的課程內容
      • About these Materials 關於這些材料
      • Metacognition元認知
      • Metacognition in Action行動中的元認知
    • UNIT2
      • Introduction簡介
      • Functions of Computers  Input  Output  Storage  and Processing計算機功能  輸入  輸出  存儲和處理
      • Hardware硬件
      • Software軟件
      • Fireware固件
      • Componentization  Standardization組件化  標準化
      • Connection Interfaces and Cables連接接口和電纜
  • HW2
  • HW3
    • Chacrater input字符輸入
      • input strings types int輸入字符串類型int
      • Discussion討論區-1
      • User input in Python使用Python的用戶輸入
      • Manipulating strings a few ways處理字符串幾種方法
    • Odd Or Even奇數或偶數
      • Exercise 2 and solution練習2和解決方案
      • Discussion討論區-2
      • Modular arithmetic the modulus operator模塊化算術模運算符
      • Conditionals有條件的
      • Checking for equality and comparators in general檢查是否相等以及一般的比較器
    • List Less Than Ten列出少於十
      • Exercise 3 and Solution練習3和解決方案
      • Discussion討論區-3
      • Lists清單
      • More Conditionals更多條件
  • 心得
Discussion討論區-3 << Previous Next >> More Conditionals更多條件

Lists清單

This week’s exercise hits on a topic critical for all types and styles of programming: lists. Lists are basically an ordered way of grouping things (called elements) - the cool thing about lists in Python is that you can have a list that contains objects of multiple types. Your list can mix between strings, integers, objects, other lists, what have you.

The way to construct an empty list is just to do

x = []

And your variable x now holds an empty list. To add things to this list, just “append” them to the list. Like so:

x = []
x.append(3)

Your list x now looks like [3].

In Python, lists are also iterables, which means you can loop through them with a for loop in a convenient way. (If you come from other languages like C++ or Java you are most likely used to using a counter to loop through indices of a list - in Python you can actually loop through the elements.) I will let the code speak for itself:

my_list = [1, 3, "Michele", [5, 6, 7]]
for element in my_list:
  print(element)

Will yield the result:

1 
3
"Michele"
[5, 6, 7]

There are many other properties of lists, but for the basic exercise all you should need is this for loop property. Future weeks will address other properties of lists.

本週的練習涉及一個對所有類型和風格的編程都至關重要的主題:list。列表基本上是一種將事物(稱為elements)進行分組的有序方式-Python中列表的最酷的功能是您可以擁有一個包含多種類型對象的列表。您的列表可以混合在字符串,整數,對象,其他列表之間,包括您所擁有的。

構造一個空列表的方法就是

x = []

x現在,您的變量包含一個空列表。要將內容添加到此列表中,只需將它們“添加”到列表中即可。像這樣:

x = []
x.append(3)

您的清單x現在看起來像[3]。

在Python中,列表也是可迭代的,這意味著您可以方便地使用for循環遍歷它們。(如果您來自其他語言,例如C ++或Java,則最有可能習慣於使用計數器循環遍歷列表的索引-在Python中,您實際上可以循環遍曆元素。)我將讓代碼說明自己:

my_list = [1, 3, "Michele", [5, 6, 7]]
for element in my_list:
  print(element)

將產生結果:

1 
3
"Michele"
[5, 6, 7]

列表還有許多其他屬性,但是對於基本練習而言,您只需要此for循環屬性。未來幾週將處理列表的其他屬性。


Discussion討論區-3 << Previous Next >> More Conditionals更多條件

Copyright © All rights reserved | This template is made with by Colorlib