# 10/12 python 基礎程式設計(一) ### 今日教學內容 1. 輸出print() 2. Data Type 3. List 詳細介紹 ### Ch1 : Hello World ```py= print("Hello World") ``` Printing in Python is simple, just use print() and include the string you want to display. ### Ch2 : Data Type #### Primary data types * 整數 int (integer):0、1、39、42、2147483647、-2147483648 * 浮點數 float (floating number):.1、0.、2.71828182846、3.14159 * 複數 complex (complex number):1j、3j、5+2j * 字串 str (string):"Never"、"Ever"、'abc'、'' * 布林值 bool (bool):True、False #### Data structure types * list : represented by square brackets, ex.[2,5,7,"abc",[1,2,3]] * tuple : represented by parentheses, ex.(2,5,7,"abc",[1,2,3]) * set : represented by curly braces, ex.{1,2,3,"abc"}. This Data type only allows unique elements, so {1,1,2} is equivalent to {1,2}. * dictionary : represented by curly braces and semicolon, ex.{"Apple":1} **To check the data type, we can use *type()*, and then print it by using print().** ```py= print(type(1)) print(type(.1)) print(type('1')) print(type(1j)) print(type([1])) ``` Result : ![](https://hackmd.io/_uploads/BJ9-rp8bp.png) ### List Operation #### List 基本語法 Lists can be accessed using an index to retrieve the value at a specific position. The syntax for this is : **list_variable_name[index]** note that the indexing in Python **starts from 0, not 1**, which is different from human convention. If the index number exceeds the length of the list, the program will raise an error. ```py= li = [1,2,3,4,5] print(li[4],li[3],li[2]) # 5, 4, 3 print(li[5]) # error ``` Because the **length of "li" is 5**, its **indices range from 0 to 4**. So, "li[5]" will display an IndexError. ❗List can also be indexed from the back, where li[-3] represents the third-to-last element in the list. ```python= print(li[-3]) # 3 ``` <br/> Since lists can contain other lists, you can use multiple indices to access values within nested lists. The syntax for this is: **list_variable_name[index1][index2]** ```py= li = [[1,2,3,4,5],[6,7,8,9,10]] print(li[0][4],li[1][3],li[0][2]) # 5, 9, 3 ``` | index1\index2 | 0 | 1 | 2 | 3 | 4 | | ------------- | --- | --- | --- | --- |:---:| | **0** | 1 | 2 | 3 | 4 | 5 | | **1** | 6 | 7 | 8 | 9 | 10 | #### Slicing Additionally, if you want to create a list with a range of values, you can use a technique called **"slicing."** The syntax is similar to ‘range’ (start, stop, step), but you use colons **‘:’** to separate the elements. Moreover, you can omit numbers in certain positions. For example, li[3:] means starting from the 3rd position to the end, and li[:3] means from the beginning up to the 3rd position. ```py= li = [1,2,3,4,5] print(li[1:4],li[3:],li[:3],li[::-1]) # [2, 3, 4] [4, 5] [1, 2, 3] [5, 4, 3, 2, 1] ``` :::warning li[1:4] means from index 1 to the fourth index, so the result is [2, 3, 4] not [2, 3, 4, 5] li[::-1] means from last index to first index, so the result is [5, 4, 3, 2, 1] ::: <br/> To change the values inside a list, you can directly modify them using indexing and slicing. It's important to note that when **using slicing to assign new values**, the data type on the right side must be a list. When using indexing, there are no restrictions on the data type. However, be aware that the data type stored in the list may differ as a result. ```py= li = [1,2,3,4,5] li[1:4] = [3] print(li) # [1 ,3 ,5] li[1] = [3] print(li) # [1, [3], 5] ``` #### .append() Next, let's introduce methods for changing the length of a list. In the previous examples, we only modified the values inside the list without changing its length. To **add a new value to a list**, you can use the **.append(value)** method. ```py= li = [1,2,3,4,5] print(li) # [1, 2, 3, 4, 5] li.append(7) print(li) # [1, 2, 3, 4, 5, 7] ``` You'll notice that this method is special because it is a member function of the list, meaning it can only be used with lists. It's exclusive to lists, so you don't enclose the entire list in parentheses; instead, you append it to the variable name using a dot. #### .pop() There are functions to increase the length of a list, and of course, there are functions to decrease it as well. By using **.pop(index)**, you can **delete the value at the specified index in the list** and reduce its length at the same time. ```py= li = [1,2,3,4,5] li.pop() print(li) # [1, 2, 3, 4] li.pop(1) print(li) # [1, 3, 4] ``` #### .sort() Next is .sort(). When a list contains data of only one data type or all the data within it can be compared, then the list can be sorted using the .sort() method. ```py= li = [2,3,1,4,5] li.sort() print(li) # [1, 2, 3, 4, 5] ``` Additionally, Python has another function called sorted(). #### sorted() sorted() and .sort() have the same functionality, but they differ in their usage. .sort() can be used to sort the list on which the method is called, whereas **sorted() does not modify the original list. Instead, it creates a new sorted list while leaving the original list unchanged.** This is because .sort() is a member function with the authority to modify the values of the object it's called on, while sorted() is not, which is why it creates a new list. ```py= li = [2,3,1,4,5] a = sorted(li) print(li) # [2, 3, 1, 4, 5] print(a) # [1, 2, 3, 4, 5] ``` #### len() Next is len(), which can **return the length of a list**. It can be used in conjunction with for loops and range(). ```py= li = [4,3,2,1] for i in range(len(li)): print(li[i]) # 4, 3, 2, 1 ``` ### Thanks for watching ![](https://hackmd.io/_uploads/ByPeh0L-6.jpg)