涵蓋 Python 基礎核心概念,以 HR 實際應用場景貫穿,方便日後快速溫習。 Covers core Python fundamentals with HR workplace scenarios throughout, designed for quick revision.
print("你好,我是 Python!Hello, I am Python!")
print("我在學習 AI 技能。I am learning AI skills.")
用 = 把資料「裝入」變數。文字用 " " 包住,數字不需要。
Use = to assign data to a variable. Text needs " " quotes; numbers do not.
employee_name = "James Tong" # 文字(str)Text (str)
department = "人力資源部HR Department"
years = 3 # 整數(int)Integer (int)
salary = 25000.50 # 浮點數(float)Decimal (float)
在引號前加 f,用 { } 包住變數名稱。
Add f before the quote, then wrap variable names in { }.
name = "James Tong"
dept = "人力資源部HR Department"
print(f"員工 Employee {name} 所屬部門: — Department: {dept}")
# 輸出:員工 James Tong 所屬部門:人力資源部Output: Employee James Tong — Department: HR Department
str 文字Text、int 整數Integer、float 小數Decimalprint()f"...{variable}..."name = input("請輸入你的名字:Enter your name: ")
dept = input("請輸入你的部門:Enter your department: ")
print(f"你好,Hello, {name}! 你所在的部門是 Your department is {dept}.")
input() 收到的任何東西都是文字(str),即使你輸入數字 1,Python 收到的也是 "1"。需要做數學運算時,必須先用 int() 或 float() 轉換。
Important: input() always returns a string (str). Even if you type 1, Python receives "1". You must convert it with int() or float() before doing any math.
冒號 : 後面的程式碼必須縮排(Tab 鍵)。
Code after the colon : must be indented (press Tab).
months = float(input("請輸入入職月數:Enter months of service: "))
if months < 3:
print("仍在試用期Still on probation")
else:
print("已過試用期Probation completed")
| 函式Function | 接受Accepts | 例子Example | 不能處理Cannot Handle |
|---|---|---|---|
int() | 整數字串Whole number strings | int("3") → 3 | "0.5" ← 會報錯Error! |
float() | 整數或小數Whole or decimal | float("0.5") → 0.5 | — |
float(),因為員工可能填「0.5 年」或「1.5 年」等小數。
For service years, always use float() — employees may have 0.5 or 1.5 years of service.
Python 從上到下逐一檢查,第一個成立的條件執行後,其餘全部跳過。 Python checks from top to bottom. Once the first true condition is found, all remaining conditions are skipped.
months = float(input("請輸入入職月數:Enter months of service: "))
if months < 3:
print("試用期員工Probation Employee")
elif months < 36:
print("初級員工Junior Employee")
elif months < 60:
print("中級員工Mid-level Employee")
else:
print("資深員工Senior Employee")
| 符號Symbol | 意思Meaning | 注意事項Notes |
|---|---|---|
< | 少於Less than | — |
> | 大於Greater than | — |
== | 等於Equal to | ⚠️ 雙等號!= 是賦值,== 才是比較Double equals! = assigns, == compares |
<= | 少於或等於Less than or equal | — |
>= | 大於或等於Greater than or equal | — |
employees = ["James Tong", "陳大文Chan Tai Man", "李小明Lee Siu Ming"]
# 用 [ ] 包住,項目之間用逗號分隔Wrap in [ ], separate items with commas
for person in employees:
print(f"正在處理:Processing: {person}")
employees = [
("James Tong", 2),
("陳大文Chan Tai Man", 14),
("李小明Lee Siu Ming", 38),
("王美華Wong Mei Wah", 72),
]
for name, months in employees: # 「解包」同時取出兩個值Unpack both values at once
print(f"{name}:{months} 個月months")
for name, months in employees:
if months < 3:
status = "試用期員工Probation Employee"
elif months < 36:
status = "初級員工Junior Employee"
elif months < 60:
status = "中級員工Mid-level Employee"
else:
status = "資深員工Senior Employee"
print(f"{name}:入職 Tenure: {months} 個月,狀態【months — Status: [{status}】]")
| 關鍵字Keyword | 作用Purpose | 比喻Analogy |
|---|---|---|
def | 定義函式(製作工具)Define a function (create the tool) | 製作標準作業流程表Write a standard procedure |
| 參數Parameter | 函式的輸入空格Input slot for the function | 流程表上的空格Blank field in the procedure form |
return | 把結果交回給呼叫者Send the result back to the caller | 填完表格後輸出結論Submit the completed form |
# ❌ 沒有 return — 只印出,結果無法再用No return — prints only, result cannot be reused
def print_status(months):
if months < 3:
print("試用期Probation") # 做完即棄Use once, then gone
# ✅ 有 return — 結果可存起來,繼續使用With return — result can be stored and reused
def check_status(months):
if months < 3:
return "試用期員工Probation Employee"
else:
return "正式員工Confirmed Employee"
status = check_status(2) # 接住結果Catch the result
print(f"狀態:Status: {status}")
def check_status(months):
PROBATION_MONTHS = 3 # 只需在這裡改一次Change only here to update everywhere
if months < PROBATION_MONTHS:
return "試用期員工Probation Employee"
elif months < 36:
return "初級員工Junior Employee"
elif months < 60:
return "中級員工Mid-level Employee"
else:
return "資深員工Senior Employee"
for name, months in employees:
status = check_status(months)
print(f"{name}:{status}")
return。只需要即時顯示一次,可以不用。
When to use return? Use return when you need to store or reuse the result later (save to variable, write to report, embed in f-string). Skip it if you only need to display the result once.
| 概念Concept | 語法Syntax | HR 應用HR Use Case |
|---|---|---|
| 輸出Output | print("...") | 印出員工資料Display employee info |
| 變數Variable | name = "James" | 儲存姓名、月數Store name, months |
| f-stringf-string | f"Hello {name}" | 格式化員工報告Format HR reports |
| 輸入User Input | input("prompt") | 互動式查詢工具Interactive query tool |
| 類型轉換Type Conversion | int() / float() | 處理年資數字Handle numeric tenure |
| 條件判斷Conditionals | if / elif / else | 判斷試用期狀態Determine probation status |
| 迴圈Loop | for x in list: | 批量處理員工名單Batch process staff list |
| 函式Function | def fn(param): return | 可重用的狀態判斷工具Reusable status checker |