🚀 Advanced AI Tutorials

บทเรียน AI ขั้นสูง

Learn to Use AI for Code Generation & Problem Solving

เรียนรู้ใช้ AI สร้างโค้ดและแก้ปัญหา

🎉 ยินดีด้วย! Congratulations!

คุณพร้อมที่จะใช้ AI แก้ปัญหาจริงแล้ว!

You're ready to use AI for real problem-solving!

ในหน้านี้ คุณจะได้เรียนรู้การใช้ AI สร้างโค้ด แก้ปัญหาในชีวิตจริง และสร้างโปรแกรมที่มีประโยชน์

You'll learn to use AI for code generation, solve real problems, and create useful programs!

💻 บทเรียนที่ 1
Tutorial 1

🎯

AI Code Generation - Basics
การใช้ AI สร้างโค้ดเบื้องต้น 🌱 Easy

สิ่งที่คุณจะได้เรียนรู้:

  • วิธีขอให้ AI เขียนโค้ดอย่างมีประสิทธิภาพ
  • การอธิบายปัญหาให้ AI เข้าใจ
  • การทดสอบและปรับปรุงโค้ดที่ได้
  • ตัวอย่างการสร้างโปรแกรมง่ายๆ

What You'll Learn:

Master the art of asking AI to write code! You'll learn how to describe problems clearly, test generated code, and create simple but useful programs.

1การเขียน Prompt ที่ดี - Writing Good Prompts

Prompt คือคำสั่งหรือคำถามที่คุณให้ AI สิ่งสำคัญคือต้องชัดเจน เฉพาะเจาะจง และระบุรายละเอียดที่จำเป็น

Prompt ที่ดี ควรมี:

  1. อธิบายปัญหาหรือสิ่งที่ต้องการ
  2. ระบุภาษาโปรแกรมที่ต้องการ
  3. บอกข้อกำหนดพิเศษ (ถ้ามี)
  4. ขอให้อธิบายโค้ดด้วย

English: A prompt is the instruction or question you give to AI. The key is to be clear, specific, and include necessary details.

Good prompts should have:

  1. Clear description of the problem or desired outcome
  2. Specific programming language
  3. Any special requirements
  4. Request for code explanation
❌ Bad Prompt (ไม่ดี): "Write code to calculate" ✅ Good Prompt (ดี): "Write a Python function that calculates compound interest. It should take principal amount, annual interest rate, number of times interest is compounded per year, and number of years as inputs. Include comments explaining each step." ✅ Prompt ที่ดี (ภาษาไทย): "เขียนฟังก์ชัน Python คำนวณดอกเบี้ยทบต้น รับ input เป็น: เงินต้น, อัตราดอกเบี้ยต่อปี, จำนวนครั้งที่คิดดอกเบี้ยต่อปี, และจำนวนปี ใส่ comment อธิบายแต่ละขั้นตอนด้วย"

2ตัวอย่างที่ 1: คำนวณดอกเบี้ยทบต้น

ปัญหา: คุณต้องการโปรแกรมคำนวณว่าถ้าฝากเงิน 10,000 บาท ดอกเบี้ย 5% ต่อปี ทบต้นทุกเดือน จะได้เงินเท่าไหร่ใน 5 ปี?

English: Problem - You need a program to calculate: If you deposit 10,000 baht at 5% annual interest, compounded monthly, how much will you have after 5 years?

Prompt ที่ให้ AI:

"เขียนโปรแกรม Python คำนวณดอกเบี้ยทบต้น ใช้สูตร A = P(1 + r/n)^(nt) โดยที่ P=เงินต้น, r=อัตราดอกเบี้ย, n=จำนวนครั้งที่ทบต้นต่อปี, t=จำนวนปี พร้อมอธิบายแต่ละบรรทัด"

English: Prompt for AI - "Write a Python program to calculate compound interest using the formula A = P(1 + r/n)^(nt) where P is principal, r is rate, n is compounds per year, t is time in years. Include explanations."

# AI จะสร้างโค้ดประมาณนี้: # AI will generate code like this: def calculate_compound_interest(principal, rate, compounds_per_year, years): """ Calculate compound interest คำนวณดอกเบี้ยทบต้น Parameters: principal (float): เงินต้น / Initial amount rate (float): อัตราดอกเบี้ยต่อปี (เช่น 0.05 = 5%) / Annual rate compounds_per_year (int): จำนวนครั้งที่ทบต้นต่อปี / Times compounded years (float): จำนวนปี / Number of years Returns: float: จำนวนเงินรวม / Final amount """ # สูตรดอกเบี้ยทบต้น / Compound interest formula # A = P(1 + r/n)^(nt) amount = principal * ((1 + rate / compounds_per_year) ** (compounds_per_year * years)) return amount # ตัวอย่างการใช้งาน / Example usage principal = 10000 # 10,000 บาท rate = 0.05 # 5% ต่อปี compounds = 12 # ทบต้นทุกเดือน (12 ครั้งต่อปี) years = 5 # 5 ปี final_amount = calculate_compound_interest(principal, rate, compounds, years) interest_earned = final_amount - principal print(f"เงินต้น: ฿{principal:,.2f}") print(f"เงินรวมหลัง {years} ปี: ฿{final_amount:,.2f}") print(f"ดอกเบี้ยที่ได้รับ: ฿{interest_earned:,.2f}")
Output (ผลลัพธ์): เงินต้น: ฿10,000.00 เงินรวมหลัง 5 ปี: ฿12,833.59 ดอกเบี้ยที่ได้รับ: ฿2,833.59
💡 Pro Tip: หลังจากได้โค้ดจาก AI แล้ว คุณสามารถถามต่อว่า "อธิบายโค้ดนี้ทีละบรรทัด" หรือ "เพิ่ม error handling" หรือ "สร้าง GUI ให้โค้ดนี้ด้วย"

English: After getting code from AI, you can ask: "Explain this code line by line", "Add error handling", or "Create a GUI for this code"

🔧 บทเรียนที่ 2
Tutorial 2

🐍

Practical Python with AI
Python ใช้งานจริงด้วย AI 🔍 Medium

โปรเจกต์จริงที่ AI ช่วยสร้างได้:

  • จัดการไฟล์อัตโนมัติ
  • ดึงข้อมูลจากเว็บไซต์
  • วิเคราะห์ข้อมูล Excel/CSV
  • สร้างกราฟและรายงาน
  • ส่งอีเมลอัตโนมัติ

Real Projects AI Can Help Build:

AI can help you create practical programs: automated file organization, web scraping, Excel data analysis, graph generation, automated email sending, and much more!

1ตัวอย่างที่ 2: จัดระเบียบไฟล์อัตโนมัติ

ปัญหา: โฟลเดอร์ Downloads ของคุณยุ่งเหยิง มีไฟล์หลายร้อยไฟล์ปนกัน ต้องการโปรแกรมที่จัดเรียงไฟล์ตามประเภท (รูปภาพ, เอกสาร, วิดีโอ, ฯลฯ)

English: Problem - Your Downloads folder is messy with hundreds of files mixed together. You need a program to organize files by type (images, documents, videos, etc.)

Prompt: "เขียนโปรแกรม Python จัดระเบียบไฟล์ในโฟลเดอร์โดยอัตโนมัติ แยกไฟล์ตามนามสกุล (jpg, png → Images, pdf, docx → Documents, mp4, avi → Videos) สร้างโฟลเดอร์ใหม่ถ้ายังไม่มี และย้ายไฟล์ไปยังโฟลเดอร์ที่เหมาะสม"

# โปรแกรมจัดระเบียบไฟล์อัตโนมัติ # Automatic File Organizer import os import shutil from pathlib import Path def organize_files(source_folder): """ จัดระเบียบไฟล์ตามประเภท Organize files by type """ # กำหนดประเภทไฟล์ / Define file categories file_categories = { 'Images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg'], 'Documents': ['.pdf', '.doc', '.docx', '.txt', '.xlsx', '.pptx'], 'Videos': ['.mp4', '.avi', '.mov', '.mkv', '.flv'], 'Audio': ['.mp3', '.wav', '.flac', '.m4a'], 'Archives': ['.zip', '.rar', '.7z', '.tar', '.gz'], 'Code': ['.py', '.js', '.html', '.css', '.java', '.cpp'] } # สร้างโฟลเดอร์ถ้ายังไม่มี / Create folders if they don't exist for category in file_categories.keys(): category_path = Path(source_folder) / category category_path.mkdir(exist_ok=True) # ดึงรายการไฟล์ทั้งหมด / Get all files for file in Path(source_folder).iterdir(): if file.is_file(): # หาว่าไฟล์เป็นประเภทไหน / Determine file category file_ext = file.suffix.lower() moved = False for category, extensions in file_categories.items(): if file_ext in extensions: # ย้ายไฟล์ / Move file destination = Path(source_folder) / category / file.name shutil.move(str(file), str(destination)) print(f"ย้าย {file.name} → {category}/") moved = True break # ไฟล์ที่ไม่รู้จัก ย้ายไป Others / Unknown files go to Others if not moved: others_path = Path(source_folder) / 'Others' others_path.mkdir(exist_ok=True) destination = others_path / file.name shutil.move(str(file), str(destination)) print(f"ย้าย {file.name} → Others/") print("\\n✅ จัดระเบียบไฟล์เสร็จสิ้น! / File organization complete!") # วิธีใช้งาน / Usage if __name__ == "__main__": # เปลี่ยน path เป็นโฟลเดอร์ที่ต้องการจัดระเบียบ # Change to your target folder folder_path = "/Users/YourName/Downloads" organize_files(folder_path)
Output เมื่อรันโปรแกรม: ย้าย vacation.jpg → Images/ ย้าย report.pdf → Documents/ ย้าย movie.mp4 → Videos/ ย้าย song.mp3 → Audio/ ย้าย project.zip → Archives/ ย้าย script.py → Code/ ✅ จัดระเบียบไฟล์เสร็จสิ้น!

2ตัวอย่างที่ 3: วิเคราะห์ข้อมูลยอดขาย

ปัญหา: คุณมีไฟล์ Excel บันทึกยอดขายรายวัน ต้องการโปรแกรมที่อ่านไฟล์ คำนวณยอดขายรวม ยอดขายเฉลี่ย และสร้างกราฟแสดงยอดขายแต่ละเดือน

English: Problem - You have an Excel file with daily sales records. You need a program to read the file, calculate total sales, average sales, and create a graph showing monthly sales.

# โปรแกรมวิเคราะห์ยอดขาย # Sales Analysis Program import pandas as pd import matplotlib.pyplot as plt from datetime import datetime def analyze_sales(excel_file): """ วิเคราะห์ข้อมูลยอดขาย / Analyze sales data """ # อ่านไฟล์ Excel / Read Excel file df = pd.read_excel(excel_file) # แสดงข้อมูล 5 แถวแรก / Show first 5 rows print("📊 ข้อมูลตัวอย่าง / Sample Data:") print(df.head()) print("\\n") # คำนวณสถิติพื้นฐาน / Calculate basic statistics total_sales = df['Sales'].sum() avg_sales = df['Sales'].mean() max_sales = df['Sales'].max() min_sales = df['Sales'].min() print("💰 สรุปยอดขาย / Sales Summary:") print(f"ยอดขายรวม: ฿{total_sales:,.2f}") print(f"ยอดขายเฉลี่ย: ฿{avg_sales:,.2f}") print(f"ยอดขายสูงสุด: ฿{max_sales:,.2f}") print(f"ยอดขายต่ำสุด: ฿{min_sales:,.2f}") print("\\n") # แปลงวันที่เป็น datetime / Convert date to datetime df['Date'] = pd.to_datetime(df['Date']) # สร้างคอลัมน์เดือน / Create month column df['Month'] = df['Date'].dt.to_period('M') # รวมยอดขายแต่ละเดือน / Aggregate monthly sales monthly_sales = df.groupby('Month')['Sales'].sum() # สร้างกราฟ / Create graph plt.figure(figsize=(12, 6)) monthly_sales.plot(kind='bar', color='skyblue', edgecolor='black') plt.title('📈 ยอดขายรายเดือน / Monthly Sales', fontsize=16, weight='bold') plt.xlabel('เดือน / Month', fontsize=12) plt.ylabel('ยอดขาย (บาท) / Sales (THB)', fontsize=12) plt.xticks(rotation=45) plt.grid(axis='y', alpha=0.3) plt.tight_layout() # บันทึกกราฟ / Save graph plt.savefig('sales_chart.png', dpi=300, bbox_inches='tight') print("✅ บันทึกกราฟเป็น 'sales_chart.png'") # แสดงกราฟ / Display graph plt.show() return df, monthly_sales # วิธีใช้งาน / Usage example if __name__ == "__main__": # อ่านไฟล์ sales_data.xlsx df, monthly = analyze_sales('sales_data.xlsx')
💡 Tip: ถ้าคุณไม่มีไฟล์ตัวอย่าง สามารถขอให้ AI สร้างไฟล์ตัวอย่างด้วย! พิมพ์ "สร้างไฟล์ Excel ตัวอย่างข้อมูลยอดขาย 30 วัน มีคอลัมน์ Date และ Sales"

English: If you don't have sample data, ask AI to create it! Type "Create a sample Excel file with 30 days of sales data, with Date and Sales columns"

3ตัวอย่างที่ 4: ตรวจสอบราคาหุ้นอัตโนมัติ

ปัญหา: ต้องการโปรแกรมที่ดึงราคาหุ้นตัวโปรดจากอินเทอร์เน็ต แสดงราคาปัจจุบัน และส่ง alert ถ้าราคาสูงหรือต่ำกว่าที่กำหนด

English: Problem - You need a program that fetches your favorite stock prices from the internet, displays current prices, and sends alerts if prices go above or below set thresholds.

# โปรแกรมตรวจสอบราคาหุ้น # Stock Price Checker import yfinance as yf from datetime import datetime def check_stock_price(symbol, alert_high=None, alert_low=None): """ ตรวจสอบราคาหุ้นและแจ้งเตือน Check stock price and send alerts symbol: รหัสหุ้น เช่น 'AAPL' สำหรับ Apple / Stock symbol alert_high: ราคาสูงสุดที่ต้องการแจ้งเตือน / High price alert alert_low: ราคาต่ำสุดที่ต้องการแจ้งเตือน / Low price alert """ try: # ดึงข้อมูลหุ้น / Fetch stock data stock = yf.Ticker(symbol) info = stock.info current_price = info.get('currentPrice', info.get('regularMarketPrice')) print(f"\\n📊 ข้อมูลหุ้น {symbol}") print(f"ชื่อบริษัท: {info.get('longName', 'N/A')}") print(f"ราคาปัจจุบัน: ${current_price:.2f}") print(f"ราคาเปิด: ${info.get('open', 'N/A')}") print(f"ราคาสูงสุดวันนี้: ${info.get('dayHigh', 'N/A')}") print(f"ราคาต่ำสุดวันนี้: ${info.get('dayLow', 'N/A')}") print(f"ปริมาณการซื้อขาย: {info.get('volume', 'N/A'):,}") # ตรวจสอบ alert / Check alerts if alert_high and current_price >= alert_high: print(f"\\n🚨 แจ้งเตือน! ราคาสูงกว่า ${alert_high}") print(f"ราคาปัจจุบัน: ${current_price:.2f}") if alert_low and current_price <= alert_low: print(f"\\n🚨 แจ้งเตือน! ราคาต่ำกว่า ${alert_low}") print(f"ราคาปัจจุบัน: ${current_price:.2f}") return current_price except Exception as e: print(f"❌ เกิดข้อผิดพลาด: {e}") return None def monitor_multiple_stocks(stocks_dict): """ ตรวจสอบหลายหุ้นพร้อมกัน Monitor multiple stocks stocks_dict: {symbol: (high_alert, low_alert)} """ print(f"\\n{'='*50}") print(f"📈 Stock Monitoring - {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") print(f"{'='*50}") for symbol, (high, low) in stocks_dict.items(): check_stock_price(symbol, high, low) # ตัวอย่างการใช้งาน / Usage example if __name__ == "__main__": # กำหนดหุ้นที่ต้องการติดตาม # Define stocks to monitor my_stocks = { 'AAPL': (180, 150), # Apple: แจ้งถ้า > $180 หรือ < $150 'GOOGL': (145, 130), # Google 'TSLA': (250, 200), # Tesla } monitor_multiple_stocks(my_stocks)
Output: ================================================== 📈 Stock Monitoring - 2026-02-16 14:30:00 ================================================== 📊 ข้อมูลหุ้น AAPL ชื่อบริษัท: Apple Inc. ราคาปัจจุบัน: $175.50 ราคาเปิด: $174.20 ราคาสูงสุดวันนี้: $176.80 ราคาต่ำสุดวันนี้: $173.90 ปริมาณการซื้อขาย: 52,345,678 📊 ข้อมูลหุ้น GOOGL ชื่อบริษัท: Alphabet Inc. ราคาปัจจุบัน: $142.30 ...
⚠️ หมายเหตุ: ต้องติดตั้ง yfinance ก่อน: pip install yfinance และข้อมูลมีความล่าช้า 15-20 นาที สำหรับการลงทุนจริง ควรใช้ API แบบเสียเงินที่มีข้อมูล real-time

English: Note: Install yfinance first: pip install yfinance. Data has 15-20 minute delay. For real investing, use paid APIs with real-time data.

Best Practices - เทคนิคการใช้ AI อย่างมืออาชีพ

💡 Professional Tips for Using AI

  • Always test AI-generated code: อย่าเชื่อโค้ดที่ได้ทันที ทดสอบก่อนใช้งานจริง
  • Ask for explanations: ขอให้ AI อธิบายโค้ดเพื่อให้คุณเข้าใจและเรียนรู้
  • Iterate and improve: ถ้าผลลัพธ์ไม่ใช่ที่ต้องการ บอก AI และขอให้ปรับปรุง
  • Add error handling: ขอให้ AI เพิ่ม error handling เพื่อจัดการข้อผิดพลาด
  • Request comments: ขอให้ใส่ comment อธิบายโค้ดเสมอ
  • Verify calculations: ตรวจสอบผลลัพธ์ทางคณิตศาสตร์ด้วยตัวเอง
  • Check for security: ตรวจสอบว่าโค้ดมีช่องโหว่ด้านความปลอดภัยหรือไม่
  • Learn from the code: อ่านและทำความเข้าใจ อย่าแค่ copy-paste

⚠️ Things to Watch Out For

  • AI can make mistakes: AI ไม่ได้ถูกเสมอ ตรวจสอบผลลัพธ์ทุกครั้ง
  • Outdated libraries: AI อาจแนะนำ library เวอร์ชันเก่า ตรวจสอบ documentation
  • Security concerns: ระวังโค้ดที่อาจมีช่องโหว่ เช่น SQL injection
  • License issues: โค้ดบางส่วนอาจมีลิขสิทธิ์ ตรวจสอบก่อนใช้เชิงพาณิชย์
  • Don't share sensitive data: อย่าส่งข้อมูลส่วนตัวหรือรหัสผ่านให้ AI
  • API limits: ระวังค่าใช้จ่าย API ถ้าใช้บ่อยเกินไป

🏆 คุณเก่งมาก! You're Doing Great!

คุณได้เรียนรู้การใช้ AI สร้างโค้ดและแก้ปัญหาจริงแล้ว!

You've learned to use AI for code generation and real problem-solving!

ตอนนี้คุณพร้อมที่จะทดสอบความรู้

Now you're ready to test your knowledge!

ไปทำ AI Exam เพื่อดูว่าคุณเรียนรู้มาแค่ไหน!