2025年3月6日 星期四

Python 4 小時快速入門

  4 小時快速入門 Python 的教材,適合初學者快速掌握 Python 的基本概念和語法,並能夠撰寫簡單的程式。教材分為 4 個時段,每個時段 1 小時,包含理論講解與練習。


📘 Python 4 小時快速入門

⌛ 時間規劃

時間內容
第 1 小時Python 基礎語法與數據類型
第 2 小時流程控制與函式
第 3 小時資料結構(列表、字典、集合、元組)
第 4 小時檔案操作與簡單專案實作

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

1️⃣ Python 介紹

  • Python 是高階、直譯式、動態語言,適合新手入門。
  • 可用於 Web 開發、數據分析、人工智慧、自動化等。

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

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

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

3️⃣ 基本輸入輸出

name = input("請輸入你的名字:")  # 取得輸入
print("Hello,", name)

4️⃣ 運算符號

x = 10
y = 3

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

📌 第 2 小時:流程控制與函式

5️⃣ 條件判斷 (if-elif-else)

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

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

6️⃣ 迴圈 (for, while)

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

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

7️⃣ 定義函式

def greet(name):
    print("Hello,", name)

greet("Alice")  # 呼叫函式

📌 第 3 小時:資料結構

8️⃣ 列表(List)

fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # 取得第一個元素

fruits.append("orange")  # 新增元素
fruits.remove("banana")  # 移除元素
print(fruits)

9️⃣ 字典(Dictionary)

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

student["age"] = 19  # 更新值
student["gender"] = "male"  # 新增鍵值對
print(student)

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

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

# Tuple (元組,不可修改)
coordinates = (10, 20)
print(coordinates[0])

📌 第 4 小時:檔案操作與簡單專案實作

1️⃣1️⃣ 檔案操作

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

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

1️⃣2️⃣ 簡單專案實作:猜數字遊戲

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("恭喜!你猜對了!")

🎯 4 小時快速入門總結

✅ 基礎語法:變數、運算符、輸入輸出
✅ 流程控制:條件判斷、迴圈、函式
✅ 資料結構:列表、字典、集合、元組
✅ 實作應用:檔案操作、簡單專案

這份教材可以幫助你 快速上手 Python,適合用於 速成學習、課程導入、或自學練習!🚀

沒有留言:

張貼留言