Professional Documents
Culture Documents
1.1 When should I put a return ? pls check the following 2 codes. #@@
Out[1]: 3
My nickname is pig.
My nickname is pig.
My nickname is pig.
----------------------
My nickname is 1004.
My nickname is 1004.
My nickname is 1004.
My nickname is 1004 .
My nickname is 1004.
My nickname is 1004.
In [28]: say3('pig') #due to we only put return, python will not return anything
In [29]: say3('Coco')
My nickname is Coco .
My nickname is Coco.
My nickname is Coco.
In [30]: say3(10)
My nickname is 10 .
My nickname is 10.
My nickname is 10.
My name is Dio.
I am 20 years old.
I am male.
----------
My name is Dio.
I am 20 years old.
I am male.
----------
My name is Dio.
I am 20 years old.
I am male.
In [44]: say_myself1('Dio',20,False)
print("----------")
say_myself1('Dio',20,0) #@@
My name is Dio.
I am 20 years old.
I am female.
----------
My name is Dio.
I am 20 years old.
I am female.
In [46]: say_myself2('Dio')#Due to we already set default value, so it can print 20 and NCCU automatically.
My name is Dio.
I am 20 years old.
I am a student of NCCU.
In [48]: say_myself2('Dio',27,'NTU')# We can enter what we need if default value is not we want.
My name is Dio.
I am 27 years old.
I am a student of NTU.
Out[49]: 7
Out[53]: 3
Out[54]: 3
In [55]: #Ask users for working hours and rate per hour using input() to compute gross pay (hours*rate per hour).
#Use 35 hours and a rate of 2.75 per hour to test the program (the pay should be 96.25).
#You should use input to read a string and float() to convert the string to a number.
hours=float(input('Enter hours:'))
rate=float(input('Enter rate:'))
gross=hours*rate
print('Gross pay:',gross)
Enter hours: 35
Out[56]: 96.25
2. Method
object.method: This week
p.s. Function(object): Before this week
2.1 x.sort
to make items in list small to big in oder.
In [62]: lst1=[1,0,2,4,3,5,7,6,8,10,9]
lst1.sort()
print(lst1)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [64]: lst2=['bananas','apple','cherry']
lst2.sort()
print(lst2)
print("------------------------------")
lst3=['ant','apple','abandon','a']#@@
lst3.sort(key=len)# we can also sort by length of string in list
print(lst3)
------------------------------
In [68]: lst4=['','?','App','blue','30','!']#@@
lst4.sort()
print(lst4)
#@@ # Why ? is bigger than numbers? Pls check https://zh.wikipedia.org/zh-tw/Unicode字符列表
print("------------------------------------")
print(min(lst4))
print(max(lst4))
#space<!<number<?<capital letter<small letter
------------------------------------
blue
[0, 2, 3, 4, 8, 9]
------------------
[0, 2, 3, 4, 8, 9]
[8, 3, 4, 9, 2, 0]
2.2 x.reverse()
This method just show the list backward.
In [1]: lst5=[10,2,5,3,8,1]
lst5.reverse()
lst5
[1, 2, 3, 5, 8, 10]
[10, 8, 5, 3, 2, 1]
--------------------------------------------------------------------------------------
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
If a key function is given, apply it once to each list item and sort them,
--------------------------------------------------------------------------------------
[10, 8, 5, 3, 2, 1]
2.3 x.append()
To put things into list
In [5]: x=[1,2]# we can add a string
x.append('banana')
print(x)
[1, 2, 'banana']
In [10]: #Let's check how many items are there in the list.
print(len(x))
# [1, 2, 'banana', 1000, [3, 4], '?']
#index 0 1 2 3 4 5
print(x[4])# To find index 4 in the list
[3, 4]
2.4 x.extend()
Pls put an eye on the difference between append and extend
In [11]: x1=[1,2,3,4]
x1.append('NCCU')
print(x1)
print("--------------------------------")
x2=[1,2,3,4]#If using extend, the string will be seperated.
x2.extend('NCCU')
print(x2)
[1, 2, 3, 4, 'NCCU']
--------------------------------
2.5 x.set()
Just like list, but u can't find repeat data.
Pls note that the order in set is random. #@@
In [14]: #help(set(e)) # Pls check row 5.--> Build an unordered collection of unique elements.
x4=[1,2,3,4,'N','C','C','U']
setA=set(x4)
print(type(setA))
print(setA)
<class 'set'>
<class 'list'>
---------------
2.6.1 del()
2.6.2 x.remove
In [84]: help(x.remove) #@@ #Pls check row 3. -> Remove first occurrence of value.
print("------------------------------------------------------")
a1=["N", "C", "U", "C"]
a1.remove("C") #remove the first "C"
print(a1)
------------------------------------------------------
2.6.3 x.pop()
In [23]: y=[1,2,3,4]
In [28]: help(y.pop) #@@ #Pls check row 3 -> Remove and return item at index (default last).
print("----------------------------------------------------------------------")
y=[1,2,3,4]
print(y.pop()) #Due to the default index is -1, so it will remove and return the last item.
print(y)
----------------------------------------------------------------------
[1, 2, 3]
In [86]: y=[1,2,3,4]
# [1,2,3,4]
#index 0 1 2 3
print(y.pop(2)) # 2 is index
print(y)
[1, 2, 4]
2.7 x.clear()
clear all itmes
[]
-index找不到的時候會出現type error
-find找不到的時候會出現-1
(兩個都是直接輸入item 找數字定位)
Diff2: We can't use find in list.
-----------------
-1
----------------------------------------------------------------------
Return -1 on failure.
In [89]: x=[1,2,3,4]
# [1,2,3,4]
#index 0 1 2 3
print(x.index(3)) #it means number 3 is at index 2.
#print(x.find(3)) # Diff2: we can't use find in list.
In [2]: x="python"
# python
#index 012345
print(x.find("yt")) #it will only print the location of the first letter.
print(x.index('yt'))
2.9 x.count()
In [9]: email='109353501@g.nccu.edu.tw'
# 109353501@ g . n c c u . e d u . t w (pls ignore the blanks)
#index 0123456789 10 11 12 13 14 15 16 17 18 19 20 21 22
print(email.count('.'))#it will show the number of . is in the string.共有三個'.'
print(email.index('.'))#it will show the first one.找出第一個'.'的數字定位
print(email.find('.'))#it will show the first one.找出第一個'.'的數字定位
#dir(email) #show what we can do with email
print(email.rfind('.'))#it will show the last one.找出最後一個'.'的數字定位
print(email.rindex('.'))#it will show the last one #@@找出最後一個'.'的數字定位
print("--------------------------------------------------------------------")
help(email.rfind)#@@ Show highest index, show -1 when not found
help(email.rindex)#@@ Show highest index, show ValueError when not found.
11
11
20
20
--------------------------------------------------------------------
Return -1 on failure.
In [12]: email='109353501@g.nccu.edu.tw'
# show student ID
# [0~N] --> 0 ~ N-1 (即不包含N)
print(email[0:9])
#[:N] --> ~ N-1
print(email[:9])
print("---------------")
# show mail without student ID
# [N:] --> N ~ 從N開始到最後
print(email[9:])
print("---------------")
# show only nccu
# 109353501@ g . n c c u . e d u . t w (pls ignore the blanks)
#indexLV1 0123456789 10 11 12 13 14 15 16 17 18 19 20 21 22
#indexLV2 0 1 2 3 4 5 6 7 8 9 10 11 12 13
print(email[9:][3:7])
print(email[12:16])
109353501
109353501
---------------
@g.nccu.edu.tw
---------------
nccu
nccu
2.10 split() we might can use this when import a txt. file.
In [23]: line='a lot of space'
help(line.split)#@@ #row6 -> None (the default value) means split according to any whitespace.
print("-------------------------------------------------------------------------------")
a=line.split()#split according to any whitespace
print(a)
print("-------------------------------------------------------------------------------")
b=line.split(' ')#@@
print(b)
print("-------------------------------------------------------------------------------")
#@@
#c=line.split("") #Error due to empty separator.
#print(c)
Return a list of the words in the string, using sep as the delimiter string.
sep
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
--------------------------
----------------------------------------------------------------------------------------------------------------
-----------------------------------
['公司', '簡稱', 'TSE', '產業別', '年度', '上市狀況', '產業別', '產業名稱', '成立年數', '上市年數', '事務所碼', '簽證
事務所', '是否為大型事務所', '會計師1', '會計師2', '簽證意見類型', '繼續經營假設是否有疑慮', '財報重編次數', '事務所任期
_自1983起(年)', '事務所任期_上市櫃起(年)', '事務所任期_公開發起(年)', 'CPA1任期_自1983起(年)', 'CPA1任期_上市櫃起
(年)', 'CPA1任期_公開發行起(年)', 'CPA2任期_自1983起(年)', 'CPA2任期_上市櫃起(年)', 'CPA2任期_公開發行起(年)', 'CPA1
應輪調', 'CPA2應輪調', 'CPA1連續查核任期', 'CPA2連續查核任期', '更換會計師人數', '更換會計師事務所', '更換會計師事務
所', 'A', 'CPA1經驗_自1983起(年)', 'CPA1經驗_上市櫃(年)', 'CPA1經驗_公開發行(年)', 'CPA1經驗_產業(年)', 'CPA2經驗_自1
983起(年)', 'CPA2經驗_上市櫃(年)', 'CPA2經驗_公開發行(年)', 'CPA2經驗_產業(年)']
['dioh', 'g.nccu.edu.tw']
-------------------------
dioh
-------------------------
g.nccu.edu.tw
-------------------------
dio
dio
dioh
dioh
In [31]: print("-------------------------")
#to get nccu
# ["dioh", "g.nccu.edu . t w"] (pls ignore the blanks)
#index 0 1
#index 0123 0123456789 10 11 12
print(a[1][2:6])
print("-------------------------")
#to get tw
# ["dioh", "g.nccu.edu . t w"] (pls ignore the blanks)
#indexLV1 0 1
#indexLV1(negative) -2 -1 #@@
#indexLV2 0123 0123456789 10 11 12
#indexLV2(negative) -2 -1 #@@
#index 11 ~ 12(N=13, here doesn't include index 13)
print(a[1][11:13])
print(a[1][11:])
print("-------------------------")
#@@
#to get tw by using negative indexing
print(a[1][-2:])
print(a[-1][-2:])
print(a[-1][11:])
print(a[-1][11:13])
-------------------------
nccu
-------------------------
tw
tw
-------------------------
tw
tw
tw
tw
End of Class