Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python เป็น Scripting Language หรือ Programming Language?

Python เป็น Scripting Language

ใช่ Python เป็น Scripting Language

Scripting Language vs Programming Language

ความแตกต่างหลัก: Scripting Language ไม่ต้อง Compile ตรงไปตรงมา Interpret

  • Compiled Languages (C++, Java, Go): Source Code → Compiler → Machine Code → Execute
  • Scripting/Interpreted Languages (Python, JavaScript, Ruby, PHP): Source Code → Interpreter → Execute (Line by Line)

ทำไม Python จึงเป็น Scripting Language?

  • Python ใช้ Interpreter แปลงและรัน Code ทีละบรรทัด
  • ไม่มีขั้นตอน Compilation แยกต่างหาก
  • รันได้ทันที: python script.py

Interpreter vs Compiler

AspectCompilerInterpreter
ProcessScan ทั้งโปรแกรม → Machine Codeแปลงทีละบรรทัด → Execute ทันที
Speedเร็ว (Optimized Binary)ช้ากว่า (Runtime Overhead)
Error DetectionCompile-time (ก่อนรัน)Runtime (ขณะรัน)
PortabilityCompile ใหม่ทุก PlatformRun ได้ทุกที่ที่มี Interpreter
ExamplesC, C++, Rust, GoPython, JavaScript, Ruby, PHP

Python จริงๆ แล้วเป็น Hybrid

Python จริงๆ แล้ว Compile เป็น Bytecode (.pyc files ใน __pycache__) ก่อน แล้วค่อย Interpret Bytecode นี้ ทำให้เร็วกว่า Pure Interpreter

# Python Process
Source (.py) → Compile → Bytecode (.pyc) → Python VM (Interpreter) → Execute

Scripting Language Characteristics

  • Dynamic Typing
  • High-level Abstraction
  • Garbage Collection
  • REPL (Read-Eval-Print Loop) Support
  • ใช้สำหรับ Automation, Glue Code, Rapid Prototyping

Python ใช้ได้ทั้ง Scripting และ General Purpose

Python ไม่ได้เป็นแค่ Scripting Language มันเป็น General-Purpose Programming Language ที่รองรับ Paradigms หลายแบบ:

  • Imperative/Procedural
  • Object-Oriented
  • Functional
  • Scripting/Automation

Use Cases ของ Python Scripting

# 1. File Automation
import os, shutil
for f in os.listdir('.'):
    if f.endswith('.txt'):
        shutil.move(f, 'text_files/')

# 2. Web Scraping
import requests, bs4
resp = requests.get('https://example.com')
soup = bs4.BeautifulSoup(resp.text, 'html.parser')

# 3. System Admin
import subprocess
subprocess.run(['backup.sh'])

# 4. Data Processing
import pandas as pd
df = pd.read_csv('data.csv')
df.groupby('category').sum().to_csv('summary.csv')