2025年3月6日 星期四

Python 8 小時快速入門

  8 小時快速入門 Python 的教材,適合初學者從零開始學習 Python,並能夠掌握基礎語法、資料結構、函式、物件導向,以及簡單的應用,如檔案處理與網路爬蟲。教材分為 8 個時段,每個時段 1 小時,涵蓋理論與實作。


📘 Python 8 小時快速入門

⌛ 時間規劃

時間內容
第 1 小時Python 基礎語法與數據類型
第 2 小時流程控制(條件判斷與迴圈)
第 3 小時函式與模組
第 4 小時資料結構(列表、字典、集合、元組)
第 5 小時物件導向程式設計 (OOP)
第 6 小時檔案處理與錯誤處理
第 7 小時簡單網路爬蟲
第 8 小時簡單專案實作:猜數字遊戲與簡易計算機

📌 第 1 小時:Python 基礎語法與數據類型

1️⃣ 變數與基本數據類型

# 整數、浮點數、字串、布林值
a = 10       # 整數
b = 3.14     # 浮點數
c = "Hello"  # 字串
d = True     # 布林值

print(a, b, c, d)
print(type(a), type(b), type(c), type(d))  # 查看變數類型

2️⃣ 輸入與輸出

name = input("請輸入你的名字:")  
print(f"Hello, {name}!")

3️⃣ 運算符號

x, y = 10, 3

print(x + y)  # 加法
print(x - y)  # 減法
print(x * y)  # 乘法
print(x / y)  # 除法
print(x // y) # 整數除法
print(x % y)  # 餘數
print(x ** y) # 次方

📌 第 2 小時:流程控制

4️⃣ 條件判斷

score = int(input("輸入成績:"))

if score >= 90:
    print("A")
elif score >= 80:
    print("B")
elif score >= 70:
    print("C")
else:
    print("D")

5️⃣ 迴圈

# for 迴圈
for i in range(1, 6):  
    print(f"第 {i} 次")

# while 迴圈
n = 5
while n > 0:
    print(f"倒數: {n}")
    n -= 1

📌 第 3 小時:函式與模組

6️⃣ 定義函式

def greet(name):
    print(f"Hello, {name}!")

greet("Alice")

7️⃣ 引入模組

import math
print(math.sqrt(25))  # 計算平方根

📌 第 4 小時:資料結構

8️⃣ 列表(List)

fruits = ["apple", "banana", "cherry"]
fruits.append("orange")  # 新增元素
fruits.remove("banana")  # 移除元素
print(fruits)

9️⃣ 字典(Dictionary)

student = {"name": "John", "age": 18, "score": 90}
print(student["name"])  # 取得值

student["age"] = 19  # 更新值
print(student)

🔟 集合(Set)與元組(Tuple)

# Set
nums = {1, 2, 3, 4}
nums.add(5)
print(nums)

# Tuple
coordinates = (10, 20)
print(coordinates[0])

📌 第 5 小時:物件導向程式設計 (OOP)

1️⃣1️⃣ 類別與物件

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        print(f"Hi, I'm {self.name}, and I'm {self.age} years old.")

p1 = Person("Alice", 25)
p1.greet()

📌 第 6 小時:檔案處理與錯誤處理

1️⃣2️⃣ 檔案操作

# 寫入檔案
with open("test.txt", "w") as file:
    file.write("Hello, Python!")

# 讀取檔案
with open("test.txt", "r") as file:
    content = file.read()
    print(content)

1️⃣3️⃣ 錯誤處理

try:
    num = int(input("輸入一個數字: "))
    print(10 / num)
except ZeroDivisionError:
    print("不能除以 0!")
except ValueError:
    print("請輸入數字!")

📌 第 7 小時:簡單網路爬蟲

1️⃣4️⃣ 使用 requests 爬取網頁

import requests

url = "https://www.example.com"
response = requests.get(url)
print(response.text)

📌 第 8 小時:簡單專案實作

1️⃣5️⃣ 猜數字遊戲

import random

number = random.randint(1, 10)
guess = 0

while guess != number:
    guess = int(input("猜一個 1-10 的數字: "))
    
    if guess < number:
        print("太小了!")
    elif guess > number:
        print("太大了!")

print("恭喜!你猜對了!")

1️⃣6️⃣ 簡易計算機

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    if y == 0:
        return "不能除以 0"
    return x / y

print("選擇運算:")
print("1. 加法")
print("2. 減法")
print("3. 乘法")
print("4. 除法")

choice = input("請輸入選項 (1/2/3/4): ")
num1 = float(input("輸入第一個數字: "))
num2 = float(input("輸入第二個數字: "))

if choice == '1':
    print(add(num1, num2))
elif choice == '2':
    print(subtract(num1, num2))
elif choice == '3':
    print(multiply(num1, num2))
elif choice == '4':
    print(divide(num1, num2))
else:
    print("無效的輸入")

🎯 8 小時快速入門總結

✅ Python 基礎:變數、條件判斷、迴圈、函式
✅ 資料結構:列表、字典、集合、元組
✅ 進階概念:OOP、檔案處理、錯誤處理
✅ 應用實作:網路爬蟲、猜數字遊戲、計算機

這份教材可幫助你快速學會 Python,適合 自學、短期課程、或程式設計入門 🚀

沒有留言:

張貼留言