RobotAI Academy
หน้าหลัก Tech Builders Python Programming Fundamentals
Module 01 — Tech Builders 🐍 Python อายุ 13–18 ปี

Python Programming
Fundamentals

เรียนรู้ภาษา Python ตั้งแต่ศูนย์ — ตัวแปร เงื่อนไข ลูป ฟังก์ชัน และโครงสร้างข้อมูล ผ่านตัวอย่างโค้ดจริงที่เชื่อมกับโลก AI และ Robotics

⏱️ 4 สัปดาห์ (8 ชั่วโมง)
💻 6 หัวข้อหลัก
🧪 12 Lab Exercise
🏆 Mini Project
hello_robot.py
# 🤖 My First Python Program def greet_robot(name, age): if age < 10: return f"สวัสดี {name}! 🐣" else: return f"Hello {name}! 🤖" sensors = [30, 45, 12, 88] for val in sensors: if val > 50: print(f"⚠️ Alert: {val} cm")
ความคืบหน้า
1
2
3
4
5
6
Q
หัวข้อที่ 01

🐍 Python คืออะไร?

ทำไมนักวิทยาศาสตร์ข้อมูล วิศวกร AI และนักพัฒนา Robotics ถึงเลือก Python เป็นภาษาหลัก

Python คืออะไร?
ภาษาโปรแกรมระดับสูง อ่านง่ายเหมือนภาษาอังกฤษ ฟรี 100% รันได้ทุกระบบ ใช้ใน AI, Data Science, Web, Robotics, Automation
ทำไมต้องเรียน?
อันดับ 1 ภาษายอดนิยมโลก (TIOBE 2024) · ทุก AI Framework รองรับ Python · เรียนรู้ได้เร็ว syntax น้อย
ใช้ในชีวิตจริง
Instagram, YouTube, Netflix, Spotify, NASA, Tesla Autopilot, ChatGPT — ล้วนใช้ Python
ติดตั้งและใช้งาน
Python 3.x · VS Code Editor · Google Colab (ไม่ต้องติดตั้ง) · ทดลองได้ทันทีในเบราว์เซอร์
  • อธิบายได้ว่า Python คืออะไรและใช้ทำอะไรได้บ้าง
  • เขียนและรันโปรแกรม Python แรกได้ด้วยตัวเอง
  • เข้าใจความแตกต่างระหว่าง Python กับ Scratch/Block-based

Python vs Scratch — เปรียบเทียบ

ด้านScratch (Block)Python (Text)
วิธีเขียนลากวางบล็อกพิมพ์โค้ดเป็นข้อความ
ความยืดหยุ่นจำกัดตาม Block ที่มีไม่มีข้อจำกัด
ใช้ในงานจริงเรียนรู้ขั้นต้นIndustry Standard
เชื่อม AI/MLผ่าน ExtensionNative — TensorFlow, PyTorch
ควบคุม RobotLEGO App เท่านั้นทุก Hardware — Raspberry Pi, Arduino

โปรแกรมแรก: Hello, Python! 🎉

Python
# บรรทัดที่ขึ้นต้นด้วย # คือ Comment ไม่ถูกรัน print("สวัสดี โลก! Hello, World!") print("ฉันเขียน Python ได้แล้ว 🐍") # รัน: บันทึกไฟล์เป็น hello.py แล้วพิมพ์ python hello.py
Output
สวัสดี โลก! Hello, World! ฉันเขียน Python ได้แล้ว 🐍
💡

Google Colab — เริ่มเรียนทันทีไม่ต้องติดตั้ง! ไปที่ colab.research.google.com เปิด New Notebook พิมพ์โค้ด แล้วกด Shift+Enter รันได้เลย ฟรี และมีทรัพยากร GPU สำหรับ AI

กิจกรรม
🖥️
Lab 1.1
Setup & Hello World
⏱ 20 นาที💻 เดี่ยว

ติดตั้ง Python และ VS Code จากนั้นเขียนโปรแกรมแนะนำตัวเองอย่างน้อย 5 บรรทัด

  • 1ดาวน์โหลด Python 3.x จาก python.org และ VS Code จาก code.visualstudio.com
  • 2ติดตั้ง Extension "Python" ใน VS Code
  • 3สร้างไฟล์ intro.py เขียน print() แนะนำชื่อ อายุ โรงเรียน และสิ่งที่ชอบ
  • 4รันโปรแกรมและดูผลลัพธ์ใน Terminal
หัวข้อที่ 02

📦 ตัวแปรและประเภทข้อมูล

Variable คือกล่องเก็บข้อมูล Python มี 4 ประเภทหลัก: int, float, str, bool — เชื่อมต่อตรงกับ Sensor และ AI

🔢
int
จำนวนเต็ม
เช่น 10, -5, 0, 255
🌊
float
ทศนิยม
เช่น 3.14, -0.5, 98.6
💬
str
ข้อความ
เช่น "สวัสดี", "Robot"
bool
จริง/เท็จ
True หรือ False เท่านั้น

การประกาศตัวแปร

Python
# ── ประเภทข้อมูลหลัก ────────────────────────── age = 15 # int — จำนวนเต็ม temperature = 36.5 # float — ทศนิยม name = "น้องโค้ด" # str — ข้อความ is_online = True # bool — True/False # ── ตรวจสอบประเภท ────────────────────────────── print(type(age)) # <class 'int'> print(type(name)) # <class 'str'> # ── f-string: ใส่ตัวแปรในข้อความ ───────────── print(f"สวัสดี {name} อายุ {age} ปี") print(f"อุณหภูมิ: {temperature:.1f}°C") # ── การแปลงประเภท (Type Casting) ────────────── sensor_val = "42" # str distance = int(sensor_val) # แปลงเป็น int print(distance + 8) # 50
Output
สวัสดี น้องโค้ด อายุ 15 ปี อุณหภูมิ: 36.5°C 50

ตัวดำเนินการ (Operators)

ประเภทตัวอย่างผลลัพธ์ใช้ในงานจริง
คำนวณ10 + 3, 10 % 3, 2 ** 813, 1, 256คำนวณระยะ, พื้นที่
เปรียบเทียบ10 > 5, x == y, a != bTrue/Falseตรวจ Sensor, เงื่อนไข
ตรรกะx and y, not flag, a or bTrue/Falseหลาย Sensor พร้อมกัน
String"Hi " + name, "abc" * 3"Hi น้อง", "abcabcabc"สร้างข้อความ Output
กิจกรรม
🌡️
Lab 2.1
Robot Sensor Calculator
⏱ 25 นาที💻 เดี่ยว

สร้างโปรแกรมจำลองการอ่านค่า Sensor และแสดงผลสถานะหุ่นยนต์

  • 1ประกาศตัวแปร: distance = 25, battery = 87.5, robot_name = "RoboX"
  • 2คำนวณ: battery_left = battery / 100 * 5000 (mAh ที่เหลือ)
  • 3แสดงผลด้วย f-string รายงานสถานะหุ่นยนต์ทุกค่า
  • 4เพิ่มตัวแปร is_charging = False และแสดงสถานะการชาร์จ
หัวข้อที่ 03

🔀 เงื่อนไขและลูป

if/elif/else ตัดสินใจ, for และ while วนซ้ำ — หัวใจของ Robot Control Loop

if / elif / else — การตัดสินใจ

Python
distance = 12 # ค่าจาก Distance Sensor (ซม.) if distance < 10: print("🛑 STOP! สิ่งกีดขวางใกล้มาก") elif distance < 30: print("⚠️ ชะลอ — สิ่งกีดขวางอยู่ใกล้") elif distance < 100: print("🟡 เดินหน้าระวัง") else: print("🟢 เดินหน้าเต็มสปีด!") # ── Nested condition ────────────────────────── battery = 15 if distance < 30 and battery < 20: print("🚨 แบตน้อย + สิ่งกีดขวาง — กลับฐาน!")
Output
⚠️ ชะลอ — สิ่งกีดขวางอยู่ใกล้ 🚨 แบตน้อย + สิ่งกีดขวาง — กลับฐาน!

for Loop — วนซ้ำตามลำดับ

Python
# ── วนซ้ำ N ครั้ง ───────────────────────────── for i in range(5): # 0, 1, 2, 3, 4 print(f"รอบที่ {i+1}: หุ่นยนต์เดินหน้า") # ── วนซ้ำผ่าน List ──────────────────────────── sensor_readings = [85, 20, 67, 8, 142] for reading in sensor_readings: if reading < 15: print(f"🚨 {reading} cm — หยุดทันที!") else: print(f"✅ {reading} cm — ปลอดภัย") # ── while Loop — วนจนกว่าเงื่อนไขจะเป็นเท็จ ── fuel = 100 while fuel > 0: print(f"เชื้อเพลิงเหลือ {fuel}%") fuel -= 25 print("⛽ หมดเชื้อเพลิง! หยุดทำงาน")
Output (บางส่วน)
รอบที่ 1: หุ่นยนต์เดินหน้า ... ✅ 85 cm — ปลอดภัย 🚨 8 cm — หยุดทันที! เชื้อเพลิงเหลือ 100% ... ⛽ หมดเชื้อเพลิง!
🤖

Robot Control Loop ในชีวิตจริง — หุ่นยนต์ทุกตัวรัน while True: วนซ้ำตลอดเวลา อ่าน Sensor → ตัดสินใจ → ควบคุม Motor ซ้ำๆ นับพันครั้งต่อวินาที นี่คือ Pattern พื้นฐานของ Robotics ทั้งโลก

กิจกรรม
🚦
Lab 3.1
Smart Traffic Light Simulator
⏱ 30 นาที💻 เดี่ยว

สร้างโปรแกรมจำลองสัญญาณไฟจราจรอัจฉริยะ ที่ปรับระยะเวลาตามจำนวนรถ

  • 1สร้าง List car_counts = [5, 23, 8, 47, 2, 31] แต่ละชั่วโมง
  • 2วนซ้ำ for loop: ถ้า > 30 คัน → ไฟเขียวนาน 60 วิ, < 10 คัน → 20 วิ, อื่นๆ → 40 วิ
  • 3หาชั่วโมงที่รถมากที่สุดและน้อยที่สุด
  • 4คำนวณค่าเฉลี่ยจำนวนรถด้วย sum() / len()
หัวข้อที่ 04

🔧 ฟังก์ชัน (Functions)

ฟังก์ชันคือบล็อกโค้ดที่ตั้งชื่อได้ นำกลับมาใช้ใหม่ได้ เป็นหัวใจของ Decomposition และ Clean Code

การสร้างและเรียกใช้ฟังก์ชัน

Python
# ── def: ประกาศฟังก์ชัน ──────────────────────── def greet(name): print(f"สวัสดี {name}! 👋") greet("น้องมีน") # เรียกใช้ greet("น้องโก้") # ── return: คืนค่า ────────────────────────────── def calc_speed(distance_cm, time_sec): """คำนวณความเร็วหุ่นยนต์ cm/s""" if time_sec == 0: return 0 return distance_cm / time_sec speed = calc_speed(150, 3) print(f"ความเร็ว: {speed:.1f} cm/s") # ── Default Parameter ────────────────────────── def move_robot(direction, speed=50, duration=1.0): print(f"🤖 เดิน{direction} speed={speed}% t={duration}s") move_robot("หน้า") # speed=50, duration=1.0 move_robot("ซ้าย", 30, 0.5) # กำหนดเอง # ── Multiple Return Values ───────────────────── def sensor_status(dist, battery): safe = dist > 20 low_bat = battery < 20 return safe, low_bat is_safe, need_charge = sensor_status(35, 15) print(f"ปลอดภัย: {is_safe}, ต้องชาร์จ: {need_charge}")
Output
สวัสดี น้องมีน! 👋 สวัสดี น้องโก้! 👋 ความเร็ว: 50.0 cm/s 🤖 เดินหน้า speed=50% t=1.0s 🤖 เดินซ้าย speed=30% t=0.5s ปลอดภัย: True, ต้องชาร์จ: True
🧩

ฟังก์ชัน = Decomposition จริงๆ! แทนที่จะเขียนโค้ดซ้ำ 100 บรรทัด เราแยกเป็นฟังก์ชันเล็กๆ แต่ละอันทำหน้าที่เดียว ง่ายต่อการ Debug ทดสอบ และนำไปใช้ใหม่ ใน AI Library เช่น TensorFlow ทุกอย่างคือฟังก์ชัน

กิจกรรม
🤖
Lab 4.1
Robot Function Library
⏱ 35 นาที💻 เดี่ยว

สร้าง Library ฟังก์ชันสำหรับหุ่นยนต์ แล้วเขียน Main Program ใช้ฟังก์ชันเหล่านั้น

  • 1สร้างฟังก์ชัน move(direction, speed=50), turn(degrees), stop()
  • 2สร้างฟังก์ชัน read_sensor() ที่ return ค่าสุ่ม 5–200 ด้วย random.randint(5, 200)
  • 3เขียน Main Loop: เรียก read_sensor() แล้วตัดสินใจด้วย if/elif ว่าจะ move() หรือ stop()
  • 4รัน 10 รอบด้วย for loop
หัวข้อที่ 05

📋 List & Dictionary

โครงสร้างข้อมูลที่ใช้บ่อยที่สุดใน Python — จัดเก็บ Sensor Data, Training Data และ Config ของ AI

List — อาร์เรย์ใน Python

Python
# ── สร้างและเข้าถึง List ────────────────────── temps = [36.5, 37.1, 36.8, 38.2, 36.9] print(temps[0]) # 36.5 — index เริ่มจาก 0 print(temps[-1]) # 36.9 — ค่าสุดท้าย # ── Method ที่ใช้บ่อย ───────────────────────── temps.append(37.0) # เพิ่มท้าย temps.insert(0, 36.0) # แทรกตำแหน่งที่ 0 temps.remove(38.2) # ลบค่าที่กำหนด print(len(temps)) # นับจำนวนสมาชิก print(max(temps), min(temps)) # ค่าสูงสุด/ต่ำสุด # ── List Comprehension (Python เท่) ─────────── sensor_data = [10, 25, 5, 88, 42, 3] danger_zones = [x for x in sensor_data if x < 15] print("จุดอันตราย:", danger_zones) # [10, 5, 3]

Dictionary — เก็บข้อมูลแบบ key-value

Python
# ── สร้าง Dictionary ────────────────────────── robot = { "name": "RoboX-3", "battery": 87, "speed": 45.5, "sensors": ["color", "distance", "gyro"], "online": True } # ── เข้าถึงข้อมูล ────────────────────────────── print(robot["name"]) # RoboX-3 print(robot.get("speed")) # 45.5 print(robot.keys()) # ชื่อ key ทั้งหมด print(robot.values()) # ค่าทั้งหมด # ── แก้ไข / เพิ่ม ───────────────────────────── robot["battery"] = 92 robot["location"] = (10, 25) # เพิ่ม key ใหม่ # ── วนซ้ำ Dictionary ────────────────────────── for key, val in robot.items(): print(f" {key}: {val}") # ── AI Training Data (ตัวอย่าง) ─────────────── dataset = [ {"image": "cat1.jpg", "label": "cat", "confidence": 0.95}, {"image": "dog1.jpg", "label": "dog", "confidence": 0.88}, {"image": "cat2.jpg", "label": "cat", "confidence": 0.72}, ] cats = [d for d in dataset if d["label"] == "cat"] print(f"รูปแมวทั้งหมด: {len(cats)} รูป")
Output
RoboX-3 รูปแมวทั้งหมด: 2 รูป
กิจกรรม
🗂️
Lab 5.1
Robot Fleet Manager
⏱ 35 นาที💻 เดี่ยว

สร้างระบบจัดการฝูงหุ่นยนต์ด้วย List of Dictionaries ค้นหา รายงาน และอัปเดตข้อมูล

  • 1สร้าง robots = [{"id":"R1","battery":80,"task":"patrol"},...] 5 ตัว
  • 2เขียนฟังก์ชัน find_low_battery(robots, threshold=20) คืน List หุ่นยนต์แบตน้อย
  • 3เขียนฟังก์ชัน assign_task(robots, robot_id, new_task) อัปเดตงาน
  • 4แสดงรายงานสรุปหุ่นยนต์ทั้งหมดด้วย for loop
หัวข้อที่ 06

🤖 Python + Robotics Mini Project

นำทุกทักษะมารวมกัน สร้างโปรแกรม Python จำลองระบบ Autonomous Robot Navigation

Mini Project: Autonomous Robot Simulator

🎯

เป้าหมาย: เขียนโปรแกรม Python จำลองหุ่นยนต์อัตโนมัติที่ อ่าน Sensor สุ่ม → วิเคราะห์สภาพแวดล้อม → ตัดสินใจ → รายงานสถานะ ครบ 10 รอบ

Python — robot_simulator.py
import random import time # ══════════════════════════════════════════ # Robot Autonomous Navigation Simulator # RobotAI Academy — Tech Builders Program # ══════════════════════════════════════════ class Robot: def __init__(self, name): self.name = name self.battery = 100 self.x = 0 self.y = 0 self.log = [] def read_sensors(self): """จำลองการอ่าน Sensor""" return { "front": random.randint(2, 150), "left": random.randint(5, 100), "right": random.randint(5, 100), "battery": self.battery } def decide_action(self, sensors): """ตัดสินใจจาก Sensor Data""" if sensors["battery"] < 20: return "🔋 RETURN_TO_BASE" elif sensors["front"] < 15: if sensors["left"] > sensors["right"]: return "↩️ TURN_LEFT" else: return "↪️ TURN_RIGHT" elif sensors["front"] < 40: return "⚠️ SLOW_DOWN" else: return "🟢 MOVE_FORWARD" def execute(self, action): """ทำตาม Action ที่ตัดสินใจ""" if "MOVE_FORWARD" in action: self.x += 1 self.battery -= 5 elif "TURN" in action: self.y += 1 self.battery -= 3 elif "SLOW" in action: self.battery -= 2 def run(self, cycles=10): print(f"\n🤖 {self.name} เริ่มทำงาน!") print("=" * 45) for cycle in range(1, cycles + 1): sensors = self.read_sensors() action = self.decide_action(sensors) self.execute(action) self.log.append(action) print(f"[Cycle {cycle:02}] Front={sensors['front']:3}cm | {action}") print("=" * 45) print(f"📍 ตำแหน่งสุดท้าย: ({self.x}, {self.y})") print(f"🔋 แบตเตอรีเหลือ: {self.battery}%") print(f"🏆 เดินหน้าสำเร็จ: {self.log.count('🟢 MOVE_FORWARD')} ครั้ง") # ── รันโปรแกรม ───────────────────────────────── my_robot = Robot("RoboX-Pro") my_robot.run(cycles=10)
Output (ตัวอย่าง)
🤖 RoboX-Pro เริ่มทำงาน! ============================================= [Cycle 01] Front= 87cm | 🟢 MOVE_FORWARD [Cycle 02] Front= 12cm | ↩️ TURN_LEFT [Cycle 03] Front=134cm | 🟢 MOVE_FORWARD [Cycle 04] Front= 28cm | ⚠️ SLOW_DOWN ... ============================================= 📍 ตำแหน่งสุดท้าย: (4, 2) 🔋 แบตเตอรีเหลือ: 62% 🏆 เดินหน้าสำเร็จ: 4 ครั้ง
🚀

ไปไกลกว่านั้น! โค้ดนี้เป็น Pattern เดียวกับ ROS2 (Robot Operating System) จริงๆ ใน Module ถัดไปจะเอา Python นี้ไปควบคุม Hardware จริงผ่าน Raspberry Pi และ Arduino แทนการจำลองด้วย random

🧪 แบบทดสอบ Python Fundamentals

ทดสอบความเข้าใจ ตัวแปร, เงื่อนไข, ลูป, ฟังก์ชัน และ List/Dict · 8 ข้อ

ข้อที่ 01 — ตัวแปร
ผลลัพธ์ของโค้ดนี้คืออะไร?
x = 5    x += 3    print(x * 2)
✅ ถูกต้อง! x=5 → x+=3 ทำให้ x=8 → x*2 = 16
❌ ลองคิดใหม่: x เริ่มที่ 5, บวก 3 = 8, แล้วคูณ 2 = ?
ข้อที่ 02 — ประเภทข้อมูล
type(3.14) คืนค่าอะไร?
✅ ถูกต้อง! ตัวเลขทศนิยมใน Python คือ float
❌ 3.14 มีจุดทศนิยม = float ไม่ใช่ int
ข้อที่ 03 — เงื่อนไข
Distance Sensor อ่านได้ 8 cm โค้ดนี้จะพิมพ์อะไร?
if dist > 50: print("A")
elif dist > 10: print("B")
else: print("C")
✅ ถูกต้อง! 8 ไม่ > 50, ไม่ > 10 → เข้า else พิมพ์ "C"
❌ 8 cm ไม่ผ่านเงื่อนไข if และ elif → ต้องเข้า else
ข้อที่ 04 — ลูป
for i in range(3): print(i) แสดงผลอะไร?
✅ ถูกต้อง! range(3) = [0,1,2] — index ใน Python เริ่มจาก 0
❌ range(3) คือ 0, 1, 2 (ไม่รวม 3) — Python นับจาก 0
ข้อที่ 05 — ฟังก์ชัน
โค้ดนี้จะพิมพ์อะไร?
def add(a, b=10): return a + b
print(add(5))
✅ ถูกต้อง! b มี default value = 10 ดังนั้น add(5) = 5 + 10 = 15
❌ b=10 คือ Default Parameter ถ้าไม่ส่ง b จะใช้ค่า 10 อัตโนมัติ
ข้อที่ 06 — List
data = [10, 20, 30, 40]
print(data[-1])
แสดงผลอะไร?
✅ ถูกต้อง! index -1 ใน Python หมายถึงสมาชิกตัวสุดท้าย = 40
❌ Negative index ใน Python: -1 = ตัวสุดท้าย, -2 = ตัวรองสุดท้าย
ข้อที่ 07 — Dictionary
robot = {"name": "X1", "speed": 50}
print(robot["name"])
✅ ถูกต้อง! robot["name"] เข้าถึง value ของ key "name" = "X1"
❌ Dictionary ใช้ key เพื่อเข้าถึง value — robot["name"] คืนค่า "X1"
ข้อที่ 08 — ประยุกต์ใช้
ใน Robot Control Loop โครงสร้าง Python ที่เหมาะสมที่สุดคืออะไร?
✅ ยอดเยี่ยม! while True: เป็น Infinite Loop ที่ทำงานตลอดเวลา — เหมือนสมองหุ่นยนต์ที่ตื่นอยู่ตลอด
❌ หุ่นยนต์ต้องทำงานต่อเนื่อง while True: คือ Loop ที่วนไม่สิ้นสุด — เหมาะกับ Real-time Control
คะแนนของคุณ
0 / 8
🎓 MODULE 01 — TECH BUILDERS
สรุปบทเรียน Python Fundamentals
ทักษะพื้นฐานที่เป็นรากฐานของทุก AI และ Robotics Project
📦
Variables & Types
int, float, str, bool, f-string, Type Casting
🔀
Conditionals
if / elif / else สำหรับ Sensor Decision
🔁
Loops
for range, while True — Robot Control Pattern
🔧
Functions
def, return, default param, multiple return
📋
List & Dict
เก็บ Sensor Data, Fleet Management, AI Dataset
🤖
Mini Project
Autonomous Robot Simulator ครบ OOP Pattern