test
=
to assign variables=
Value (Fix value/Constants, other variables)True
, False
等,在Python有功能的字串0-9
、字母A-Z
a-z
,或是底線組成_
,且不可以數字開頭單引號
或雙引號
標註,或是使用str(文字內容)
宣告str
宣告,即為文字型態(除非有做轉型)包含整數 int與浮點數 float等型態。
整數 int:正數或負數,無小數點
int(數值)
宣告整數數值
非整數,則無條件捨去數值
是文字型態,則強迫轉型包含整數 int與浮點數 float等型態。
浮點數 float:正數或負數,有小數點的數值
float(數值)
宣告整數數值
為整數,則自動增加小數點數值
是文字型態,則強迫轉型True
or False
Python中並無內建的日期型態,必須使用datetime
套件,使用前要記得載入(import)
當需要用載入套件的功能時,結構為:套件名稱
.套件功能或資料類型
(可能有多層)
如datetime
.datetime.now()
:
date
, time
, datetime
等,可查閱文件.now()
為更進一步的功能(取得今日的日期物件)取得日期後,日期物件有許多內建的功能
.year
,.month
等也可直接新增指定日期物件,使用方法為:
datetime.date(年,月,日)
datetime.datetime(年,月,日,時,分,秒)
+
加 / -
減 / *
乘 / /
除>
大於 / <
小於 / ==
等於 / !=
不等於and
且 / or
或Numeric Expressions
You surround a dangerous section of code with try
and except
If the code in the try
works - the except
is skipped
If the code in the try
fails - it jumps to the except
section
Write a program to convert score to the grade and print the grade according to the following criteria:
>90: A
>80 and <=90: B
>=60 and <=80: C
<60: D
Assign 76 to variable score
, check the output
+
/ -
/ *
/ /
>
/ <
/ ==
/ !=
and
/ or
Stored and reused steps!
print('123')
, int('123')
, input()
datetime.date(2025,1,23)
, from datetime
libraryFunction_name
(Argument
)
print
(variable
or “value”)function_name
(Argument 1
,Argument 2
,…)
,
分隔We define a function using the def
reserved word
Rewrite your pay computation with time-and-a-half for overtime and create a function called computepay
which takes two parameters ( hours and rate).
Enter Hours: 45
Enter Rate: 10
Pay: 475.0 (475 = 40 * 10 + 5 * 15)
Function_name
(Argument
)def
function name
(arguments
):
The break
statement ends the current loop and jumps to the statement immediately following the loop
The continue
statement ends the current iteration and jumps to the top of the loop and starts the next iteration
Print the following pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5