12. 哪裡有問題? - Starter
下列程式碼,並預測哪些可以正常執行
• Print(hello)
• Print(This is the start of the lesson”)
• Print(“You need to remember what we did last week”)
• print(“For Example”)
• print(“This is how you use a variable in a sentence”)
• Number1 = 15
• Print(“Your age is” sentence)
13. 變數-python 保留字
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
14. 變數-內建的變數型態Built-in Data type
Object type 說明
Numbers 數字型態 可以是整數 或是 小數 (程式設計領域 稱為 浮點數) 使用時候 不用
加引號
注意~對程設世界來說 整數 與 小數(浮點數) 是不同的
Strings 文字字串 使用時 需要加 雙引號 在字串的前後端
Ex 想顯示 Hello World
就必須寫成 print “Hello World”
Lists 可以想成 是 數學的 一維矩陣,而其中的任一個元素的型態 可以是
Numbers 也可是Strings
每個元素用逗號分隔,用中括號 前後包住 Ex
list1 = ['physics', 'chemistry', 1997, 2000]
其他 目前 授課範圍內 不需要用到 先不提 XD
19. 指派 運算子
運算子 描述 實例
= 簡單的設定運算子 c = a + b 將 a + b 的運算結果賦值為 c
+= 加法設定運算子 c += a 等效於 c = c + a
-= 減法設定運算子 c -= a 等效於 c = c - a
*= 乘法設定運算子 c *= a 等效於 c = c * a
/= 除法設定運算子 c /= a 等效於 c = c / a
%= 取模設定運算子 c %= a 等效於 c = c % a
**= 冪設定運算子 c **= a 等效於 c = c ** a
//= 取整除設定運算子 c //= a 等效於 c = c // a
20. python 內建的數學函數
函数 返回值 ( 描述 )
abs(x) 返回数字的绝对值,如abs(-10) 返回 10
ceil(x) 返回数字的上入整数,如math.ceil(4.1) 返回 5
cmp(x, y) 如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1
exp(x) 返回e的x次幂(e
x
),如math.exp(1) 返回2.718281828459045
fabs(x) 返回数字的绝对值,如math.fabs(-10) 返回10.0
floor(x) 返回数字的下舍整数,如math.floor(4.9)返回 4