Python 101 — พื้นฐาน
ยินดีต้อนรับสู่ Python 101
หัวข้อที่มีชื่อว่า “ยินดีต้อนรับสู่ Python 101”ถ้าคุณเคยส่งงาน TypeScript ขึ้น production มาแล้ว คุณรู้จัก Python มากกว่าที่คิด เพราะแกนกลางของทั้งสองภาษาเป็น dynamically typed เหมือนกัน (TypeScript แปะเลเยอร์ type ตอน compile-time ส่วน Python 3 แปะ hint แบบ optional) ใช้ garbage collection เหมือนกัน และเน้น syntax ที่สื่อความหมายมากกว่าพิธีรีตองเหมือนกัน
สิ่งที่ต้องปรับมุมมองจริง ๆ เป็นเรื่องผิวเผินเสียส่วนใหญ่ คือ indentation เข้ามาแทนปีกกา ส่วน syntactic sugar หลายตัวของ JavaScript ก็มีเวอร์ชัน Python ที่สะอาดกว่ารออยู่
โมดูลนี้มีเก้าบทเรียน แต่ละบทโยงกลับไปหาโค้ดที่คุณรู้จักดีอยู่แล้ว เรียนจบแล้วคุณจะอ่านโปรเจกต์จริง เขียนสคริปต์ และสร้าง REST API ด้วย FastAPI ได้
ลองชิมรสชาติแบบเร็ว ๆ: โปรแกรมเดียวกันในทั้งสองภาษา
หัวข้อที่มีชื่อว่า “ลองชิมรสชาติแบบเร็ว ๆ: โปรแกรมเดียวกันในทั้งสองภาษา”โปรแกรมเล็ก ๆ ตัวนี้ filter รายชื่อแล้วทักทาย เขียนด้วย TypeScript ก่อน แล้วตามด้วย Python ตรรกะเดียวกัน ผลลัพธ์เดียวกัน ต่างกันแค่ syntax
const names: string[] = ["Alice", "Bob", "Carol", "Dave"];
const short = names.filter((n) => n.length <= 4);
for (const name of short) { console.log(`Hello, ${name}!`);}
// Hello, Bob!// Hello, Dave!names: list[str] = ["Alice", "Bob", "Carol", "Dave"]
short = [n for n in names if len(n) <= 4]
for name in short: print(f"Hello, {name}!")
# Hello, Bob!# Hello, Dave!ลองเล่นดู
หัวข้อที่มีชื่อว่า “ลองเล่นดู”names: list[str] = ["Alice", "Bob", "Carol", "Dave"]
short = [n for n in names if len(n) <= 4]
for name in short: print(f"Hello, {name}!")Loading Python runtime (first run only)…
สิ่งที่คุณจะได้เรียนรู้
หัวข้อที่มีชื่อว่า “สิ่งที่คุณจะได้เรียนรู้”| บทเรียน | สิ่งที่คุณรู้อยู่แล้ว (TS) | สิ่งที่คุณจะได้เรียน (Python) |
|---|---|---|
| Variables | let / const + static types | Dynamic typing, type hints, ค่าคงที่แบบ UPPER_CASE |
| Functions | Arrow & function | def, default args, keyword args, *args/**kwargs |
| Control Flow | if/else, for, switch | if/elif, for...in, match/case |
| Collections | Array, object, Map, Set | list, tuple, dict, set, slicing |
| Classes | class, constructor, this | class, __init__, self, dunders |
| Modules | ESM import/export, npm | import, from ... import, pip |
| Errors | try/catch/finally | try/except/else/finally, raise |
| None & Truthiness | null, undefined, ค่า falsy | None, is None, truthiness ของ Python |