念研究所時就常聽學弟歌頌Python有多好多好用,然而我對P語言的了解僅限於:
它不用編譯可以直接執行,以及當parser處理字串很好用外,就沒了!
雖然知道網路上有很多資訊可以學,不過太容易到手的資訊總是讓人沒動力,
就像跟圖書館借的書有時間壓力會盡快看完,但自己買的卻一直懶得翻的道理一樣。
(寫到這裡才想起來我有一本培養與鍛鍊程式設計的邏輯腦,買了幾年了還沒看...)
於是就一直處於Python好像很好用,可是懶得學的心態,
每次需要處理資料還是用熟悉的C++從#include開始重刻一份code,效率實在不彰。
直到最近在FB看到有學弟在打廣告這本書 - It's django 用Python迅速打造Web應用,
說是他同學寫的,我還想說: 「喔~台大的學弟寫的。」
點開博客來的連結,咦~作者不就是我實驗室的,常常歌頌Python多好用的那個學弟嗎!?
當我還在寫有的沒的部落格文時,學弟竟然出了這麼專業標題的書!!
而且還上博客來通路販售,實在太威了~~
秉持著支持實驗室學弟 + 順便學P語言 + 博客來剛好有100元購物金(這不是主因~),
便下定了一本。收到後有種熟悉的感覺,很像在小時候圖書館裡常見的那種電腦叢書。
打開書皮...本來還期待左邊封頁是作者資訊跟大頭貼,結果只是廣告別的書(嘆)。
總共有21個章節,光是目錄就有10頁之多。到底這本書在講什麼?
第1章: Python各種基礎語法和程式觀念,連沒上過程式設計的人也看得懂。
第2章: 教你使用Python模組與套件,免費拿別人寫過的東西來利用。
第3章~第20章: 學習如何用Django來開發與架設網站。
原來這本書不只是單純Python教學,而是如何用它來寫網站!
但應該有不少人跟我一樣沒聽過Django這個詞,套句作者說的:
「大家可以把Django想成一個用Python寫成的寫網站工具,透過他你可以快速地、無負擔地寫出很酷炫的動態網站」
聽起來好像很強很有趣耶! 不過前提是要把這本書好好讀完才行,總共有372頁呢QQ
隨意翻開一頁,排版還蠻整齊的,有點像教科書的感覺...
嗯?這個標題...作者果然也是個鄉民啊。
身為認真的學姊我趕緊從第一章開始讀起,有學過C或其他程式語言的人可能會覺得一開始的內容很簡單,畢竟很多程式概念是相通的。
但漸漸的開始發現有些需要落落長的code才能完成的事,用Python竟然只要幾行甚至一行就能做到! 例如:
1. 讀檔,每一行如果有兩個integer用空白鍵隔開的話,就加起來印出來 -> 七行
2. 把清單裡有重複出現的item丟掉 -> 一行
3. 判斷清單裡所有值都是True -> 一行
好像很神奇呢! 尤其是讀檔方面簡單不囉嗦,用個"for line in f"就能自動拿出每一行來處理,不用在那邊判斷EOF啦~
而strip()可以自動拿掉首尾的空白鍵跟換行符號等,就不用再一個個濾掉特殊字元了...
例外則可以處理所有不符合規則的行,這點很值得善用,可節省很多判斷特例的功夫!!
不過俗話說萬丈高樓平地起,還是讓我們跟著作者的介紹順序,一步一腳印地從頭學下去吧!!
以下筆記第一章的語法重點,想看詳細的說明還是要看書才不會搞混喔~~
*** 資料型態 ***
Title | Operation | Print value |
整數與浮點數 |
result = 1+1 result+2 6%4 1.2e2-10 |
2 4 2 110.0 |
字串 (str) |
string_a = "hello" + ' ' + 'world' first_word = string_a[:5] string_f = 'My name is {name}, I am {age} years old.'.format(name='dokelung', age=27) # -*- coding: utf-8 -*- print '打上面那行後就可以用中文字串' |
'hello world' 'hello' 'My name is dokelung, I am 27 years old.'
'打上面那行後就可以用中文字串' |
變數可任意換型別 |
result = 2 result = 'hello' |
2 'hello' |
清單 (list) 儲存一串可改的資料 |
lst = ['dokelung', 27, None] lst[-2] lst[1] = 28 lst.append('New') first_element = lst.pop(0) lst.sort() person = ['dokelung', 27, [1988, 09, 30]] |
['dokelung', 27, None] 27 ['dokelung', 28, None] ['dokelung', 28, None, 'New'] [28, None, 'New'] [None, 28, 'New'] ['dokelung', 27, [1988, 09, 30]] |
元組 (tuple) 儲存一串不可改的資料 |
tup = (1, 2, 3) tup[0] tup[1] = 0 |
(1, 2, 3) 1 TypeError |
字典 (dictionary) 每筆資料都有不可改的key 對應可改的value |
dic = {'name':'dokelung', 'age':27} dic['name'] dic['age'] = 28 dic['gender'] = 'male' |
{'name':'dokelung', 'age':27} 'dokelung' {'name':'dokelung', 'age':28} {'gender': 'male', 'age': 27, 'name': 'dokelung'} |
*** 運算 ***
Title | Operation | Print value |
賦值 |
a = 1 |
1 |
自運算 |
a += 1 |
2 |
比較運算 |
0 < a <= 5 |
True |
邏輯運算 |
True and False or not True |
False |
身分運算 |
1 == 1 1 is 1 [1, 2, 3] == [1, 2, 3] [1, 2, 3] is [1, 2, 3] |
True True True False |
隸屬運算 |
'll' in 'hello world' |
True |
*** 流程控制 ***
Title | Operation |
if |
if 3 <= month <= 5: print 'Spring' elif 6 <= month <= 8: print 'Summer' elif 9 <= month <= 11: print 'Fall' else: print 'Winter' string = 'spring' if 3 <= month <= 5 else 'not spring' |
while |
n = 5 count = 0 while True: if count <= 0: count+= 1 continue print 'just print {n} times'.format(n=n) if count == n: break count += 1 |
for |
lst = [1, 2, 3, 4, 5] lst_square = [] for num in lst: lst_square.append(num**2) |
comprehension |
lst_square = [num**2 for num in lst] #[1, 4, 9, 16, 25] lst_square = [num**2 for num in lst if num%2==0] #[4, 16] scores = [88, 90, 100, 65, 78] score_dic = { idx:score for idx, score in enumerate(scores) } |
*** 輸入與輸出 ***
Title | Operation | Print value |
輸出 |
print 'dokelung', 27, print 'not change line' |
dokelung 27 not change line |
輸入 |
answer = 6 while True: str_num = input('Enter an integer in 1-10:') int_num = int(str_num) if int_num==answer: print('True!') break |
Enter an integer in 1-10:5 Enter an integer in 1-10:3 Enter an integer in 1-10:6 True!
|
讀檔 |
f = open('test_file') for line in f: print line.strip() f.seek(0) print f.readline() f.close() |
line1 line2 line3 line1
|
寫檔 |
lst = ["First line", "Second line"] with open('test_file', 'w') as f: for line in lst: print >>f, line |
First line |
例外 |
with open('test_file') as f: for line in f: try: a, b = line.strip().split() print int(a)+int(b) except: pass |
<test_file> 3 4 ??? %%% <output> 7 |
*** 函式 ***
Title | Operation | Print value |
int |
int('99') |
99 |
float |
float(99) |
99.0 |
bool |
bool('') |
False |
str |
str(99) |
'99' |
list |
list((1,2,3)) |
[1,2,3] |
set |
list(set([1,1,2,2])) |
[1,2] |
all |
all([True,True,False]) |
False |
any | any([True,True,False]) | True |
max | max([1,2,3,4,5]) | 5 |
min | min([1,2,3,4,5]) | 1 |
range |
for num in range(5): print num, for num in range(2,5): print num, |
0 1 2 3 4
2 3 4
|
len | print len({1:1, 2:2, 3:4}) | 3 |
def |
def power(base, exp): return base ** exp print power(exp=3, base=2) |
8 |
recursive |
def print_list(rec_lst): for item in rec_lst: if isinstance(item, list): print_list(item) else: print item rec_lst = [1, 2, [3, 4, 5], 6, [7, [8, 9]]] print_list(rec_lst) |
1 2 3 4 5 6 7 8 9 |
default value |
def add(a, b=1, c=1, d=1): return a+b+c+d print add(5,5) |
12 |
* |
def add(*tup): return sum(tup) lst[1,2,3,4] print add(lst[0], lst[1]) print add(lst[0], lst[1], lst[2]) print add(*lst) |
3 6 10 |
** |
dic = {'base':2, 'exp':3} print power(base=2, exp=3) print power(**dic) |
8 8 |
closure | def gen_power(base): def power(exp): return base ** exp return power power2 = gen_power(2) print power2(3) |
8 |
decorator |
def print_result(head): def decorator(func): def modified_func(*args,**kwargs): result = func(*args,**kwargs) print head, result return modified_func return decorator
@print_result(head='result:') def add(*tup): return sum(tup) #same as: add = print_result(head='result:') (add)
add(1,2,3,4,5) |
result: 15 |
*** 物件導向 ***
Title | Operation |
base class |
class Animal: def __init__ (self, name): self.name = name def run (self): print self.name, 'runs' def introduce_my_self (self): print "Hello, I'm", self.name |
derived class |
class Cat (Animal): def shout (self): print 'Meoq' class Dog (Animal): def shout (self): print 'Bark' class Human(Animal): def __init__ (self, name, id): if id <= 0: raise ValueError('id should be positive!') self.name = name self.id = id |
instance |
my_cat = Cat('Kitty') my_dog = Dog('One') me = Human('Dokelung', -1) |
哇~光是第一章就有這麼豐富實用的Python教學,何況後面還有20章呢!!
有沒有很迫不及待想看看怎麼用django寫網頁了呢?
PS. 額外放送一下第二章很實用的匯入模組法,方便外部丟入參數給程式跑:
Operation | Print value |
import sys print sys.argv |
$python test.py Hello World ['test.py', 'Hello', 'World'] |
最後附上作者詼諧有趣的推銷文案!! 有興趣的人快去買一本吧~~
想要簽名的話我可以幫你要喔XD!!
<< It’s Django - 用Python迅速打造Web應用 >>
---- 重點整理
不囉嗦,即日起就可以在各大網路書店購買到這本書囉!
博客來: http://www.books.com.tw/products/0010676433
天瓏: http://www.tenlong.com.tw/items/9789863475460
(金石堂、三民書局、誠品、pchome等通路都找的到喔,偷偷跟大家說,上面兩間是最便宜的,而且集中購買比較能夠衝刺銷售量)
另外目前在天瓏的實體書店也有販售囉(各家連鎖書店也會在近日陸續上架)
----這本書在講甚麼咧?
如果已經很清楚的朋友可以直接訂購不用看這部分的說明了,不了解的朋友也請先付款結帳,看完書你就知道了...好啦...我會解釋拉,別打我
* Python 是一個很容易學且應用很廣很強大的程式語言 (2014最受歡迎程式語言)
# 多容易學:
絕對是你這輩子看過程式語言裡面前三好學的,只要認真,不會程式的人一個星期可以學會,會寫程式的人一天可以學會。
# 應用多廣:
寫網站、寫爬蟲、做遊戲、畫動畫、繪圖製表、寫視窗程式、做科學計算、作統計分析、幫助做實驗、做IC design...你想的到的都行,不行也沒關係,你也可以自己來!
# 多強大:
Google 在用、NASA 在用、Dropbox 在用、YouTube 在用,還能不強大嗎!
* Django 是用 Python 寫成的 網站製作框架
跟大家談框架太複雜了(想知道更多還不看書!),大家可以把Django 想成一個用Python寫成的寫網站工具,透過他你可以快速地、無負擔地寫出很酷炫的動態網站
# 多快速:
熟了之後,一天可以寫一個 blog、討論區或是購物網站或簡易版 facebook、twiter 都沒問題
# 怎樣沒負擔
你只要跟著這本書學完,會了 Python 和 Django 之後,就可以橫行天下了,其他像是 SQL、資料庫還是有的沒的基本上可以先丟在一邊等心情好再研究
# 可以寫出多酷炫的網站
絕對不會讓大家只會做點一千萬次連結都長得一樣的網站,我們會讓你的網站具備動態的能力,包含隨時間改變的內容或是跟使用者互動的功能,想想我們可以讓你寫出一個FB耶,還不快試試
----這本書好在哪裡
淺顯易懂、循序漸進
淺顯易懂、循序漸進
淺顯易懂、循序漸進
很重要所以我說三次
"絕對讓你看得懂"是我們寫這本書的核心思想,當然,腦袋打結的時候,作者隨時給你問!
另外這本書是第一本"中文"的Django書,討厭英文或是找不到別本書的朋友快入手一本喔! (你幾乎找不到第二本)
----我們強烈推薦符合以下條件的朋友購買本書:
1. 想學做網站的朋友
這裡指的作網站是專業級的作網站,不是用 DreamWeaver 或是FrontPage 做的那種國小電腦課作品,不論你以前有沒有寫過程式,有了這本書,不論是部落格、討論區、個人網站、實驗室網站、社群網站、購物網站...你都能做得出來,也許你就是下一個馬克佐伯克(Mark Zuckerberg 臉書創辦人)。
2. 想學架網站的朋友
你知道做網站跟架網站是兩回事嗎?放心,我們的書不會放生你的,除了讓大家能夠做出網站,我們還詳盡地介紹如何架站! 真的很簡單!
3. 想學寫程式的朋友
你覺得你這輩子跟又酷又帥(又宅)的程式設計師毫無關係嗎? 現在我要大聲地跟你說:你錯了!只要會了 Python 這個程式語言,你也能輕鬆地踏入寫程式的領域,不論你是想要多學一種語言的程式高手,或是正在苦思該學習哪一門程式語言的初心者,又或是被 C/C++ 或是 Java 殘害已久(誤),本來想退出江湖或改行的各路大俠們,相信我,會了 Python,你也能重新成為武林盟主。
4. 只是想支持克倫、買來當紀念的朋友
買一本書,不用理由,恭喜你領悟人生真諦了!
留言列表