ที่เก็บข้อมูล เปรียบเสมือนกล่องที่ใส่ข้อมูลไว้ และสามารถเปลี่ยนค่าได้ ตัวอย่าง:A named container that holds a value. Like a labeled box you can put data into — and the contents can change. Example:一个有名字的"容器",用来存放数据。就像贴了标签的盒子,里面的东西可以随时换。 例子:x = 10
String
สตริง (ข้อความ)
字符串(文本)
ข้อมูลประเภทข้อความ ต้องใช้เครื่องหมายคำพูด " " หรือ ' ' ครอบ ตัวอย่าง:"Hello" หรือ 'สวัสดี'Text data. Must be wrapped in double quotes " " or single quotes ' '. Example:"Hello" or 'Hi there'文本数据,必须用双引号 " " 或单引号 ' ' 包起来。 例子:"Hello" 或 '你好'
Integer
จำนวนเต็ม
整数
ตัวเลขที่ไม่มีทศนิยม ตัวอย่าง:A whole number — no decimal point. Example:没有小数点的整数。 例子:1, 100, -50
Float
จำนวนทศนิยม
浮点数(小数)
ตัวเลขที่มีจุดทศนิยม ตัวอย่าง:A number with a decimal point. Example:带小数点的数字。 例子:3.14, 2.5, -0.75
ชุดข้อมูลที่เก็บเป็นคู่ของ key (กุญแจ) และ value (ค่า) ใช้ปีกกา { } ตัวอย่าง:person = {"name": "สมชาย", "age": 25}A collection of key–value pairs, written inside curly braces { }. Look up a value by its key. Example:person = {"name": "Somchai", "age": 25}由键(key)和值(value)成对组成的集合,写在花括号 { } 里。通过键可以查到对应的值。 例子:person = {"name": "小明", "age": 25}
Function
ฟังก์ชัน
函数
ชุดคำสั่งที่ถูกกำหนดไว้ สามารถเรียกใช้ซ้ำได้ ใช้คำว่า def ตัวอย่าง:A reusable block of code, defined with the def keyword. Call it whenever you need that behaviour. Example:一段可以反复调用的代码块,用 def 关键字定义。需要时再"调用"它即可。 例子:
In Python, the : symbol marks the start of a new block of code. You'll see it after:
在 Python 里,: 用来表示"接下来要开始一个新的代码块"。你会在这些地方看到它:
การสร้างฟังก์ชัน (function)
การตรวจสอบเงื่อนไข (if/else)
การวนลูป (for/while)
การสร้างคลาส (class)
Defining a function
Conditionals — if/else
Loops — for/while
Defining a class
定义函数(function)
条件判断 —— if / else
循环 —— for / while
定义类(class)
# ต้องมี : หลัง ifif age > 18:print("เป็นผู้ใหญ่")
# ต้องมี : หลัง defdefgreet():print("สวัสดี")
# ต้องมี : หลัง forfor i inrange(5):print(i)
# Colon required after ifif age > 18:print("Adult")
# Colon required after defdefgreet():print("Hello")
# Colon required after forfor i inrange(5):print(i)
# if 后面必须有 :if age > 18:print("成年人")
# def 后面必须有 :defgreet():print("你好")
# for 后面必须有 :for i inrange(5):print(i)
Build your own function — enter your name and age:
来写一个属于你自己的函数 —— 输入你的名字和年龄:
ผลลัพธ์จะแสดงที่นี่...Output will appear here...运行结果会显示在这里…
💻ตัวอย่างโค้ดสำหรับฝึกหัดCode Examples for Practice练习用的代码示例
ตัวอย่างที่ 1: สร้างลิสต์และแสดงผลExample 1: Build a list and print it示例 1:创建列表并打印
# สร้างลิสต์ของผลไม้
fruits = ["แอปเปิ้ล", "กล้วย", "ส้ม", "มะม่วง"]
# แสดงผลไม้ทั้งหมดprint("ผลไม้ที่ฉันชอบ:")
for fruit in fruits:
print("- ", fruit)
# เพิ่มผลไม้ใหม่
fruits.append("ทุเรียน")
print("จำนวนผลไม้ทั้งหมด:", len(fruits))
# Create a list of fruits
fruits = ["apple", "banana", "orange", "mango"]
# Show every fruitprint("My favorite fruits:")
for fruit in fruits:
print("- ", fruit)
# Add a new fruit
fruits.append("durian")
print("Total fruits:", len(fruits))
# 创建一个水果列表
fruits = ["苹果", "香蕉", "橙子", "芒果"]
# 把所有水果都打印出来print("我喜欢的水果:")
for fruit in fruits:
print("- ", fruit)
# 加一个新的水果
fruits.append("榴莲")
print("水果总数:", len(fruits))
ตัวอย่างที่ 2: ใช้ Dictionary เก็บข้อมูลExample 2: Store data in a dictionary示例 2:用字典保存数据
# สร้างฟังก์ชันคำนวณพื้นที่สี่เหลี่ยมdefcalculate_area(width, height):
area = width * height
return area
# เรียกใช้ฟังก์ชัน
result = calculate_area(10, 5)
print("พื้นที่:", result, "ตารางหน่วย")
# ฟังก์ชันตรวจสอบเลขคู่หรือคี่defcheck_even_odd(number):
if number % 2 == 0:
return"เลขคู่"else:
return"เลขคี่"print(check_even_odd(7)) # เลขคี่print(check_even_odd(10)) # เลขคู่
# Function: area of a rectangledefcalculate_area(width, height):
area = width * height
return area
# Call the function
result = calculate_area(10, 5)
print("Area:", result, "square units")
# Function: check even or odddefcheck_even_odd(number):
if number % 2 == 0:
return"even"else:
return"odd"print(check_even_odd(7)) # oddprint(check_even_odd(10)) # even
# 函数:计算矩形面积defcalculate_area(width, height):
area = width * height
return area
# 调用函数
result = calculate_area(10, 5)
print("面积:", result, "平方单位")
# 函数:判断奇偶数defcheck_even_odd(number):
if number % 2 == 0:
return"偶数"else:
return"奇数"print(check_even_odd(7)) # 奇数print(check_even_odd(10)) # 偶数
ตัวอย่างที่ 4: ค้นหาไฟล์ในโฟลเดอร์Example 4: Find a file in your folders示例 4:在文件夹里查找文件
A tiny script that starts in the current directory, walks every subfolder, and prints the path of the first file matching the name you pass. Glob patterns like "*.txt" work too. Run it with python find_file.py <filename>.
# ค้นหาไฟล์โดยเริ่มจากโฟลเดอร์ปัจจุบัน เดินลงไปทุกโฟลเดอร์ย่อยimport os
import sys
import fnmatch
from pathlib import Path
# ต้องส่งชื่อไฟล์มา 1 ตัวจาก command lineiflen(sys.argv) != 2:
sys.exit(f"Usage: {Path(sys.argv[0]).name} <filename>")
pattern = sys.argv[1]
# os.walk เดินลงทุกโฟลเดอร์ย่อยให้เองfor dirpath, _, filenames in os.walk(Path.cwd()):
for fn in filenames:
if fnmatch.fnmatch(fn, pattern):
print(Path(dirpath) / fn)
sys.exit(0) # เจอแล้วจบเลย
sys.exit(f"Not found: {pattern}")
# Search for a file starting in the current directory, recursing into subfoldersimport os
import sys
import fnmatch
from pathlib import Path
# Expect exactly one filename on the command lineiflen(sys.argv) != 2:
sys.exit(f"Usage: {Path(sys.argv[0]).name} <filename>")
pattern = sys.argv[1]
# os.walk descends into every subfolder for usfor dirpath, _, filenames in os.walk(Path.cwd()):
for fn in filenames:
if fnmatch.fnmatch(fn, pattern):
print(Path(dirpath) / fn)
sys.exit(0) # Stop at the first match
sys.exit(f"Not found: {pattern}")
# 从当前目录开始查找文件,递归进入每个子文件夹import os
import sys
import fnmatch
from pathlib import Path
# 命令行只接受一个文件名iflen(sys.argv) != 2:
sys.exit(f"Usage: {Path(sys.argv[0]).name} <filename>")
pattern = sys.argv[1]
# os.walk 会自己钻到每一个子文件夹for dirpath, _, filenames in os.walk(Path.cwd()):
for fn in filenames:
if fnmatch.fnmatch(fn, pattern):
print(Path(dirpath) / fn)
sys.exit(0) # 找到第一个就退出
sys.exit(f"Not found: {pattern}")
Read the error message carefully when things break — it tells you exactly where the problem is
Use print() to peek at variable values while debugging
Start small. Get one tiny piece working before adding complexity
Don't forget the colon : and the indentation that follows it
🎓 给初学者的小建议:
自己动手敲一遍代码,别只复制粘贴
报错时认真看错误信息(Error message),它会告诉你哪里出问题了
用 print() 来查看变量当前的值,是最简单的调试手段
从最小的代码片段开始跑通,再一点点加复杂度
别忘了写冒号 : 和之后的缩进
🚀ขั้นต่อไป — ดูโค้ดจริงที่ทำงานได้Next Step — See Real Code in Action下一步 —— 看真实代码的运行
เมื่อรู้พื้นฐานแล้ว ลองดูทุกอย่างทำงานจริงใน agent.py — AI agent ขนาดประมาณ 190 บรรทัด ที่ใช้แนวคิดทั้งหมดในหน้านี้: ตัวแปร, ฟังก์ชัน, dictionary, if/else, for loops
Now that you know the basics, watch them work together in agent.py — a real ~190-line AI agent built with everything on this page: variables, functions, dictionaries, if/else, and for loops.
学完基础之后,来看看它们在 agent.py 里一起工作 —— 一个约 190 行的真实 AI 智能体,用到了这一页讲过的所有概念:变量、函数、字典、if/else、for 循环。