Skip to content. | Skip to navigation

Personal tools

Navigation

You are here: Home / Tutorial / Python Learning Paths

Python Learning Paths

狹義地說,學習 Python 的核心部份,是指認識它的關鍵字和語法,Python 的關鍵字數量大約40個,一天學三個的話,不超過兩星期就能學完,這部份算是簡單,需要持續精進的部份是,去理解各式資料結構、設計方法、函式庫、開發架構,以便找到適合自己需要的應用方式,或者說,去發掘所謂的最佳實務。 https://medium.freecodecamp.org/lets-clear-up-the-confusion-around-the-slice-splice-split-methods-in-javascript-8ba3266c29ae https://edgecoders.com/coding-tip-try-to-code-without-loops-18694cf06428 https://blog.elpassion.com/ruby-on-rails-gamechanger-3da46ae3cd6 https://medium.com/@alexmaisiura/python-how-to-reduce-memory-consumption-by-half-by-adding-just-one-line-of-code-56be6443d524 https://medium.com/@dawsoneliasen/how-to-build-up-an-intuition-for-recursion-986032c2f6ad https://medium.com/@chrisdoucette15/building-a-hashing-tool-with-python-3afe34db74e5 https://stackoverflow.com/q/2846653 multithreading https://medium.com/@dboyliao/%E8%81%8A%E8%81%8A-python-closure-ebd63ff0146f

寫程式的目的之一,是為了解決問題,這種情況的成果,可以視為一種公文嗎? 換個角度,在創造的場合,寫程式才容易開始具備藝術性,這比較容易讓人信服。程式語言是人類與電腦的溝通工具,轉化成機器語言時,可能只有功能性、而沒有藝術性可言,因此藝術性是對閱讀程式碼的另一群人而言,可以這樣說嗎?

純新手,推薦先閱讀前人的經驗談,看些容易消化的入門影片 #1 #2 Example

為何 Python 速度慢: GIL, Interpreted, Dynamic

學過其他語言的朋友,可以從高處綜觀異同處,例如 C 的語法大量被沿用,許多關鍵字跟 C 是同義,明顯的差別在於 Python 取消括號而採用縮排。Java 比較 Java Equivalent for Python Map Function Javascript Go + Python. Design Pattern: Composite and a Decorator. “Warning: Your programming career” @vardanator_pi

Full Stack Python 指引可能的應用方式. Python vs R

InteractivePython.org Common Trolls

Michael Driscoll: Python 101 201 wxPython Cookbook

Installation

Portable Python: Python(x,y) WinPython Anaconda manage multiple versions Python Anywhere

for Windows

conda create -n tensorflow python=3.5

source active tensorflow

Basic

IPython Magic Commands Lesser Known Usages Passed by Assignment *args **kwargs formatting: __format__() Essential Python Resources Chrome Extensions IDE Comparison decorator itertools.combinations(mylist, 2) __iter__ __getitem__ decorator 可用來變造函式的行為,descriptor 是一套有特殊約定的實作規則,例如 @property 功能乃基於 descriptor 來實現。如果是寫 API 給其他工程師使用,這兩個有機會能讓 API 更簡潔易用,如果不是這類場合,只需會使用即可。

import sys

def usage():
    print("run_mod.py: a module demo")
    print("option: -v, show version info"

if sys.argv[1] == '-v':
    print("Version 0.1")
else:
    usage()

if __name__ == '__main__':
    print("Main Runs")

sys.path.append('../') 可以 import 相對路徑 但要避免使用 import 陷阱

使用 _ 前置字 (例 safe_format 變成 _safe_format) 的函式模組,可以避免被外部程式呼叫。五種使用 _ 的情況 四種範例 載入 underscore 函式時可能要更改名稱 利用 sys.modules 查詢模組載入狀況

perl equiv: my $result = `grep XXX $filelist`

Learn Python in Y Minutes Python For Beginners Regular Expression

Awesome Python DeadSnakes for Debian

Simple Email Crawler String vs List of Chars 使用 lambda 當作 key d = {lambda x=[10]: print(x.append(11), x) : 3} Dictionary vs JSON

Python Challenge App Java: Primitive Type Functional Programming in Python Turn Functional into Comprehension OOP #1 #2 #3

繼承的好範例 Object vs Type vs Metaclass: 繼承的 class 透過 assert 可以事先檢查

staticmethod vs classmethod

Popularity: Hackathon 除錯時常要理解 Comparison 的細節

The Dynamic Language Runtime and The Iron Languages

person groups was expected to be iterable but was None

lambda is an expression, not a statement

-        terms = filter(lambda term: term.value in self.context.groups,
+        terms = filter(lambda term: term.value in (self.context.groups or []),
                        vocabulary)

Private, Protected, Public: Python 並沒有機制阻止變數的存取或物件方法的呼叫,純粹透過社群慣例來處理這類議題,也就是 _ 和 __ 前置字

Name Mangling:

具備 DocString 的函式會成為 Public 除非明確地指定為 Private。

Recursive 範例

Descriptor: @property

Packaging Ecosystem 2016

Intermediate

Binary Search vs Galloping Search

def binary_search(array, target):
    first = 0
    last = len(array) - 1
    comparisons = 0
 
    while first <= last:
        comparisons += 1
        midpoint = (first + last) // 2
        if array[midpoint] == target:
            return (midpoint, comparisons)
        elif array[midpoint] > target:
            last = midpoint - 1
        else:
            first = midpoint + 1
    return (first, comparisons)

def galloping_search(array, target):
 
    if array[0] == target:
        return (0, 1)
    comparisons = 1
 
    # Stage 1 - Find the large index
    size = len(array)
    jump = 1
    cursor = 0
    while cursor < size and array[cursor] < target:
        comparisons += 1
        jump *= 2
        cursor += jump
 
    # Stage 2 - slice the array and run the binary search
    array = array[(cursor // 2):min(cursor, size) + 1]
    sub_search = binary_search(array, target)
 
    return ((cursor // 2) + sub_search[0] - 1, sub_search[1] + comparisons)

Check List Subclass asyncore-file.dispatcher IPython Notebooks ipywidgets jupyter-js-widgets

IPython 字型設定是透過 CSS,檔案預設位於 ~/.ipython/profile_default/static/custom/custom.css,也可以透過 Theme 來設定。

http://try.github.io/

The Data Scientist Skillset

Documentation Contribution Workflow

Egg Introduction

Codio: Django, Pyramid

Plone Document Check List

BananaPi + USB Camera

__future__

UnboundLocalError: local variable 'k' referenced before assignment 當匯入的資料有欄位缺漏時 產生 split() 結果無法對應 k,v 兩個值

try:
    k, v = bs(obj.getText()).find('p', {'class': 'ct'}).text.split(': ')
    if k == u'TYPE_HZ': return v # OK
except ValueError:
    pass
#if k == u'TYPE_HZ': return v # Error

Advanced

Namespace by Eric Snow 三種 Namespace Container: mapping-based (VOLATILE) attribute-based (STABLE) property-based (STATIC)

obj[key]            obj.__getitem__(key)
obj[key] = value    obj.__setitem__(key, value)
del obj[key]        obj.__delitem__(key)
key in obj          obj.__contains__(key)

GIL Concurrency Issue: Grumpy Numpy integration

Double Underscore

Anti-Patterns Beautiful and Idiomatic

Google Tech Talk 2007:

Everything is Runtime: Compiletime 也是 Runtime,像 .pyc 檔案是 Cached or Borrowed Runtime 預設用來節省時間。

Execution happens in Namespaces: module, function, class 都有自己的 Namespace。

Modules are executed Top-to-Bottom: 就像 Script 方式。載入 Module 後,會從頭到尾執行內容。

def and class statements are Runtime: 執行到的時候,相關的 Function 才建立。一般的 Python 程式碼會被編譯成 Code Object 形式,執行 def 時會把這部份的 Code Object 額外包裝成 Function Object (包含參數)。

def is primarily an assignment statement: 觀察 Bytecode 的話,會發現它跟一般的 assignment 相同。Function 的內容在執行 def 時會被先編譯,但不是馬上被執行,要等到呼叫時才執行。

class statement executes suite (code block) in a separate namespace (a dict): 當 class 執行結束時,會額外建立 Class Object (包含 Namespace 內容),裡面定義的 Function (第一個參數必須是 self) 會變成 Method。

not all callables are functions, even though all functions are callables: range() is not a function call but a call to a type, pow() and sqrt() and like that are true functions, as indicated by feeding them to type().

range() 只能處理整數 numpy.arange() 可以處理浮點數

Modern Dictionary by Raymond Hettinger

How to Write Reusable Code Moving to Python3

The Tao of Python

Concurrent

Python Source Formatter

Big Data XMind

PyCharm The Good Parts Tips Managing Plone Project with PyCharm

List of List: Flat List

res = ['{} . {} = {}'.format(x, y, x * y) for x in range(10) for y in range(10)]

Generator Generator Expression vs List Comprehension Coroutines and Subroutines Tricks for System Programmers

return 代表 function 執行結束,把控制權交回,這種方式通常被稱為 Subroutine。應至少學習一個非同步架構,最好是 asyncio,未來開發不可能離開 websocket 等即時技術,http/2 也會變流行。

Class and Object Relation 繼承後停用物件方法

String Formatting: % vs format

exit vs sys.exit

Desktop Development: #1 #2

Test Driven Development Modules with Different Versions

Stream: Why We Switch from Python to Go

32-bit platform: time.localtime()

Raspberry Pi

Micro SD Card dd on MacOS Tweet from Raspberry Pi with Tweepy Raspberry Pi: Full Stack Chromecast replacer 中文輸入

Excel VBA OpenOffice Calc

DIY Custom Linux OS with Buildroot Scientific Computing by Fernando Perez

Naming Things: Don't Use Reserved Words

$ python setup.py --classifiers
Framework :: Plone
Framework :: Plone :: 4.3
Programming Language :: Python
Programming Language :: Python :: 2.6
Programming Language :: Python :: 2.7
Topic :: Software Development :: Libraries :: Python Modules

Proxying to a Python Web Application Running in Docker

Super considered super!

Python vs C/C++ in Embedded Systems

Pythonic or Idiomatic Python

一個人必須深刻了解一種語言,才能適當使用成語來表達意思,能善用 Python 成語和禪心,就是 Pythonic

Python3 比 Python2 更容易寫出好品質的程式碼 Python3 Patterns, Recipes, and Idioms 移植範例: zope.app.wsgi

Never Write for loops Again Debugging List Comprehension Design Patterns

Python Worst Practice

Why Ruby Isn't Python

Drew Perttula: I think programmers do not realize which code is an idiom- the C programmer will tell you that she needed to index elements in the list, and wanted the index to start at 0 and run to the end. What's idiomatic about clearly telling the language what you want to do? C programmers probably do realize that x++ or main() {} aren't likely to look the same in python, but I'm not surprised they get mad when there's no switch/case. Python does a lot of work for you, so not only will things not always port directly from unpython to python, but they're often unnecessary once you get here.

def numbers_to_strings(argument):
  switcher = {
    0: "zero",
    1: "one",
    2: "two",
  }
  return switcher.get(argument, "nothing")

Modernised C code: use PyVarObject_HEAD_INIT

person groups was expected to be iterable but was None

Data Class in Python 3.7