Liz's Blog

Python學習筆記#1:基礎篇

| Comments

這週開始同步進行Python及補完之前R機器學習的理論篇,一樣是一點點,持續學習下去囉~

Udemy
課程名稱:Python for Data Science and Machine Learning Bootcamp
講師:Jose Portilla
價格:利用特價時,以15澳幣購入(台幣計價有時候會看到300塊的優惠價)

課程推薦新手利用Anaconda來安裝Python和Jupyter。

此課程對完全不會寫程式的人會有點困難,但有一點程式語言基礎的話,應該不會太快放棄啦?!此篇算是基礎快速入門篇,比起之前學的R好像相對簡單一點點。而且有Python中文社群來支援>3 >3 >3

1.數學運算(加減乘除、次方、餘數)

10 + 11
10 * 10
10 / 10
10 ** 10
10 % 3
10 + 10 * 5 + 1

2.變數分配(注意變數名稱不能用數字或特殊字元為開頭)

today_purchase = 10000
yesterday_purchase = 10500
total = today_purchase + yesterday_purchase
total

3.字串(使用單引號或雙引號都可以,但如果遇到句子中有',就只能使用雙引號)

'Happy Birthday'
"Happy Birthday"
"I'm sorry!"

4.印出(Printing)

x = 'Hi'
x

print(x)

name = 'Super VIP'
ID = 19375
print('Hi! {}, Your ID is {}'.format(name,ID))

name = 'Super VIP'
ID = 19375
print('Hi! {a}, Your ID is {b}'.format(a=name,b=ID))

5.串列(Lists)

message = ['super','big','fan','no.',1]
message

#append加上新物件
message.append('WOW')
message

#第一個元素為0,第二個元素為1,依此類推
message[1]

#[1:3]代表從第二個元素開始,結束(不包含)在第四個元素之前
message[1:3]

#修改元素內容
message[5] = [1,2]
message

message[5][1]

6.字典(Dictionaries)

dic = {'key1':'item1','key2':'item2'}
dic                                      #{'key1': 'item1', 'key2': 'item2'}
dic['key1']                              #'item1'
dic.keys()                               #dict_keys(['key1', 'key2'])
dic.items()                              #dict_items([('key1', 'item1'), ('key2', 'item2')])

7.布林值(Booleans)

True                                  #True
False                                 #False

8.Tuples(和list差別在於Tuple是不可改的,但list可以)

t = (100,101,102)
t[1]
t[0] = 103                             #出現TypeError

9.集合(Sets)

#歸類出有多少類別
x = {5,6,5,6,7,7,8}                    
x                                      #{5, 6, 7, 8}

x.add(1)                               
x                                      #{1, 5, 6, 7, 8}

10.比較運算(Comparison Operators)

9 > 10                                 #False     
10 >= 10                               #True
10 == 10                               #True
10 != 9                                #True

11.Logic Operators(且、或)

10 > 9 and 10 < 11                     #True
10 < 11 or 10 < 9 or 10 == 11          #True

12.if、elif、else判斷式

if 10 > 9:
    print('Awesome')                    #Awesome

if 10 > 11:
    print('Awesome')
else:
    print('QQ')                         #QQ
    
if 10 < 9:
    print('Awesome')
elif 10 == 10:
    print('Perfect')
else:
    print('QQ')                         #Perfect

13.for Loops

seq = [5,6,7]
for num in seq:
    print(num)
    
seq = [5,6,7]
for num in seq:
    print('cool')

14.while Loops(迴圈)

a = 5
while a < 10:
    print('a is: {}'.format(a))
    a = a+1

15.range()

#取0-9,共十個值
range(10)

for i in range(10):
    print(i)
    
list(range(10))                    #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

16.List Comprehension(可以讓for迴圈集中在一行寫完)

x = [1,2,3,4,5]

#一般可以這樣寫
out = []
for num in x:
    out.append(num**3)
print(out)                     #[1, 8, 27, 64, 125]

#這樣寫更快
[num**3 for num in x]          #[1, 8, 27, 64, 125]

17.自訂函數(Functions)

#()不輸入特定值,則會出現預設值
def post(params='New Post'):
    print(params)
post()                          #New Post

post('first post')              #first post

def num(x):
    return x*2
out = num(3)
print(out)                      #6

18.Map & Lambda

#map()可以幫list作轉換,前者是轉換後的形式,後者是要轉換的list,會輸入<map at xxxxxxx>。再用list列出即可。
seq = [5,6,7]
def double(x):
    return x*2
map(double,seq)
list(map(double,seq))                         #[10, 12, 14]

#使用lambda可以將自訂函式列在同一行
list(map(lambda x: x*2,seq))                  #[10, 12, 14]

19.Filter & Lambda

#filter()可以篩出符合條件的list。
filter(lambda y: y%3 == 0,seq)
list(filter(lambda y: y%3 == 0,seq))          #[6]

20.Methods

greeting = 'How are you today, John Doe?'
#全小寫
greeting.lower()                          #'how are you today, john doe?'         

#全大寫
greeting.upper()                          #'HOW ARE YOU TODAY, JOHN DOE?'

#一個單字變成一個元素
greeting.split()                          #['How', 'are', 'you', 'today,', 'John', 'Doe?']

#以,分開元素
greeting.split(',')                       #['How are you today', ' John Doe?']

greeting.split(',')[1]                    #' John Doe?'

#pop()取出特定元素,預設是最後一個
lst = [5,4,6]
lst.pop()                                 #6
lst                                       #[5, 4]

#y在[5,6,4]中嗎?
'y' in [5,6,4]                            #False

#y在['x','y','z']中嗎?
'y' in ['x','y','z']                      #True

Comments

comments powered by Disqus