# 個人筆記
# 第一堂02/14
1.單純數學符號計算
| 符號 | 名稱 | 範例 |
| -------- | -------------|---------|
| + |加 |x + y |
| - |減 |x - y |
| * |乘 |x * y |
| / |除 |x / y |
| % |餘數 |x % y |
| ** |次方 |x ** y(3**3=27)|
| // |商 |x // y |
2.特殊數學符號計算
| 符號 | 範例 | 算法 |
| -------- | ------- | ------- |
| = |x = 5 |x = 5 |
| += |x += 8 |x = x +8 |
| -= |x -= 7 |x = x -7 |
| *= |x *= 7 |x = x * 7|
| /= |x /= 7 |x = x /7 |
| %= |x %= 7 |x = x % 7|
| **= | x //= 7 |x = x //7|
| //= | x **= 7 |x = x **7|
# 第二堂02/21
1.import math 匯入數學模組
```
math.floor() 將括號裡的數無條件捨去
math.ceil() 將括號裡的數無條件進位
math.sqrt() 將括號裡的數開根號
```
2.單字定義
```
max求最大值
min求最小值
abs絕對值
pow次方 Ex.pow(2,3)=8
```
# 第三堂03/07
1."xx"引起來的為字串
2.函數、型態定義
```
type() 查找括號內的資料型態
len() 算出括號內的長度(有幾項)
sum() 算出括號裡所有值的總和
數值型態:int(整數),float(浮點數)
字串型態:str(字串)
容器型態:list, dict
布林型態:bool(布林值:可設定為兩種值True或False)
```
3.例題解釋
(1)
:::success
```
x="5"
y="3"
print(type(x)) #印出x的資料型態
print(type(y)) #印出y的資料型態
x+y #注意:如果x,y的資料型態不一樣,則兩者無法相加
```
```
<class 'str'>
<class 'str'>
53
```
:::
(2)
:::success
```
a=input("Enter a age")
b=input("Enter b age")
print(a+b) #印出兩字串相加結果
a=int(a) #將字串a結果轉換為數值a
b=int(b) #將字串b結果轉換為數值b
print(a+b) #印出兩數值相加結果
```
```
Enter a age10 #會先出現Enter a age讓你填入數字
Enter b age10 #再出現Enter b age讓你填入數字
1010
20
```
:::
(3)
:::success
```
thislist = list(("apple", "banana", "cherry"))
print(thislist[0]) #印出第一項
```
```
apple
```
注意:0為第一項,1為第二項,-1為最後一項,-2為倒數第二項,以此類推
:::
(4)
:::success
```
thislist=[9,80,32,78,69,83,92,35,21]
thislist.sort() #將串列由小排到大
print(thislist)
```
```
[9, 21, 32, 35, 69, 78, 80, 83, 92]
```
:::
(5)**迴圈**
:::success
```
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
```
```
apple
banana
cherry
```
:::
# 第四堂03/14
1.例題解釋
(1)
:::success
```
lst=['a','b','c','d','e']
print(lst[1:3]) #切片的意思,1為ab之間
```
```
['b', 'c']
```
:::
(2)
:::success
```
lst=['a','b','c','d','e']
lst[2]='x'
print(lst) #將編號2(第三項)改成x
```
```
['a', 'b', 'x', 'd', 'e']
```
:::
(3)
:::success
```
lst=['a','b','c','d','e']#宣告一個變數lst代表一個串列有五個元素分別為a字串,b字串,c字串,d字串,e字串
lst[2]='x'#lst串列裡編號為二的元素設定為x
lst.append('c')#在lst串列裡附加一個元素c
lst.append('c')
lst.remove('b')#在lst串列裡移除一個元素b
print(lst)
lst.sort()#使用排序功能
print(lst)
print(lst.count('c'))#印出字串c在串列裡出現幾次
```
```
['a', 'x', 'd', 'e', 'c', 'c']
['a', 'c', 'c', 'd', 'e', 'x']
2
```
:::
(4)
:::success
```
lst=[9,80,32,78,69,83,92,35,21]
for x in lst:
if x>0 and x<50:
print('第一組')
print(x)
if x>50 and x<100:
print('第二組')
print(x)
```
```
第一組
9
第二組
80
第一組
32
第二組
78
第二組
69
第二組
83
第二組
92
第一組
35
第一組
21
```
:::
# 第五堂03/21
1.例題解釋
(1)求總和、最大值
:::success
```
lst=[9,80,32,78,69,83,92,35,21]
a=0 #宣告一個總和變數a,設定為0
for x in lst:
a=x+a
print(a)
b=0 #設定一個變數b,放置最大值
for x in lst:
if x>b:
b=x
print(b)
```
```
499
92
```
:::
(2)
:::success
```
a=[3,5,7,4,6]
y=0
z=0
for x in a:
y=y+x
if x>z :
z=x
print(x)
print(y)
print(z)
```

```
6
25
7
```
:::
# 第六堂03/28
1.例題解釋
(1)繪圖
:::success
```
#使用畫圖套件 matplotlib.pyplot
import matplotlib.pyplot as plt
a=[1,2,3,4,5]
b=[1,2,3,4,5]
plt.scatter(x=a,y=b,s=10) #括弧裡為參數,整個為函數
plt.axis('square') #軸為正方形
plt.xlim(0,6) #x軸範圍設定在0-10之間
plt.ylim(0,6)
print(f'我的圖形')
plt.show()
```
答案省略
:::
(2)
:::success
```
dic={'WaterMelon':100, 'Apple':40} #watermelon為鍵,100為值
print( dic['WaterMelon'] )
```
```
100
```
:::
:::success
```
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red" #加入一個配對,鍵為color,值為red
print(thisdict)
```
```
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}
```
:::
(3)
:::success
dict={'WaterMelon':'100',
'Apple':'40',
'Banana':'30',
'Orange':'20',
'peach':'50',
'Grape':'25'}
```
for x in dict:
print(x)
```
```
WaterMelon
Apple
Banana
Orange
peach
Grape
```
```
for x in dict.values():
print(x)
```
```
100
40
30
20
50
25
```
```
for x, y in dict.items():
print(x, y)
```
```
WaterMelon 100
Apple 40
Banana 30
Orange 20
peach 50
Grape 25
```
```
a=set()
for x in dict.values():
a.add(x[0:1])
print(a)
```
```
{'4', '3', '1', '5', '2'}
```
```
lst = (dict.values())
emptylist = []
emptydict = {}
for i in lst:
emptylist.append(i[:1])
for i in emptylist:
emptydict[i] = emptydict.get(i,0) + 1
Value = emptydict.values()
Key = emptydict.keys()
TValue = tuple(Value)
TKey = tuple(Key)
for i in range(len(TValue)):
print(f'{TKey[i]}出現了{TValue[i]}次')
```
```
1出現了1次
4出現了1次
3出現了1次
2出現了2次
5出現了1次
```
:::
# 第七堂04/11
1.例題解釋
(1)寫入CSV
:::success
```
# 手工掛載雲端硬碟
with open('/content/drive/MyDrive/___DataSet/001_Hello.txt', 'w') as f:
f.write('Hello Google Drive')
```
#with:以...為基礎
:::
(2)讀取CSV
:::success
```
with open('/content/drive/MyDrive/___DataSet/001_Hello.txt', 'r') as f:
ss=f.read()
print(ss)
```
```
Hello Google Drive
```
:::
(3)刪除檔案
:::success
```
import os
if os.path.exists("/content/drive/MyDrive/___DataSet//001_Hello.txt"):
os.remove("/content/drive/MyDrive/___DataSet//001_Hello.txt")
print("檔案已經刪除")
else:
print("檔案不存在")# 刪除檔案
```
```
檔案已經刪除
```
:::
(4)讀取雲端硬碟中的excel檔
:::success
```
import pandas as pd
df = pd.read_excel('/content/drive/My Drive/___DataSet/001_Income_F.xlsx')
df
```
:::
(5)
:::success
```
import pandas as pd
grades = {
"name": ["Mike", "Sherry", "Cindy", "John"],
"math": [80, 75, 93, 86],
"chinese": [63, 90, 85, 70]
}
df = pd.DataFrame(grades)
df
```
```
name math chinese
0 Mike 80 63
1 Sherry 75 90
2 Cindy 93 85
3 John 86 70
```
:::
(6)
:::success
```
grades = [
["Mike", 80, 63],
["Sherry", 75, 90],
["Cindy", 93, 85],
["John", 86, 70]
]
df = pd.DataFrame(grades)
df.columns = ["student_name", "math_score", "chinese_score"] #自訂欄位名稱
df.columns = ["name", "math", "chinese"] #修改欄位名稱
df
```
```
name math chinese
0 Mike 80 63
1 Sherry 75 90
2 Cindy 93 85
3 John 86 70
```
:::
(7)
:::success
```
grades = [
["Mike", 80, 63],
["Sherry", 75, 90],
["Cindy", 93, 85],
["John", 86, 70]
]
print(df.head(2)) #取得最前面的兩筆資料"
print(df.tail(3)) #取得最後面的三筆資料"
```
```
學號 體重 身高
0 A001 60 165
1 A002 50 157
學號 體重 身高
2 A003 80 182
3 A004 75 175
4 A005 72 170
```
:::
(8)
:::success
```
print("取得索引值0~2的資料")
print(df[0:3])
```
```
取得索引值0~2的資料
學號 體重 身高
0 A001 60 165
1 A002 50 157
2 A003 80 182
```
:::
(9)
:::success
df.at[1, "身高"]
```
157
```
df.iat[4,2]
```
170
```
df.loc[[1, 3], ["學號", "體重"]]
```
學號 體重
1 A002 50
3 A004 75
```
df.iloc[[1, 3], [0, 2]]
```
學號 身高
1 A002 157
3 A004 175
```
:::
(10)
:::success
```
list(range(1,10))
```
```
[1, 2, 3, 4, 5, 6, 7, 8, 9]
```
:::
(11)
:::success
```
for i in range(0,5):
print(i)
print(df.at[i,"學號"])
```
```
0
A001
1
A002
2
A003
3
A004
4
A005
```
:::
(12)
:::success
```
for i in range(0,5):
for j in range(0,5):
print(f'{i},{j}')
```
```
0,0
0,1
0,2
0,3
0,4
1,0
1,1
1,2
1,3
1,4
2,0
2,1
2,2
2,3
2,4
3,0
3,1
3,2
3,3
3,4
4,0
4,1
4,2
4,3
4,4
```
:::
(13)
:::success
```
import pandas as pd
grades = {
"學號": ["A001","A002","A003","A004","A005"],
"體重": [60,50,80,75,72],
"身高": [165,157,182,175,170]
}
df = pd.DataFrame(grades)
for i in range(0,5):
print(df.at[i, "學號"])
for j in range(0,1):
print(df.at[i, "體重"])
for x in range(0,1):
print(df.at[i, "身高"])
```
```
A001
60
165
A002
50
157
A003
80
182
A004
75
175
A005
72
170
```
:::
2.容器型態
(1)串列
(2)字典
(3)集合
(4)data frame
# 第八堂04/25
1.老師上課講解加分作業
2.討論個人加分作業
3.討論小組期末作業報告
# 第九堂05/02
1.老師講解作業程式
2.討論小組期末作業報告
# 第十堂05/09
1.excel輸入&跑圖
(1)
(2)
2.colab
https://colab.research.google.com/drive/1zpNy8QsILBC6iCR6YlH2h_vOovFf6Y-W
# 第十一堂05/16
1.上課內容
(1)
:::success
```
#計算1,2,.....,n的總和
n=10
s=0
for i in range(1,n+1):
s=s+i
print(s)
```
```
55
```
:::
(2)
:::success
```
def Sum1toN(n):
s=0
n=10
for i in range(1,n+1):
s=s+i
return s
Sum1toN(n)
```
```
55
```
:::
(3)
:::success
```
def my_function():
print("Hello from a function")
my_function()
```
```
Hello from a function
```
:::
(4)
:::success
```
def myfun(x,y,z):
print(x+"你好"+y+z)
myfun("同學","啊","早安")
```
```
同學你好啊早安
```
:::
(5)
:::success
```
def mycal(x,y,z):
print(x+y+z)
mycal(3,5,2)
mycal(y=5,x=3,z=2)
```
```
10
10
```
:::
(6)
:::success
```
def mycal(x,y):
z=x+y
return z
a=mycal(10,5)
print(a)
def mycal(x,y):
z=x+y
return z
b=mycal(x=10,y=5)
print(b)
```
```
15
15
```
:::
(7)
:::success
```
def mycal(x,y,z):
i=x+y+z
return i
a=mycal(3,5,2)
print(a)
```
```
10
```
:::
2.小考內容
(1)請定義一個函數myFunction,傳入一個參數n。此函數可以計算出1,2,…,n的總和。函數能傳回後的總和。(必須用for迴圈完成)
:::success
```
def myFunction(n): #設定一個函數myFunction,傳入一個參數n
s=0 #令一個變數s=0
for i in range(1,n+1): #針對range內的元素設定為i
s=s+i #把s+i的值再設定回s
return s #回傳s
myFunction(10) #n代10得出解答
```
```
55
```
:::
(2)請定義一個函數myFunction,傳入一個參數n。此函數可以找出1,2,…,n的之間2的倍數,並列印出到螢幕上。(必須用for迴圈完成)
:::success
```
def myFunction(n): #設定一個函數myFunction,傳入一個參數n
s=0 #令一個變數s=0
for i in range(1,n+1): #針對range內的元素設定為i
if i%2==0: #如果i除以2的餘數等於0
print(i) #將i印出
myFunction(10) #把n代10函數印出
```
```
2
4
6
8
10
```
:::
(3)請在螢幕上印出如下圖案 (必須用for迴圈完成)
:::success
```
s='*' #設定一個變數s為'*'
for i in range(1,14): #針對range內的元素設定為i
if i%2==1: #如果i除以2的餘數等於1
a=i*s #將i乘上變數指定給a
print(a) #將a印出
```
```
*
***
*****
*******
*********
***********
*************
```
:::
# 第十二堂05/23
小考作答狀況+助教解題訂正
https://colab.research.google.com/drive/1AQc30_UHnxYTpq0uG2V5kzZLOVKe8s_2
{"metaMigratedAt":"2023-06-16T19:59:09.464Z","metaMigratedFrom":"Content","title":"個人筆記","breaks":true,"contributors":"[{\"id\":\"61b2fea5-a756-4dbd-bbe4-d0b6f09881f4\",\"add\":12524,\"del\":2744}]"}