🎯 What this lesson gives you · สิ่งที่คุณจะได้จากบทเรียนนี้ · 本课能给你什么
🇬🇧 English
By the end of this page you will be able to sit down at a Maca Windows PC, open the Terminal, create a Python file with your own hands, and run it. Nothing is assumed. We start with the computer itself.
You need a Maca Windows PC, about two hours, and patience. Every step has a command you can copy.
The path: your MacPC → files and folders → the Terminal → install Python → write a file with vim → run it → IDLE → Jupyter → two full lessons in a notebook.
🇹🇭 ไทย
เมื่ออ่านหน้านี้จบ คุณจะนั่งหน้าเครื่อง MacWindows เปิด Terminal สร้างไฟล์ Python ด้วยมือตัวเอง แล้วสั่งให้มันทำงานได้ เราเริ่มจากศูนย์ เริ่มจากตัวเครื่องคอมพิวเตอร์เลย
คุณต้องมีเครื่อง MacWindows เวลาประมาณสองชั่วโมง และความอดทน ทุกขั้นตอนมีคำสั่งให้คัดลอกได้
เส้นทาง: เครื่อง MacWindows → ไฟล์และโฟลเดอร์ → Terminal → ติดตั้ง Python → เขียนไฟล์ด้วย vim → รันไฟล์ → IDLE → Jupyter → บทเรียนเต็มสองบทใน notebook
🇨🇳 中文
读完本页后,你能坐到一台 MacWindows 电脑 前面,打开终端,亲手创建一个 Python 文件,然后运行它。本课不预设任何基础,我们从电脑本身讲起。
你需要一台 MacWindows 电脑、大约两小时,还有耐心。每一步都有可以直接复制的命令。
路线:认识你的 Mac电脑 → 文件和文件夹 → 终端 → 安装 Python → 用 vim 写文件 → 运行它 → IDLE → Jupyter → notebook 里的两整课。
🎬 Where Python comes from · Python มาจากไหน · Python 是从哪来的
🇬🇧 English
Python was written by one man. Guido van Rossum, a Dutch programmer, started it over the Christmas holiday of 1989. His office was shut and he was bored.
He led the project for nearly thirty years. Python users called him the Benevolent Dictator For Life — a joke title, but he really did settle every argument. He stepped down in 2018.
And the name is not the snake. He was reading the scripts of Monty Python's Flying Circus, a British comedy show, and wanted a name that was short and a little silly. The snakes came later, on the logo.
This matters more than it sounds. It is why Python's own documentation is funny, and why the language values code you can read over code that looks clever.
🇹🇭 ไทย
Python เขียนขึ้นโดยคนคนเดียว Guido van Rossum โปรแกรมเมอร์ชาวดัตช์ เริ่มทำมันช่วงวันหยุดคริสต์มาสปี 1989 ตอนนั้นที่ทำงานปิดและเขาเบื่อ
เขาดูแลโครงการนี้เกือบสามสิบปี ผู้ใช้ Python เรียกเขาว่า Benevolent Dictator For Life (เผด็จการใจดีตลอดชีพ) เป็นตำแหน่งล้อเล่น แต่เขาเป็นคนตัดสินทุกข้อถกเถียงจริง ๆ เขาวางมือในปี 2018
และชื่อนี้ไม่ได้มาจากงู ตอนนั้นเขากำลังอ่านบทของ Monty Python's Flying Circus รายการตลกอังกฤษ และอยากได้ชื่อสั้น ๆ ที่ดูกวน ๆ ส่วนงูมาทีหลัง ตอนทำโลโก้
เรื่องนี้สำคัญกว่าที่คิด มันคือเหตุผลที่คู่มือของ Python อ่านแล้วขำ และเป็นเหตุผลที่ภาษานี้ให้ค่ากับโค้ดที่อ่านออก มากกว่าโค้ดที่ดูฉลาด
🇨🇳 中文
Python 是一个人写出来的。荷兰程序员 Guido van Rossum 在 1989 年圣诞假期开始动手,因为办公室关门了,他很无聊。
他主导这个项目将近三十年。Python 用户称他为「仁慈的终身独裁者」—— 一个玩笑头衔,但所有争论确实由他拍板。他在 2018 年卸任。
而这个名字不是来自蛇。当时他在读英国喜剧节目 Monty Python's Flying Circus 的剧本,想要一个又短又有点无厘头的名字。蛇是后来做 logo 时才出现的。
这件事比听上去更重要。它解释了为什么 Python 的官方文档会讲笑话,也解释了为什么这门语言看重读得懂的代码,而不是看起来聪明的代码。
1️⃣ The words programmers use · คำศัพท์ที่โปรแกรมเมอร์ใช้ · 程序员用的词
🇬🇧 English
Programming is taught in English, so the words come first. Learn these twelve and most instructions stop being mysterious.
Notice that many of them are ordinary English words doing a special job. A file is a file. To run something is to make it go. The words are not hard — they are just precise.
Click any card to hear it said aloud.
🇹🇭 ไทย
การเขียนโปรแกรมสอนกันเป็นภาษาอังกฤษ ดังนั้นเราเริ่มที่คำศัพท์ก่อน จำ 12 คำนี้ให้ได้ แล้วคำสั่งส่วนใหญ่จะไม่ลึกลับอีกต่อไป
สังเกตว่าหลายคำเป็นคำอังกฤษธรรมดาที่ถูกใช้ในความหมายเฉพาะ file ก็คือไฟล์ run ก็คือทำให้มันทำงาน คำพวกนี้ไม่ยาก เพียงแต่มีความหมายที่ชัดเจนมาก
คลิกที่การ์ดเพื่อฟังเสียงอ่าน
🇨🇳 中文
编程是用英语教的,所以先learn单词。把这十二个词记住,大部分说明书就不再神秘了。
注意其中很多都是普通英语词,只是在这里有特定含义。file 就是文件,run 就是让它跑起来。这些词不难,只是含义很精确。
点任意卡片可以听发音。
.py.一份保存下来的文档。Python 程序就是以 .py 结尾的文件。ไฟล์หนึ่งไฟล์ โปรแกรม Python คือไฟล์ที่ลงท้ายด้วย .pyls is a command.你输入的一条指令,然后按回车。คำสั่งหนึ่งบรรทัดที่พิมพ์แล้วกด Returnpython3python hello.py runs a file.让电脑照文件里写的去做。ทำให้คอมพิวเตอร์ทำตามที่เขียนไว้ในไฟล์vim is an editor.用来输入和修改文字的程序。โปรแกรมสำหรับพิมพ์และแก้ข้อความage = 28 puts 28 in a box called age.存放一个值的名字。ชื่อที่เก็บค่าเอาไว้install it, then import it.别人写好、你可以直接用的代码。โค้ดที่คนอื่นเขียนไว้ให้เราใช้2️⃣ Your Mac — the parts that matter · รู้จักเครื่อง Mac ของคุณ · 认识你的 Mac
🇬🇧 English
You do not need to understand the whole machine. Four parts matter for programming.
The CPU does the thinking. The RAM holds whatever is open right now, and forgets it when you shut down. The SSD stores your files forever. The operating system — macOS — sits between you and all of that.
When Python runs your file, the SSD hands the file to RAM, and the CPU does what it says. That is the whole story.
🇹🇭 ไทย
คุณไม่จำเป็นต้องเข้าใจทั้งเครื่อง มีอยู่สี่ส่วนที่สำคัญต่อการเขียนโปรแกรม
CPU ทำหน้าที่คิด RAM เก็บสิ่งที่กำลังเปิดอยู่ตอนนี้ และลืมทุกอย่างเมื่อปิดเครื่อง SSD เก็บไฟล์ของคุณไว้ถาวร ส่วน ระบบปฏิบัติการ หรือ macOS อยู่ตรงกลางระหว่างคุณกับทั้งหมดนั้น
เวลา Python รันไฟล์ของคุณ SSD ส่งไฟล์ให้ RAM แล้ว CPU ก็ทำตามที่เขียนไว้ เรื่องมีแค่นี้
🇨🇳 中文
你不需要理解整台机器。对编程来说,有四个部分重要。
CPU 负责思考。内存(RAM)装着此刻打开的东西,关机就忘光。固态硬盘(SSD)永久保存你的文件。操作系统(macOS)则夹在你和这一切之间。
当 Python 运行你的文件时,SSD 把文件交给内存,CPU 照着做。整个故事就是这样。
2️⃣ Your PC — the parts that matter · รู้จักเครื่องพีซีของคุณ · 认识你的电脑
🇬🇧 English
You do not need to understand the whole machine. Four parts matter for programming.
The CPU does the thinking. The RAM holds whatever is open right now, and forgets it when you shut down. The SSD (or hard disk) stores your files forever. Windows sits between you and all of that.
When Python runs your file, the disk hands the file to RAM, and the CPU does what it says. That is the whole story, and it is the same on every computer.
🇹🇭 ไทย
คุณไม่จำเป็นต้องเข้าใจทั้งเครื่อง มีอยู่สี่ส่วนที่สำคัญต่อการเขียนโปรแกรม
CPU ทำหน้าที่คิด RAM เก็บสิ่งที่กำลังเปิดอยู่ และลืมทุกอย่างเมื่อปิดเครื่อง SSD (หรือฮาร์ดดิสก์) เก็บไฟล์ไว้ถาวร ส่วน Windows อยู่ตรงกลางระหว่างคุณกับทั้งหมดนั้น
เวลา Python รันไฟล์ ดิสก์ส่งไฟล์ให้ RAM แล้ว CPU ก็ทำตามที่เขียนไว้ เรื่องมีแค่นี้ และเหมือนกันทุกเครื่อง
🇨🇳 中文
你不需要理解整台机器。对编程来说,有四个部分重要。
CPU 负责思考。内存(RAM)装着此刻打开的东西,关机就忘光。固态硬盘(或机械硬盘)永久保存文件。Windows 夹在你和这一切之间。
当 Python 运行你的文件时,硬盘把文件交给内存,CPU 照着做。就是这样,每台电脑都一样。
C: drive.长期存储。在 Windows 上通常就是 C: 盘。ที่เก็บถาวร บน Windows มักเป็นไดรฟ์ C:3️⃣ Files and folders — creating and editing on the Mac · ไฟล์และโฟลเดอร์ — สร้างและแก้ไขบนเครื่อง Mac · 文件和文件夹 —— 在 Mac 上新建和编辑
🇬🇧 English
Finder is the app that shows your files. Its icon is the blue smiling face, always first in the Dock. Everything you make lives somewhere in Finder.
Your files sit in your Home folder, which the Terminal writes as ~. Inside it are Desktop, Documents, Downloads, Pictures and Music.
Make a folder for your code now. Open Finder, click Documents, then press ⌘⇧N. Type Code and press Return. Everything in this lesson goes in there.
To create a text file with the mouse: open TextEdit, press ⌘⇧T to switch to plain text, type, then save it into your Code folder. Python files must be plain text — never rich text.
🇹🇭 ไทย
Finder คือแอปที่แสดงไฟล์ของคุณ ไอคอนเป็นหน้ายิ้มสีฟ้า อยู่ตัวแรกใน Dock เสมอ ทุกอย่างที่คุณสร้างจะอยู่ที่ไหนสักแห่งใน Finder
ไฟล์ของคุณอยู่ใน Home folder ซึ่ง Terminal เขียนแทนด้วย ~ ข้างในมี Desktop, Documents, Downloads, Pictures และ Music
สร้างโฟลเดอร์สำหรับโค้ดเดี๋ยวนี้เลย เปิด Finder คลิก Documents แล้วกด ⌘⇧N พิมพ์ Code แล้วกด Return ทุกอย่างในบทเรียนนี้จะอยู่ในนั้น
ถ้าจะสร้างไฟล์ข้อความด้วยเมาส์ ให้เปิด TextEdit กด ⌘⇧T เพื่อเปลี่ยนเป็นข้อความธรรมดา พิมพ์ แล้วบันทึกลงในโฟลเดอร์ Code ไฟล์ Python ต้องเป็นข้อความธรรมดาเท่านั้น
🇨🇳 中文
访达(Finder)是显示文件的程序,图标是蓝色笑脸,永远排在程序坞第一个。你做的每样东西都在 Finder 里的某个位置。
你的文件放在主目录里,终端里写作 ~。里面有 Desktop、Documents、Downloads、Pictures 和 Music。
现在就建一个放代码的文件夹。打开 Finder,点 Documents,按 ⌘⇧N,输入 Code,回车。本课的一切都放进去。
用鼠标建文本文件:打开 TextEdit,按 ⌘⇧T 切换成纯文本,输入内容,保存到 Code 文件夹。Python 文件必须是纯文本,不能是富文本。
rm is not.移到废纸篓,还能找回。终端的 rm 不行。.py, .txt, .ipynb. It tells the Mac what kind of file it is.点号后面那部分,告诉电脑这是什么文件。3️⃣ Files and folders — creating and editing on Windows · ไฟล์และโฟลเดอร์ — สร้างและแก้ไขบน Windows · 文件和文件夹 —— 在 Windows 上新建和编辑
🇬🇧 English
File Explorer is the app that shows your files. Its icon is the yellow folder on the taskbar. Open it with WinE.
Your files live in C:\Users\YourName. Inside it are Desktop, Documents, Downloads, Pictures and Music. Windows writes paths with a backslash \, not a forward slash.
Make a folder for your code now. Open File Explorer, click Documents, then press CtrlShiftN. Type Code and press Enter.
Do this one thing first. In File Explorer click View → Show → File name extensions. Windows hides them by default, which means a file called hello.py.txt looks exactly like hello.py. That single setting saves hours of confusion.
To make a text file with the mouse: open Notepad, type, then Save As into your Code folder. Set Save as type to All Files and name it hello.py. Never use Word or WordPad — Python files must be plain text.
🇹🇭 ไทย
File Explorer คือแอปที่แสดงไฟล์ของคุณ ไอคอนเป็นโฟลเดอร์สีเหลืองบนแถบงาน เปิดด้วย WinE
ไฟล์ของคุณอยู่ที่ C:\Users\YourName ข้างในมี Desktop, Documents, Downloads, Pictures และ Music Windows เขียนพาธด้วย แบ็กสแลช \ ไม่ใช่สแลชธรรมดา
สร้างโฟลเดอร์สำหรับโค้ดเดี๋ยวนี้เลย เปิด File Explorer คลิก Documents แล้วกด CtrlShiftN พิมพ์ Code แล้วกด Enter
ทำสิ่งนี้ก่อนเลย ใน File Explorer คลิก View → Show → File name extensions ปกติ Windows ซ่อนนามสกุลไฟล์ไว้ ทำให้ไฟล์ชื่อ hello.py.txt ดูเหมือน hello.py เป๊ะ ๆ การตั้งค่าอันเดียวนี้ช่วยประหยัดเวลาได้เป็นชั่วโมง
ถ้าจะสร้างไฟล์ข้อความด้วยเมาส์ ให้เปิด Notepad พิมพ์ แล้ว Save As ลงในโฟลเดอร์ Code ตั้ง Save as type เป็น All Files แล้วตั้งชื่อ hello.py อย่าใช้ Word หรือ WordPad เพราะไฟล์ Python ต้องเป็นข้อความธรรมดา
🇨🇳 中文
文件资源管理器是显示文件的程序,图标是任务栏上的黄色文件夹。用 WinE 打开。
你的文件放在 C:\Users\YourName 里,里面有 Desktop、Documents、Downloads、Pictures 和 Music。Windows 的路径用反斜杠 \,不是斜杠。
现在就建一个放代码的文件夹。打开文件资源管理器,点 Documents,按 CtrlShiftN,输入 Code,回车。
先做这一件事。在资源管理器里点 查看 → 显示 → 文件扩展名。Windows 默认把扩展名藏起来,于是一个叫 hello.py.txt 的文件看上去和 hello.py 一模一样。就这一个设置,能省下好几小时的困惑。
用鼠标建文本文件:打开记事本,输入内容,另存为到 Code 文件夹,把保存类型设成所有文件,命名为 hello.py。不要用 Word 或写字板 —— Python 文件必须是纯文本。
hello.py from hello.py.txt.一定要打开,否则分不清这两个文件。.py, .txt, .ipynb. It tells Windows what kind of file it is.点号后面那部分,告诉系统这是什么文件。4️⃣ The keyboard shortcuts worth memorising · คีย์ลัดที่ควรจำ · 值得背下来的快捷键
🇬🇧 English
Every one of these saves you a trip to the mouse. Learn the first three today and add the rest as you need them.
On a Mac the ⌘ key does what Ctrl does on Windows. In the Terminal, though, Ctrl comes back — CtrlC stops a running program.
🇹🇭 ไทย
ทุกอันช่วยให้คุณไม่ต้องเอื้อมไปหาเมาส์ วันนี้จำสามอันแรกให้ได้ก่อน ที่เหลือค่อยเพิ่มเมื่อต้องใช้
บน Mac ปุ่ม ⌘ ทำหน้าที่เหมือนปุ่ม Ctrl บน Windows แต่ใน Terminal ปุ่ม Ctrl กลับมาใช้อีกครั้ง — CtrlC ใช้หยุดโปรแกรมที่กำลังทำงาน
🇨🇳 中文
每一个都能省下一次伸手去摸鼠标。今天先记住前三个,其余的用到再加。
Mac 上的 ⌘ 相当于 Windows 的 Ctrl。但在终端里 Ctrl 又回来了 —— CtrlC 用来停止正在运行的程序。
4️⃣ The keyboard shortcuts worth memorising · คีย์ลัดที่ควรจำ · 值得背下来的快捷键
🇬🇧 English
Every one of these saves you a trip to the mouse. Learn the first three today.
One thing to watch: CtrlC copies almost everywhere, but inside the terminal it stops the running program. Same keys, completely different job.
🇹🇭 ไทย
ทุกอันช่วยให้คุณไม่ต้องเอื้อมไปหาเมาส์ วันนี้จำสามอันแรกให้ได้ก่อน
มีอย่างหนึ่งที่ต้องระวัง CtrlC ใช้คัดลอกได้เกือบทุกที่ แต่ในเทอร์มินัลมันคือการหยุดโปรแกรม ปุ่มเดียวกัน แต่คนละหน้าที่
🇨🇳 中文
每一个都能省下一次伸手摸鼠标。今天先记住前三个。
有一点要注意:CtrlC 在几乎所有地方都是复制,但在终端里是停止正在运行的程序。同样的键,完全不同的作用。
5️⃣ The Terminal — where Python lives · Terminal — ที่ที่ Python อยู่ · 终端 —— Python 住的地方
🇬🇧 English
The Terminal is a window where you type commands instead of clicking. It looks unfriendly and it is completely safe. Open it with ⌘Space, type terminal, press Return.
You will see a prompt ending in $ and a blinking block. That means "I am waiting". Type a command, press Return, read the answer.
Only four commands matter to start: pwd (where am I), ls (what is here), cd (go somewhere), mkdir (make a folder).
Practise in the simulator below. It is a real working terminal in your browser — nothing you type can break anything. Type help to see every command it knows.
🇹🇭 ไทย
Terminal คือหน้าต่างที่คุณพิมพ์คำสั่งแทนการคลิก หน้าตาอาจดูน่ากลัวแต่ปลอดภัยสนิท เปิดด้วย ⌘Space พิมพ์ terminal แล้วกด Return
คุณจะเห็น prompt ที่ลงท้ายด้วย $ และเคอร์เซอร์กะพริบ นั่นแปลว่า "กำลังรออยู่" พิมพ์คำสั่ง กด Return แล้วอ่านคำตอบ
ตอนเริ่มต้นมีแค่สี่คำสั่งที่สำคัญ pwd (ฉันอยู่ที่ไหน) ls (มีอะไรอยู่ตรงนี้) cd (ไปที่อื่น) mkdir (สร้างโฟลเดอร์)
ลองฝึกในเครื่องจำลองข้างล่างนี้ มันเป็นเทอร์มินัลที่ใช้งานได้จริงในเบราว์เซอร์ พิมพ์อะไรก็ไม่พัง พิมพ์ help เพื่อดูคำสั่งทั้งหมด
🇨🇳 中文
终端是一个用打字下命令、而不是点鼠标的窗口。它看起来不友好,其实完全安全。用 ⌘Space 打开,输入 terminal,回车。
你会看到一个以 $ 结尾的提示符和一个闪烁的方块。那表示"我在等你"。输入命令,回车,读结果。
开始只需要四个命令:pwd(我在哪)、ls(这里有什么)、cd(去别处)、mkdir(建文件夹)。
在下面的模拟器里练。它是浏览器里一个真能用的终端,你怎么打都不会弄坏东西。输入 help 看它认识的所有命令。
pwdlsls -la shows more.列出这个文件夹里的东西。แสดงว่ามีอะไรอยู่ในโฟลเดอร์นี้cdcd .. goes up one. cd ~ goes home.cd .. 上一层,cd ~ 回主目录。cd .. ขึ้นหนึ่งชั้น cd ~ กลับบ้านmkdirmkdir Code creates a folder called Code.新建文件夹。สร้างโฟลเดอร์ใหม่touchtouch hello.py.建一个空文件。สร้างไฟล์เปล่าcat notes.txtgrep Chiang notes.txt-i ignores capital letters, -n shows line numbers, -r searches a whole folder.只打印包含你要找的内容的那些行。พิมพ์เฉพาะบรรทัดที่มีข้อความที่คุณหาrm5️⃣ The Terminal — where Python lives · Terminal — ที่ที่ Python อยู่ · 终端 —— Python 住的地方
🇬🇧 English
On Windows the terminal is called PowerShell, and the app that holds it is Windows Terminal. Press Win, type terminal, press Enter. On older machines type powershell instead.
You will see a prompt ending in >, like PS C:\Users\You>. That means "I am waiting". Type a command, press Enter, read the answer.
Only four commands matter to start: pwd (where am I), ls (what is here), cd (go somewhere), mkdir (make a folder). PowerShell has its own long names for these, but it accepts the short ones, so what you learn here works on both systems.
🇹🇭 ไทย
บน Windows เทอร์มินัลเรียกว่า PowerShell และแอปที่ใช้เปิดคือ Windows Terminal กด Win พิมพ์ terminal แล้วกด Enter ถ้าเครื่องเก่าหน่อยให้พิมพ์ powershell แทน
คุณจะเห็น prompt ที่ลงท้ายด้วย > เช่น PS C:\Users\You> นั่นแปลว่า "กำลังรออยู่" พิมพ์คำสั่ง กด Enter แล้วอ่านคำตอบ
ตอนเริ่มต้นมีแค่สี่คำสั่งที่สำคัญ pwd (ฉันอยู่ที่ไหน) ls (มีอะไรอยู่ตรงนี้) cd (ไปที่อื่น) mkdir (สร้างโฟลเดอร์) PowerShell มีชื่อยาว ๆ ของมันเอง แต่ก็รับชื่อสั้นเหล่านี้ด้วย สิ่งที่คุณเรียนที่นี่จึงใช้ได้ทั้งสองระบบ
🇨🇳 中文
在 Windows 上终端叫 PowerShell,装它的程序叫 Windows 终端。按 Win,输入 terminal,回车。老一点的机器就输入 powershell。
你会看到一个以 > 结尾的提示符,像 PS C:\Users\You>。那表示"我在等你"。输入命令,回车,读结果。
开始只需要四个命令:pwd(我在哪)、ls(这里有什么)、cd(去别处)、mkdir(建文件夹)。PowerShell 有自己的长名字,但也认这些短的,所以你在这里学的两个系统都能用。
pwdGet-Location.显示当前文件夹。บอกว่าตอนนี้อยู่โฟลเดอร์ไหนlsGet-ChildItem. dir works too.列出这个文件夹里的东西。แสดงว่ามีอะไรอยู่ในโฟลเดอร์นี้cdcd .. goes up one. cd ~ goes to your home folder.cd .. 上一层,cd ~ 回主目录。cd .. ขึ้นหนึ่งชั้นmkdirmkdir Code creates a folder called Code.新建文件夹。สร้างโฟลเดอร์ใหม่New-Item notes.txttouch. This is the equivalent.Windows 没有 touch,用这个代替。Windows ไม่มี touch ใช้อันนี้แทนcat notes.txtGet-Content.把文件内容打印出来。แสดงเนื้อหาไฟล์บนหน้าจอSelect-Stringgrep. Select-String "Chiang" notes.txt does the same job.Windows 没有 grep,用这个。Windows ไม่มี grep ใช้อันนี้6️⃣ Practise the commands · ฝึกใช้คำสั่ง · 练习这些命令
🇬🇧 English
Below is a real working terminal, inside this page. Nothing you type can break anything. Type help to see every command it knows.
Almost everything here works on both systems. Windows PowerShell understands pwd, ls, cd, mkdir, cat and rm — they are nicknames for its own longer commands.
The practice terminal below draws a Mac-style prompt (student@mac:~$). On Windows yours will look like PS C:\Users\You> instead — the commands you type are the same.
Two do not: touch and grep. On Windows use New-Item notes.txt and Select-String instead. The simulator accepts the Mac names so you can practise the idea either way.
🇹🇭 ไทย
ข้างล่างนี้คือเทอร์มินัลที่ใช้งานได้จริง อยู่ในหน้านี้เลย พิมพ์อะไรก็ไม่พัง พิมพ์ help เพื่อดูคำสั่งทั้งหมดที่มันรู้จัก
เกือบทุกอย่างที่นี่ใช้ได้ทั้งสองระบบ PowerShell ของ Windows เข้าใจ pwd, ls, cd, mkdir, cat และ rm เพราะเป็นชื่อเล่นของคำสั่งยาว ๆ ของมันเอง
มีสองคำสั่งที่ใช้ไม่ได้คือ touch และ grep บน Windows ให้ใช้ New-Item notes.txt และ Select-String แทน เครื่องจำลองนี้รับชื่อแบบ Mac เพื่อให้คุณฝึกแนวคิดได้ทั้งสองทาง
🇨🇳 中文
下面是一个真能用的终端,就在本页里。你怎么打都不会弄坏东西。输入 help 看它认识的所有命令。
这里几乎所有东西在两个系统上都能用。Windows 的 PowerShell 认得 pwd、ls、cd、mkdir、cat 和 rm —— 它们是自家长命令的小名。
有两个不行:touch 和 grep。在 Windows 上要用 New-Item notes.txt 和 Select-String。模拟器接受 Mac 的写法,好让你两边的思路都能练。
cd Documents→cd Code→ls→python3python hello.py
A first look at regular expressions · รู้จัก regular expression เบื้องต้น · 初识正则表达式
🇬🇧 English
grep Chiang notes.txt looks for the exact word. But the text you give grep is not a plain word — it is a pattern, and patterns can describe a whole family of words at once.
These patterns are called regular expressions, or regex. A few characters have special meanings. Learn these eight and you can read most of the regexes you will meet.
The same patterns work in Python, in your editor's Find box, and in almost every programming language. Learning them once pays you back everywhere.
🇹🇭 ไทย
grep Chiang notes.txt ค้นหาคำนั้นตรง ๆ แต่ข้อความที่คุณส่งให้ grep ไม่ใช่แค่คำธรรมดา มันคือ แพตเทิร์น และแพตเทิร์นหนึ่งอันสามารถอธิบายคำทั้งกลุ่มได้พร้อมกัน
แพตเทิร์นเหล่านี้เรียกว่า regular expression หรือ regex อักขระบางตัวมีความหมายพิเศษ จำแปดตัวนี้ให้ได้ แล้วคุณจะอ่าน regex ส่วนใหญ่ที่เจอได้
แพตเทิร์นแบบเดียวกันนี้ใช้ได้ใน Python ในช่องค้นหาของโปรแกรมแก้ไข และในเกือบทุกภาษาโปรแกรม เรียนครั้งเดียวใช้ได้ทุกที่
🇨🇳 中文
grep Chiang notes.txt 是在找那个确切的词。但你交给 grep 的那段文字并不是普通单词,它是一个模式,一个模式可以同时描述一整类词。
这种模式叫做正则表达式(regular expression,简称 regex)。有几个字符带有特殊含义。记住下面这八个,你就能读懂大部分会遇到的正则。
同样的模式在 Python 里、在编辑器的查找框里、在几乎所有编程语言里都能用。学一次,处处受益。
| Pattern | Means | Example | Matches |
|---|---|---|---|
. | any one character | h.t | hat, hot, hit |
* | zero or more of the thing before it | ab*c | ac, abc, abbbc |
+ | one or more | ab+c | abc, abbc — but not ac |
? | optional — zero or one | colou?r | color, colour |
^ | start of the line | ^Chiang | lines that begin with Chiang |
$ | end of the line | 1989$ | lines that end with 1989 |
[abc] | any one of these characters | gr[ae]y | gray, grey |
\d | any digit | \d\d\d\d | 1989, 2026 |
Try these in the terminal above — notes.txt is already there:
ลองพิมพ์ในเทอร์มินัลด้านบนได้เลย ไฟล์ notes.txt มีอยู่แล้ว
在上面的终端里试试 —— notes.txt 已经在那儿了:
The same idea inside Python is the re module. You will not need it for a while, but the patterns are identical:
แนวคิดเดียวกันใน Python คือโมดูล re คุณยังไม่ต้องใช้ในตอนนี้ แต่แพตเทิร์นเหมือนกันทุกอย่าง
在 Python 内部,同样的想法就是 re 模块。你暂时还用不上,但模式完全一样:
import re
text = "Python was started in 1989"
print(re.search(r"\d\d\d\d", text).group()) # 1989
print(re.findall(r"[A-Z]\w+", text)) # ['Python']
print(bool(re.match(r"^Python", text))) # True
7️⃣ Install Python with Homebrew · ติดตั้ง Python ด้วย Homebrew · 用 Homebrew 安装 Python
🇬🇧 English
Macs ship with an old Python that Apple uses for its own purposes. Do not use it. Install your own.
Homebrew is a package manager: one command installs a program, its dependencies and all. It is free and made by volunteers. Install it once, then use brew forever.
Paste the first line into the Terminal. It will ask for your Mac password — typing shows nothing, that is normal. Then install Python and check it worked.
Always type python3python, not python. On a Mac the shorter name may point at Apple's old version.
🇹🇭 ไทย
เครื่อง Mac มี Python เวอร์ชันเก่าติดมาอยู่แล้ว ซึ่ง Apple ใช้เอง อย่าไปใช้ตัวนั้น ให้ติดตั้งของตัวเอง
Homebrew คือ ตัวจัดการแพ็กเกจ คำสั่งเดียวติดตั้งโปรแกรมพร้อมทุกอย่างที่มันต้องใช้ ฟรีและสร้างโดยอาสาสมัคร ติดตั้งครั้งเดียว แล้วใช้คำสั่ง brew ได้ตลอด
คัดลอกบรรทัดแรกไปวางใน Terminal มันจะถามรหัสผ่านเครื่อง ตอนพิมพ์จะไม่มีอะไรขึ้นเลย ซึ่งเป็นเรื่องปกติ จากนั้นติดตั้ง Python แล้วตรวจสอบ
พิมพ์ python3python เสมอ อย่าพิมพ์แค่ python เพราะบน Mac ชื่อสั้นอาจชี้ไปที่เวอร์ชันเก่าของ Apple
🇨🇳 中文
Mac 自带一个旧版 Python,那是苹果自己用的。不要用它,装你自己的。
Homebrew 是一个包管理器:一条命令就把程序和它依赖的东西全装好。免费,由志愿者维护。装一次,以后一直用 brew。
把第一行粘进终端。它会问你 Mac 密码 —— 输入时屏幕上什么都不显示,这是正常的。然后装 Python 并检查装好了没。
永远输入 python3python,不要只写 python。Mac 上短名字可能指向苹果那个旧版本。
brew install pythonpython3python --versionpip3pip install <name>pip installs Python libraries. brew installs programs. Different jobs.pip 装 Python 库,brew 装程序。brew install --cask visual-studio-code--cask installs a normal Mac app with a window.--cask 装有窗口的普通 Mac 应用。7️⃣ Install Python on Windows · ติดตั้ง Python บน Windows · 在 Windows 上安装 Python
🇬🇧 English
Windows does not come with Python. You install it once.
The quickest way is winget, which is already on Windows 11. Open the terminal and run the first line below. If winget is missing, go to python.org/downloads and use the installer instead.
If you use the installer, tick "Add python.exe to PATH" on the first screen. It is easy to miss, and without it the terminal will not find Python at all.
A Windows oddity: if Python is not installed and you type python, Windows opens the Microsoft Store instead of saying "not found". That is not an error in your typing — it just means Python is not there yet.
On Windows the command is python, not python3. There is also py, which always works even when python does not.
🇹🇭 ไทย
Windows ไม่มี Python ติดมาให้ ต้องติดตั้งเองครั้งเดียว
วิธีที่เร็วที่สุดคือใช้ winget ซึ่งมีอยู่แล้วใน Windows 11 เปิดเทอร์มินัลแล้วรันบรรทัดแรกข้างล่าง ถ้าไม่มี winget ให้ไปที่ python.org/downloads แล้วใช้ตัวติดตั้งแทน
ถ้าใช้ตัวติดตั้ง อย่าลืมติ๊ก "Add python.exe to PATH" ในหน้าจอแรก มองข้ามได้ง่ายมาก และถ้าไม่ติ๊ก เทอร์มินัลจะหา Python ไม่เจอเลย
เรื่องแปลกของ Windows ถ้ายังไม่ได้ติดตั้ง Python แล้วคุณพิมพ์ python Windows จะเปิด Microsoft Store ขึ้นมาแทนที่จะบอกว่า "ไม่พบ" นั่นไม่ใช่คุณพิมพ์ผิด แค่แปลว่ายังไม่มี Python
บน Windows คำสั่งคือ python ไม่ใช่ python3 และยังมี py ซึ่งใช้ได้เสมอแม้ python จะใช้ไม่ได้
🇨🇳 中文
Windows 不自带 Python,要自己装一次。
最快的办法是用 winget,Windows 11 上已经有了。打开终端,运行下面第一行。如果没有 winget,就去 python.org/downloads 用安装程序。
用安装程序的话,第一个界面上一定要勾选 "Add python.exe to PATH"。很容易漏掉,不勾的话终端根本找不到 Python。
Windows 的一个怪脾气:如果还没装 Python 就输入 python,系统会打开 Microsoft Store,而不是说"找不到"。这不是你打错了,只是 Python 还没装。
在 Windows 上命令是 python,不是 python3。另外还有 py,即使 python 不好使它也总能用。
winget install Python.Python.3.12python --versionpypy -3.12 script.py picks a version. Handy when python misbehaves.Windows 的 Python 启动器。pip install <name>py -m pip install <name>.装 Python 库。找不到就用 py -m pip。8️⃣ vim — create and edit a file without leaving the Terminal · vim — สร้างและแก้ไฟล์โดยไม่ต้องออกจาก Terminal · vim —— 不离开终端就能新建和编辑文件
🇬🇧 English
vim is a text editor that lives inside the Terminal. It is already on every Mac. You can create, edit and save a Python file without ever touching the mouse.
Follow this exactly. Do not try to understand it yet:
vim hello.py— vim opens. The~down the left mean "no line here yet".- Press i. The bottom shows
-- INSERT --. - Type
print("Sawadee from Chiang Mai") - Press Esc. The
-- INSERT --disappears. - Type
:wqand press Return.
You are back at the prompt, and the file exists. That is the whole recipe. Everything below just explains it.
🇹🇭 ไทย
vim คือโปรแกรมแก้ไขข้อความที่อยู่ใน Terminal ทุกเครื่อง Mac มีติดมาแล้ว คุณสร้าง แก้ และบันทึกไฟล์ Python ได้โดยไม่ต้องแตะเมาส์เลย
ทำตามนี้เป๊ะ ๆ ยังไม่ต้องพยายามเข้าใจ
vim hello.py— vim เปิดขึ้นมา เครื่องหมาย~ทางซ้ายแปลว่า "บรรทัดนี้ยังไม่มีอะไร"- กด i ข้างล่างจะขึ้นคำว่า
-- INSERT -- - พิมพ์
print("Sawadee from Chiang Mai") - กด Esc คำว่า
-- INSERT --จะหายไป - พิมพ์
:wqแล้วกด Return
คุณกลับมาที่ prompt แล้ว และไฟล์ก็ถูกสร้างเรียบร้อย นี่คือขั้นตอนทั้งหมด ที่เหลือข้างล่างแค่อธิบายเพิ่ม
🇨🇳 中文
vim 是一个住在终端里的文本编辑器。每台 Mac 都自带。你完全不碰鼠标就能新建、编辑、保存 Python 文件。
严格照着做,现在先别想为什么:
vim hello.py—— vim 打开了。左边那一列~表示"这行还没有内容"。- 按 i,底部出现
-- INSERT --。 - 输入
print("Sawadee from Chiang Mai") - 按 Esc,
-- INSERT --消失。 - 输入
:wq,回车。
你回到了提示符,文件也建好了。这就是全部流程,下面只是解释它。
d deletes — it does not type "d".vim 一开始就在这个模式。按键是命令,不是字母。vim เริ่มที่โหมดนี้ ปุ่มต่าง ๆ คือคำสั่ง ไม่ใช่ตัวอักษร:w:q:wq:q!dd / udd deletes the whole line, u undoes.普通模式下:dd 删整行,u 撤销。nano hello.py8️⃣ An editor in the Terminal · โปรแกรมแก้ไขในเทอร์มินัล · 终端里的编辑器
🇬🇧 English
Windows has no vim out of the box. You have two good choices.
The easy one: notepad hello.py opens Notepad from the terminal. Type, press CtrlS, close the window. You are back at the prompt. That is enough to finish this whole lesson.
The one worth learning: install vim with winget install vim.vim. It is the same editor Mac and Linux people use, it runs inside the terminal, and it is on almost every server in the world. The recipe below is identical on every system.
Follow it exactly. Do not try to understand it yet:
vim hello.py— vim opens. The~down the left mean "no line here yet".- Press i. The bottom shows
-- INSERT --. - Type
print("Sawadee from Chiang Mai") - Press Esc. The
-- INSERT --disappears. - Type
:wqand press Enter.
🇹🇭 ไทย
Windows ไม่มี vim ติดมาให้ คุณมีสองทางเลือกที่ดี
ทางที่ง่าย พิมพ์ notepad hello.py เพื่อเปิด Notepad จากเทอร์มินัล พิมพ์เนื้อหา กด CtrlS แล้วปิดหน้าต่าง คุณก็กลับมาที่ prompt แค่นี้ก็พอสำหรับบทเรียนทั้งบทนี้
ทางที่ควรเรียน ติดตั้ง vim ด้วย winget install vim.vim มันคือโปรแกรมเดียวกับที่คน Mac และ Linux ใช้ ทำงานในเทอร์มินัล และมีอยู่ในเซิร์ฟเวอร์เกือบทุกเครื่องในโลก ขั้นตอนข้างล่างเหมือนกันทุกระบบ
ทำตามนี้เป๊ะ ๆ ยังไม่ต้องพยายามเข้าใจ
vim hello.py— vim เปิดขึ้นมา เครื่องหมาย~ทางซ้ายแปลว่า "บรรทัดนี้ยังไม่มีอะไร"- กด i ข้างล่างจะขึ้นคำว่า
-- INSERT -- - พิมพ์
print("Sawadee from Chiang Mai") - กด Esc คำว่า
-- INSERT --จะหายไป - พิมพ์
:wqแล้วกด Enter
🇨🇳 中文
Windows 不自带 vim。你有两个不错的选择。
简单的:输入 notepad hello.py,从终端打开记事本。输入内容,按 CtrlS,关掉窗口,就回到提示符了。做完这一整课,这样就够。
值得学的:用 winget install vim.vim 装上 vim。它就是 Mac 和 Linux 用户用的那个编辑器,跑在终端里,全世界几乎每台服务器上都有。下面的流程在任何系统上都一样。
严格照着做,现在先别想为什么:
vim hello.py—— vim 打开了。左边那列~表示"这行还没有内容"。- 按 i,底部出现
-- INSERT --。 - 输入
print("Sawadee from Chiang Mai") - 按 Esc,
-- INSERT --消失。 - 输入
:wq,回车。
:wq:q!notepad hello.py9️⃣ Run Python from the Terminal — the interactive prompt · รัน Python จาก Terminal — โหมดโต้ตอบ · 在终端里运行 Python —— 交互式提示符
🇬🇧 English
Type python3python on its own and press Return. The prompt changes to >>>. You are now inside Python.
Everything you type is run immediately and the answer printed. This is called the REPL — Read, Evaluate, Print, Loop. It is the best place to test one small idea.
To leave, type exit() and press Return, or press CtrlD. The prompt goes back to $.
Know which prompt you are looking at. $ means the Terminal is waiting for a Terminal command. >>> means Python is waiting for Python. Typing ls at >>> gives an error, and that error confuses more beginners than anything else on this page.
🇹🇭 ไทย
พิมพ์ python3python เดี่ยว ๆ แล้วกด Return prompt จะเปลี่ยนเป็น >>> ตอนนี้คุณอยู่ ข้างใน Python แล้ว
ทุกอย่างที่พิมพ์จะทำงานทันทีและแสดงคำตอบออกมา เรียกว่า REPL ย่อจาก Read, Evaluate, Print, Loop เป็นที่ที่ดีที่สุดสำหรับทดลองไอเดียเล็ก ๆ
ออกด้วยการพิมพ์ exit() แล้วกด Return หรือกด CtrlD prompt จะกลับไปเป็น $
ต้องรู้ว่ากำลังมอง prompt ไหนอยู่ $ แปลว่า Terminal กำลังรอคำสั่งของ Terminal ส่วน >>> แปลว่า Python กำลังรอคำสั่ง Python การพิมพ์ ls ตรง >>> จะขึ้น error และ error นี้ทำให้ผู้เริ่มต้นสับสนมากที่สุด
🇨🇳 中文
单独输入 python3python 然后回车。提示符变成 >>>,你现在在 Python 里面了。
你输入的每一行都会立刻执行并打印结果。这叫 REPL(读取、求值、打印、循环),是试验一个小想法最好的地方。
退出:输入 exit() 回车,或按 CtrlD。提示符变回 $。
要分清你在看哪个提示符。$ 表示终端在等终端命令,>>> 表示 Python 在等 Python 代码。在 >>> 处输入 ls 会报错 —— 这个错误比本页任何东西都更让初学者困惑。
🔟 Run a script from the Terminal · รันสคริปต์จาก Terminal · 在终端里运行脚本
🇬🇧 English
The REPL forgets everything when you leave it. A script is a file, so it lasts. This is how real programs are written.
Three steps, every time:
cdinto the folder holding the file.python3python hello.py- Read whatever comes back.
The most common error by far:
python3: can't open file 'hello.py': [Errno 2] No such file or directory
This almost never means the file is gone. It means you are in the wrong folder. Type ls. If you cannot see hello.py in the list, Python cannot either. cd to where it actually is.
🇹🇭 ไทย
REPL ลืมทุกอย่างเมื่อคุณออกจากมัน แต่ สคริปต์ เป็นไฟล์ จึงอยู่ถาวร โปรแกรมจริง ๆ เขียนแบบนี้
สามขั้นตอน ทุกครั้ง
cdเข้าไปในโฟลเดอร์ที่มีไฟล์นั้นอยู่python3python hello.py- อ่านสิ่งที่มันตอบกลับมา
error ที่เจอบ่อยที่สุด
python3: can't open file 'hello.py': [Errno 2] No such file or directory
แทบไม่เคยแปลว่าไฟล์หายไป แต่แปลว่า คุณอยู่ผิดโฟลเดอร์ ให้พิมพ์ ls ถ้าคุณไม่เห็น hello.py ในรายการ Python ก็ไม่เห็นเหมือนกัน ให้ cd ไปที่ที่ไฟล์อยู่จริง
🇨🇳 中文
REPL 一退出就把一切忘光。脚本是文件,所以能留下来。真正的程序都是这么写的。
每次都是三步:
cd进到文件所在的文件夹。python3python hello.py- 读它返回的内容。
最常见的错误:
python3: can't open file 'hello.py': [Errno 2] No such file or directory
这几乎从来不是说文件没了,而是说你在错误的文件夹里。输入 ls,如果列表里看不到 hello.py,Python 也看不到。cd 到它真正所在的地方。
1️⃣1️⃣ IDLE — Python's own little window · IDLE — หน้าต่างเล็ก ๆ ของ Python เอง · IDLE —— Python 自带的小窗口
🇬🇧 English
IDLE comes free with Python. It is a small editor plus a REPL in one window, with no setup at all. Start it from the Terminal:
Two windows matter. The Shell window is the same >>> prompt you already met. File → New File gives you an editor; write code there, press F5 to save and run it, and the output appears in the Shell.
IDLE is worth thirty minutes because it removes the Terminal from the loop while you are still learning the language itself. It colours your code and points at syntax errors. Move on when it starts to feel small.
🇹🇭 ไทย
IDLE ติดมากับ Python ฟรี ๆ มันคือโปรแกรมแก้ไขเล็ก ๆ รวมกับ REPL ในหน้าต่างเดียว ไม่ต้องตั้งค่าอะไรเลย เปิดจาก Terminal
มีสองหน้าต่างที่สำคัญ หน้าต่าง Shell คือ prompt >>> อันเดิมที่คุณรู้จักแล้ว ส่วน File → New File จะให้หน้าต่างสำหรับเขียนโค้ด กด F5 เพื่อบันทึกและรัน ผลลัพธ์จะไปโผล่ในหน้าต่าง Shell
IDLE คุ้มค่าที่จะลองสักครึ่งชั่วโมง เพราะช่วยตัด Terminal ออกไปก่อนในช่วงที่คุณยังเรียนตัวภาษาอยู่ มันใส่สีให้โค้ดและชี้จุดที่พิมพ์ผิด พอเริ่มรู้สึกว่ามันเล็กไปค่อยเปลี่ยนไปใช้อย่างอื่น
🇨🇳 中文
IDLE 是 Python 自带的,免费。它把一个小编辑器和一个 REPL 放在同一个窗口里,完全不用配置。从终端启动它:
有两个窗口要注意。Shell 窗口就是你已经见过的 >>> 提示符。File → New File 会给你一个编辑器,在里面写代码,按 F5 保存并运行,输出会出现在 Shell 窗口里。
IDLE 值得花半小时,因为它在你还在学语言本身的时候把终端从流程里拿掉了。它会给代码上色,并指出语法错误。等你觉得它太小了,就换别的。
1️⃣2️⃣ Jupyter notebooks · Jupyter notebook · Jupyter notebook
🇬🇧 English
A notebook is a document that mixes writing and code. The code sits in cells you can run one at a time, and the answer appears directly under each cell. It is the best way to learn a language, because you can change one line and see what breaks.
Try it before you install anything. Go to jupyter.org/try and pick JupyterLab. It runs in your browser, on their computer, with nothing to set up. Press ShiftReturn on a cell and watch it run. Your work is not saved there, so it is for playing, not for the lessons.
When you are ready to keep your work, install it once, then start it from the folder holding your notebooks. Your browser opens by itself.
The three things to know: ShiftReturn runs a cell and moves on. Cells share memory, so a variable made in cell 3 still exists in cell 9. Run cells in order — running them out of order is the cause of almost every strange result.
Stop the server with CtrlC twice in the Terminal window that started it.
🇹🇭 ไทย
notebook คือเอกสารที่ผสมคำอธิบายกับโค้ดเข้าด้วยกัน โค้ดอยู่ใน เซลล์ ที่รันทีละอันได้ และคำตอบจะโผล่ใต้เซลล์นั้นเลย เป็นวิธีเรียนภาษาที่ดีที่สุด เพราะคุณแก้ทีละบรรทัดแล้วดูว่าอะไรพัง
ลองเล่นก่อนติดตั้งได้เลย เข้าไปที่ jupyter.org/try แล้วเลือก JupyterLab มันทำงานในเบราว์เซอร์บนเครื่องของเขา ไม่ต้องติดตั้งอะไรเลย กด ShiftReturn ที่เซลล์แล้วดูว่ามันรัน แต่งานของคุณจะไม่ถูกบันทึกไว้ที่นั่น จึงเหมาะกับการลองเล่น ไม่เหมาะกับการเรียนจริง
เมื่อพร้อมจะเก็บงานไว้จริง ๆ ให้ติดตั้งครั้งเดียว แล้วสั่งเปิดจากโฟลเดอร์ที่เก็บ notebook ของคุณ เบราว์เซอร์จะเปิดขึ้นมาเอง
สามเรื่องที่ต้องรู้ ShiftReturn รันเซลล์แล้วเลื่อนไปอันถัดไป เซลล์ทุกอันใช้หน่วยความจำร่วมกัน ตัวแปรที่สร้างในเซลล์ 3 จึงยังอยู่ในเซลล์ 9 และต้องรันเซลล์ ตามลำดับ การรันข้ามไปมาคือสาเหตุของผลลัพธ์แปลก ๆ เกือบทั้งหมด
หยุดเซิร์ฟเวอร์ด้วยการกด CtrlC สองครั้งในหน้าต่าง Terminal ที่เปิดมันขึ้นมา
🇨🇳 中文
notebook 是一种把文字和代码混在一起的文档。代码放在单元格里,可以一格一格地运行,结果直接显示在格子下面。这是学语言最好的方式,因为你可以改一行、看看什么坏了。
先别急着装,可以直接试。打开 jupyter.org/try,选 JupyterLab。它在你的浏览器里、用他们的服务器运行,什么都不用配置。在某个格子上按 ShiftReturn 看它跑起来。但你的东西在那里不会被保存,所以它适合玩,不适合做课程。
等你想把成果留下来时,装一次,然后在放 notebook 的文件夹里启动它。浏览器会自己打开。
要知道的三件事:ShiftReturn 运行当前格并跳到下一格。所有格子共享内存,第 3 格里建的变量在第 9 格还在。要按顺序运行 —— 跳着运行是几乎所有怪结果的根源。
在启动它的那个终端窗口里按两次 CtrlC 停止服务。
Check your notebooks into GitHub · เก็บ notebook ของคุณขึ้น GitHub · 把你的 notebook 提交到 GitHub
🇬🇧 English
A notebook that only exists on your laptop is one spilled drink away from gone. GitHub keeps a copy online, remembers every version, and lets you show your work to a teacher or an employer.
You need a free GitHub account first. Go to github.com/join and sign up yourself — never let a program create an account for you.
Then install git and tell it who you are. You do this once, ever.
Clear your output before you commit. A notebook stores its results — every number, every picture — inside the file. That makes the file huge, makes every change look enormous, and can publish data you did not mean to share. In JupyterLab: Kernel → Restart Kernel and Clear Outputs of All Cells, then save, then commit.
🇹🇭 ไทย
notebook ที่มีอยู่แค่ในเครื่องคุณ ห่างจากการหายไปตลอดกาลแค่น้ำหกใส่แก้วเดียว GitHub เก็บสำเนาไว้ออนไลน์ จำทุกเวอร์ชัน และให้คุณเอาผลงานไปให้ครูหรือนายจ้างดูได้
ก่อนอื่นต้องมีบัญชี GitHub แบบฟรี ไปที่ github.com/join แล้วสมัครด้วยตัวเอง อย่าให้โปรแกรมสมัครบัญชีแทนคุณเด็ดขาด
จากนั้นติดตั้ง git และบอกมันว่าคุณเป็นใคร ทำแค่ครั้งเดียวตลอดไป
ล้างผลลัพธ์ก่อน commit notebook เก็บผลลัพธ์ทุกอย่างไว้ในไฟล์ ทั้งตัวเลขและรูปภาพ ทำให้ไฟล์ใหญ่มาก ทำให้การแก้ไขเล็ก ๆ ดูใหญ่โต และอาจเผยแพร่ข้อมูลที่คุณไม่ได้ตั้งใจแชร์ ใน JupyterLab ให้ไปที่ Kernel → Restart Kernel and Clear Outputs of All Cells แล้วบันทึก แล้วค่อย commit
🇨🇳 中文
只存在你笔记本电脑里的 notebook,离彻底消失只差一杯打翻的饮料。GitHub 在线保存一份副本,记住每一个版本,还能让你把作品拿给老师或雇主看。
先要有一个免费的 GitHub 账号。去 github.com/join 自己注册 —— 千万不要让程序替你注册账号。
然后装上 git,告诉它你是谁。这件事一辈子只做一次。
提交前先清空输出。notebook 会把运行结果 —— 每个数字、每张图 —— 都存在文件里。这会让文件变得很大,让每次小改动看起来都很吓人,还可能把你不想公开的数据发出去。在 JupyterLab 里:Kernel → Restart Kernel and Clear Outputs of All Cells,保存,然后再提交。
That push will ask for a password — and your password will not work · ตอน push มันจะถามรหัสผ่าน และรหัสผ่านของคุณจะใช้ไม่ได้ · push 时会问密码 —— 而你的密码用不了
🇬🇧 English
GitHub stopped accepting account passwords over HTTPS in 2021. Instead you make a personal access token — a long random password, made for one computer and one job, that you can cancel at any time without changing your real password.
Make one like this:
- On github.com, click your profile photo, top right → Settings.
- At the bottom of the left sidebar → Developer settings.
- Under Personal access tokens → Fine-grained tokens → Generate new token.
- Token name: something you will recognise later, like
MacBook — python lessons. - Expiration: 90 days. Do not choose "no expiration".
- Repository access: Only select repositories, then choose your
python-lessonsrepo. - Permissions → Repository permissions → Contents → Read and write. That single permission is what
git pushneeds. - Click Generate token, then copy it immediately. GitHub shows it once and never again.
Now run git push. When it asks for your username, type your GitHub username. When it asks for your password, paste the token. Nothing appears on screen while you paste — that is normal, not a broken keyboard.
🇹🇭 ไทย
GitHub เลิกรับรหัสผ่านบัญชีผ่าน HTTPS ตั้งแต่ปี 2021 แทนที่ด้วยการสร้าง personal access token คือรหัสผ่านยาว ๆ แบบสุ่ม สร้างไว้ใช้กับเครื่องเดียวและงานเดียว และยกเลิกเมื่อไรก็ได้โดยไม่ต้องเปลี่ยนรหัสผ่านจริงของคุณ
สร้างตามนี้
- ที่ github.com คลิกรูปโปรไฟล์มุมขวาบน → Settings
- เลื่อนลงล่างสุดของแถบด้านซ้าย → Developer settings
- ใต้หัวข้อ Personal access tokens → Fine-grained tokens → Generate new token
- Token name: ตั้งชื่อที่คุณจำได้ เช่น
MacBook — python lessons - Expiration: 90 วัน อย่าเลือก "no expiration"
- Repository access: เลือก Only select repositories แล้วเลือก repo
python-lessonsของคุณ - Permissions → Repository permissions → Contents → Read and write สิทธิ์ตัวนี้ตัวเดียวคือสิ่งที่
git pushต้องใช้ - กด Generate token แล้วคัดลอกทันที GitHub แสดงให้ดูครั้งเดียวเท่านั้น
จากนั้นรัน git push เมื่อมันถาม username ให้พิมพ์ชื่อผู้ใช้ GitHub ของคุณ เมื่อถาม password ให้วาง token ลงไป ตอนวางจะไม่มีอะไรขึ้นบนหน้าจอ ซึ่งเป็นเรื่องปกติ ไม่ใช่คีย์บอร์ดเสีย
🇨🇳 中文
GitHub 从 2021 年起就不再接受用账号密码走 HTTPS 了。取而代之的是个人访问令牌(personal access token)—— 一串又长又随机的密码,只给一台电脑、一件事用,随时可以作废,而不用改你真正的密码。
这样创建:
- 在 github.com 点右上角头像 → Settings。
- 左侧栏拉到最下面 → Developer settings。
- Personal access tokens 下面 → Fine-grained tokens → Generate new token。
- Token name:起个以后认得出的名字,比如
MacBook — python lessons。 - Expiration:90 天。不要选 "no expiration"。
- Repository access:选 Only select repositories,然后选你的
python-lessons仓库。 - Permissions → Repository permissions → Contents → Read and write。
git push需要的就是这一项权限。 - 点 Generate token,然后立刻复制。GitHub 只显示这一次,之后再也看不到。
现在运行 git push。它问 username 时输入你的 GitHub 用户名;问 password 时粘贴那个 token。粘贴时屏幕上什么都不显示 —— 这是正常的,不是键盘坏了。
🇬🇧 English
A token is a password. Treat it like one:
- Never type it into a file, a notebook cell, or a screenshot.
- Never email it or paste it into a chat, not even to a teacher.
- If it ever leaks — or you commit it by accident — go back to the same page and click Revoke, then make a new one. Revoking is instant and costs you nothing.
- This is the other reason to clear notebook outputs before committing: an output cell can quietly contain a token you printed while testing.
🇹🇭 ไทย
token คือรหัสผ่าน ให้ปฏิบัติกับมันแบบเดียวกัน
- อย่าพิมพ์มันลงในไฟล์ ในเซลล์ notebook หรือในภาพหน้าจอ
- อย่าส่งทางอีเมลหรือวางในแชต แม้แต่กับครู
- ถ้ามันหลุดออกไป หรือคุณเผลอ commit ขึ้นไป ให้กลับไปที่หน้าเดิมแล้วกด Revoke จากนั้นสร้างอันใหม่ การยกเลิกเกิดขึ้นทันทีและไม่เสียอะไรเลย
- นี่คืออีกเหตุผลที่ต้องล้างผลลัพธ์ของ notebook ก่อน commit เพราะเซลล์ผลลัพธ์อาจมี token ที่คุณเผลอ print ออกมาตอนทดลอง
🇨🇳 中文
token 就是密码。要当成密码来对待:
- 绝不要把它写进文件、notebook 单元格或截图里。
- 绝不要用邮件发送或粘到聊天里 —— 哪怕对方是老师。
- 万一泄露了,或者你不小心提交了上去,回到同一个页面点 Revoke,然后重新建一个。作废是立即生效的,也没有任何代价。
- 这也是提交前要清空 notebook 输出的另一个理由:某个输出单元格里可能悄悄留着你测试时打印出来的 token。
🇬🇧 English
Read the three commands as a sentence: add chooses what to save, commit saves it with a note about why, and push sends it to GitHub. Nothing leaves your computer until you push.
Write commit messages a human can use. "Finish section 5 exercises" tells you something in a year. "update" does not.
Git is a big subject and this is the smallest useful piece of it. When you want the rest — branches, pull requests, undoing things — work through Introduction to Git, then the GitHub Pages lab.
🇹🇭 ไทย
อ่านสามคำสั่งนี้เป็นประโยคเดียว add เลือกว่าจะเก็บอะไร commit บันทึกพร้อมโน้ตว่าทำไม และ push ส่งขึ้น GitHub ไม่มีอะไรออกจากเครื่องคุณจนกว่าคุณจะ push
เขียนข้อความ commit ให้คนอ่านรู้เรื่อง "Finish section 5 exercises" ยังบอกอะไรคุณได้ในอีกหนึ่งปี แต่ "update" ไม่บอกอะไรเลย
Git เป็นเรื่องใหญ่ และนี่คือส่วนที่เล็กที่สุดที่ใช้งานได้จริง เมื่ออยากรู้ที่เหลือ ทั้ง branch, pull request, การย้อนกลับ ให้ไปเรียน Introduction to Git แล้วต่อด้วย GitHub Pages lab
🇨🇳 中文
把这三个命令当成一句话来读:add 选择要保存什么,commit 保存并附上一句说明为什么,push 发送到 GitHub。在你 push 之前,什么都不会离开你的电脑。
提交信息要写给人看。"Finish section 5 exercises" 一年后还能告诉你点什么,"update" 什么都说明不了。
Git 是个大话题,这里只是其中最小的实用部分。想学其余的 —— 分支、pull request、撤销操作 —— 请看 Git 入门,然后是 GitHub Pages 实验课。
1️⃣3️⃣ Lesson 1 — walking through python_basics.ipynb
· บทที่ 1 — เดินผ่าน python_basics.ipynb
· 第一课 —— 走一遍 python_basics.ipynb
⬇ Download python_basics.ipynb
🇬🇧 English
This is the language itself, in ten short sections. Download it, put it in ~/Documents/Code, and open it with jupyter lab.
Every section now ends with an exercise and a worked solution. Do the exercise before you open the solution. Reading a solution feels like learning and is not.
Expect it to take two or three sittings. If a cell errors, read the last line of the error first — it names the problem.
🇹🇭 ไทย
นี่คือตัวภาษาเอง แบ่งเป็นสิบหัวข้อสั้น ๆ ดาวน์โหลดมา วางไว้ใน ~/Documents/Code แล้วเปิดด้วย jupyter lab
ตอนนี้ทุกหัวข้อจบด้วย แบบฝึกหัดพร้อมเฉลย ให้ทำแบบฝึกหัดก่อนเปิดเฉลย การอ่านเฉลยให้ความรู้สึกเหมือนได้เรียนรู้ แต่จริง ๆ ไม่ใช่
น่าจะใช้เวลาสองถึงสามรอบ ถ้าเซลล์ไหน error ให้อ่านบรรทัดสุดท้ายของ error ก่อน มันบอกว่าปัญหาคืออะไร
🇨🇳 中文
这是语言本身,分成十个短小的部分。下载下来,放进 ~/Documents/Code,用 jupyter lab 打开。
现在每一节都以一道练习和一份完整解答结束。先做练习,再看解答。看解答感觉像在学,其实不是。
大概要分两三次做完。如果某个格子报错,先读错误的最后一行 —— 它会说明问题是什么。
| # | Section | What it teaches | Its exercise |
|---|---|---|---|
| 1 | Printing and variables | print() puts something on the screen. = stores a value under a name. f-strings put variables inside text. | Print a sentence about your own city. |
| 2 | Data types | int, float, str, bool, None — and type() to check. | Guess five types, then check. |
| 3 | Arithmetic | + - * /, plus // (whole division), % (remainder), ** (power). | Turn 205 minutes into hours and minutes. |
| 4 | Strings | Join, repeat, len(), .upper(), .replace(), and counting from 0. | Slice a phrase four different ways. |
| 5 | Lists | An ordered collection that can grow. append(), remove(), [0], len(). | Build and edit a market list. |
| 6 | Dictionaries | Keys pointing at values. Look up with [key], add by assigning. | Describe a teacher as a dictionary. |
| 7 | if / elif / else | Decisions. Indentation of 4 spaces is part of the language, not decoration. | Turn a temperature into advice. |
| 8 | Loops | for walks a collection, range(n) counts, while repeats on a condition. | The 7 times table, then a countdown. |
| 9 | Functions | def, arguments, return, and default values. | Write a bmi() function. |
| 10 | Mini exercises | Three longer problems pulling it all together, with solutions. | Even numbers, counting scores, word length. |
1️⃣4️⃣ Lesson 2 — walking through python_next_steps.ipynb
· บทที่ 2 — เดินผ่าน python_next_steps.ipynb
· 第二课 —— 走一遍 python_next_steps.ipynb
⬇ Download python_next_steps.ipynb
🇬🇧 English
This is what turns "I can read Python" into "I can write a small useful script". Four topics, each of which you will use in every real program you ever write.
Section 2 writes real files into whatever folder Jupyter started in — so start it from ~/Documents/Code and your files land somewhere you can find them.
Section 3 has one cell that fails on purpose. That is not a mistake in the notebook. Read the red traceback carefully; learning to read errors is the single most useful skill on this page.
🇹🇭 ไทย
บทนี้เปลี่ยนจาก "อ่าน Python ออก" เป็น "เขียนสคริปต์เล็ก ๆ ที่ใช้งานได้จริง" มีสี่หัวข้อ ซึ่งคุณจะได้ใช้ในทุกโปรแกรมจริงที่เขียนต่อจากนี้
หัวข้อ 2 จะเขียนไฟล์จริงลงในโฟลเดอร์ที่คุณเปิด Jupyter ขึ้นมา ดังนั้นให้เปิดจาก ~/Documents/Code ไฟล์จะได้ไปอยู่ในที่ที่คุณหาเจอ
หัวข้อ 3 มีเซลล์หนึ่งที่ ตั้งใจให้พัง ไม่ใช่ความผิดพลาดของ notebook ให้อ่าน traceback สีแดงนั้นให้ดี การอ่าน error เป็นคือทักษะที่มีประโยชน์ที่สุดในหน้านี้
🇨🇳 中文
这一课把"看得懂 Python"变成"能写出有用的小脚本"。四个主题,你以后写的每个真实程序都会用到。
第 2 节会往你启动 Jupyter 的那个文件夹里写真实的文件 —— 所以要从 ~/Documents/Code 启动,文件才会落在你找得到的地方。
第 3 节有一个格子是故意报错的,不是 notebook 写错了。仔细读那段红色的 traceback;学会读错误信息是本页最有用的一项技能。
| # | Section | What it teaches | Its exercise |
|---|---|---|---|
| 1 | Modules | import math, import random — using code other people wrote, and the three import styles. | Roll two dice and take a square root. |
| 2 | File I/O | with open(...), the "r" / "w" / "a" modes, reading line by line. | Write three names, read them back numbered. |
| 3 | Error handling | try / except / finally, reading a traceback, raising your own. | Write a safe_divide() that never crashes. |
| 4 | List comprehensions | [n*2 for n in range(10)], filtering with if, dict and set versions. | Convert Celsius to Fahrenheit three ways. |
| 5 | Magic names | The words with __ on both sides: __init__, __str__, __len__, __name__, __file__. Why you never call them yourself. | Write a Playlist class that works with print(), len() and in. |
| 6 | Mini exercises | Three problems across the earlier topics, with solutions. | Random cities, safe_int(), currency conversion. |
🔗 Objects and the dot · ออบเจกต์กับจุด · 对象和那个点
🇬🇧 English
Say you are writing the space game in the next lesson. Each enemy — each creep — needs a speed, some hit points, and a path. Without classes you get this:
Two creeps, six variables. Twenty creeps, sixty variables. And nothing joins creep1_speed to creep1_hit_points except the name you typed.
A class makes one creep into one thing. Then the dot reaches inside it.
Read the dot as the word of, backwards. creep.speed is the speed of this creep. On the left, which thing. On the right, which fact about it.
🇹🇭 ไทย
สมมติว่าคุณกำลังเขียนเกมอวกาศในบทเรียนถัดไป ศัตรูแต่ละตัว หรือแต่ละ creep ต้องมีความเร็ว พลังชีวิต และเส้นทาง ถ้าไม่ใช้คลาส คุณจะได้แบบนี้
ศัตรูสองตัว ใช้หกตัวแปร ถ้ายี่สิบตัวก็หกสิบตัวแปร และไม่มีอะไรเชื่อม creep1_speed กับ creep1_hit_points เลย นอกจากชื่อที่คุณพิมพ์เอง
คลาส ทำให้ศัตรูหนึ่งตัวเป็นของหนึ่งชิ้น แล้วจุด ก็เอื้อมเข้าไปข้างในได้
ให้อ่านจุดว่าเป็นคำว่า ของ โดยอ่านย้อนกลับ creep.speed คือ ความเร็วของศัตรูตัวนี้ ข้างซ้ายคือ ของชิ้นไหน ข้างขวาคือ ข้อมูลอะไรของมัน
🇨🇳 中文
假设你在写下一课里的太空游戏。每个敌人 —— 每只 creep —— 都要有速度、血量和飞行路线。不用类的话,你会写成这样:
两个敌人,六个变量。二十个敌人,六十个变量。而且除了你自己起的名字,没有任何东西把 creep1_speed 和 creep1_hit_points 联系起来。
类把一个敌人变成一个整体,然后用点伸进去取东西。
把那个点读成「的」,从右往左读。creep.speed 就是这个敌人的速度。点的左边是哪一个东西,右边是它的哪一项。
Why this is convenient · ทำไมถึงสะดวก · 为什么这样很方便
🇬🇧 English
Put all the creeps in one list. Now one loop moves every one of them — and each uses its own speed, because creep.speed always means this creep's speed.
Three lines handle three creeps or three hundred. You never match up index numbers, and you can never accidentally give creep 7 the speed of creep 3.
The dot reaches actions as well as facts. creep.update() is the update of this creep. Facts are called attributes; actions are called methods. Same dot for both.
🇹🇭 ไทย
เอาศัตรูทุกตัวใส่ลิสต์เดียว ทีนี้ลูปเดียวก็ขยับได้ทุกตัว และแต่ละตัวใช้ความเร็วของตัวเอง เพราะ creep.speed หมายถึงความเร็วของศัตรูตัวนี้เสมอ
โค้ดสามบรรทัดจัดการได้ทั้งสามตัวหรือสามร้อยตัว คุณไม่ต้องจับคู่เลขลำดับ และไม่มีทางเผลอเอาความเร็วของตัวที่ 3 ไปให้ตัวที่ 7
จุดยังเอื้อมถึงการกระทำได้ด้วย ไม่ใช่แค่ข้อมูล creep.update() คือ การอัปเดตของศัตรูตัวนี้ ข้อมูลเรียกว่า attribute ส่วนการกระทำเรียกว่า method ใช้จุดเหมือนกันทั้งคู่
🇨🇳 中文
把所有敌人放进一个列表。这样一个循环就能移动全部,而且每一个都用自己的速度 —— 因为 creep.speed 永远指的是这一个敌人的速度。
三行代码,管三个敌人和管三百个一样。你不用去对下标,也绝不会不小心把第 3 个的速度用到第 7 个身上。
点不只能取数据,还能取动作。creep.update() 就是这个敌人的 update。数据叫 属性,动作叫 方法,用的是同一个点。
🇬🇧 English
You have been using the dot all along. In Python almost everything is an object, and the dot is how you reach inside it. You have already written all of these:
🇹🇭 ไทย
จริง ๆ คุณใช้จุดมาตลอดอยู่แล้ว ใน Python เกือบทุกอย่างคือออบเจกต์ และจุดคือวิธีเอื้อมเข้าไปข้างในมัน คุณเขียนสิ่งเหล่านี้มาหมดแล้ว
🇨🇳 中文
其实你一直都在用这个点。在 Python 里几乎所有东西都是对象,点就是伸进去取东西的方式。下面这些你早就写过了:
| You wrote | The thing | What you reached for |
|---|---|---|
"chiang mai".upper() | a string | its upper method |
fruits.append("lime") | a list | its append method |
path.exists() | a Path | its exists method |
r.json() | a requests reply | its json method |
creep.hit_points | your own creep | its hit_points attribute |
Try it now. Start python3python and type this in — the class is only five lines:
ลองเลย เปิด python3python แล้วพิมพ์ตามนี้ คลาสนี้ยาวแค่ห้าบรรทัด
现在就试。启动 python3python,把这段打进去 —— 这个类只有五行:
1️⃣5️⃣ Where to go next · ไปต่อที่ไหน · 接下来去哪
🇬🇧 English
When both notebooks feel comfortable, the next lesson is Python Toolkit. It covers the seven tools you reach for after the basics: classes, generators, decorators, type hints, pathlib, json and requests. It has its own notebook with eight more exercises.
Then build something. Make a Space Game and Map Chiang Mai are both step-by-step projects that use the classes you just met. Grokking Algorithms takes you further, through sixteen classic algorithms with their own notebooks.
When you want to check what stuck, take the Python Exam. It only asks about things taught on this page and in the two notebooks.
After that: install VS Code (brew install --cask visual-studio-codewinget install Microsoft.VisualStudioCode) and write something you actually want. A program you need beats a tutorial you finish.
🇹🇭 ไทย
เมื่อคุ้นกับ notebook ทั้งสองแล้ว บทถัดไปคือ Python Toolkit ครอบคลุมเครื่องมือเจ็ดอย่างที่ใช้ต่อจากพื้นฐาน ได้แก่ class, generator, decorator, type hint, pathlib, json และ requests มี notebook ของตัวเองพร้อมแบบฝึกหัดอีกแปดข้อ
จากนั้นลองสร้างอะไรสักอย่าง สร้างเกมอวกาศ และ ทำแผนที่เชียงใหม่ เป็นโปรเจกต์แบบทีละขั้นตอนที่ใช้คลาสที่คุณเพิ่งเรียนมา ส่วน Grokking Algorithms จะพาไปไกลกว่านั้น ผ่านอัลกอริทึมคลาสสิก 16 แบบ พร้อม notebook ของตัวเอง
อยากรู้ว่าจำอะไรได้บ้าง ลองทำ แบบทดสอบ Python ข้อสอบถามเฉพาะเรื่องที่สอนในหน้านี้และใน notebook ทั้งสองเท่านั้น
หลังจากนั้น ติดตั้ง VS Code (brew install --cask visual-studio-codewinget install Microsoft.VisualStudioCode) แล้วเขียนอะไรที่คุณอยากได้จริง ๆ โปรแกรมที่คุณต้องใช้ดีกว่าบทเรียนที่เรียนจบ
🇨🇳 中文
两个 notebook 都熟了以后,下一课是 Python 工具箱。它讲基础之后最常用的七件工具:类、生成器、装饰器、类型提示、pathlib、json 和 requests。它有自己的 notebook,还有八道练习。
然后去做点东西。做一个太空游戏和画清迈地图都是一步步来的项目,用的正是你刚学的类。Grokking Algorithms 会带你走得更远 —— 十六个经典算法,各自配有 notebook。
想检验掌握了多少,就做 Python 测验。题目只考本页和两个 notebook 里教过的内容。
再往后:装上 VS Code(brew install --cask visual-studio-codewinget install Microsoft.VisualStudioCode),然后写一个你真正想要的东西。一个你需要的程序,胜过一份你读完的教程。
1️⃣6️⃣ Check yourself · ทดสอบตัวเอง · 自我检测
$> prompt. Which command shows the files in the folder you are in?pwdlscdmkdirls = list. pwd tells you where you are; ls tells you what is here.:wq then Return:q! then Return:wq — write and quit. :q! quits and throws your work away.>>>. What happens if you type ls?>>> is Python, not the Terminal>>> means Python is waiting for Python. ls is a Terminal command. Type exit() to get back to $ first.python3python hello.py gives [Errno 2] No such file or directory. Most likely cause?sudols — if you cannot see the file, neither can Python. cd to it.requests?brew install requestswinget install requestspip3pip install requestspython3python requestsvim requestspip installs Python libraries. brew installs whole programs, like Python itself. Different jobs.run