Python - Basic
Print & Input
- 接收用戶輸入
- 使用
input方法1
temp = input('Guess a number')
- 使用
字符串語法
- 原始字符串
r- 在字符串的前面加上一個
r,則表示原始字符串,轉譯字符則不再有效1
print(r"Hello World \n") # Hello World \n
- 在字符串的前面加上一個
- 長字符串
- 專門用於實現跨行字符串
1
2
3
4
5
6x = """
Hello World 1
Hello World 2
Hello World 3
Hello World 4
"""
- 專門用於實現跨行字符串
- 字符串乘法複製
- 將字符串重複指定次數
1
print('HelloWorld ' * 3) # HelloWorld HelloWorld HelloWorld
- 將字符串重複指定次數
random module
- 生成隨機整數:
random.randint(a,b)隨機生成 a - b 的函數1
random.randint(1, 10)
- 獲取偽隨機數攻擊:
- 使用
random.getstate()獲取隨機數種子加工後隨機數生成器內部狀態 - 使用
random.setstate()設置隨機數生成器的內部狀態 - 具體編碼:
1
2
3
4
5
6
7
8
9
10
11x = random.getstate()
random.randint(1, 10) # 2
random.randint(1, 10) # 3
random.randint(1, 10) # 4
random.randint(1, 10) # 10
random.setstate(x)
random.randint(1, 10) # 2
random.randint(1, 10) # 3
random.randint(1, 10) # 4
random.randint(1, 10) # 10
- 使用
數字類型 Number
- 整數型:
- python int類型沒有大小限制
- 浮點型:
- 基於IEEE-745算法,因此會出現誤差
1
print(0.1 + 0.2) # 0.30000000000000004
- 基於IEEE-745算法,因此會出現誤差
- decimal module 精確計算:
- 使用
decimal.Decimal('x')聲明1
2
3a = decimal.Decimal('0.1')
b = decimal.Decimal('0.2')
print(a + b) # 0.3
- 使用
- 科學計數法:
- python會使用科學計算法來儲存小數點數字
1
print(0.00005) # 5e-05
- python會使用科學計算法來儲存小數點數字
- 複數 Complex Number:
- 以浮點型儲存
1
2
3x = 1 + 2j
x.real # 1.0
x.imag # 2.0
- 以浮點型儲存
- Calculation
| Operations | Descriptions |
| :—-: | :—-: |
|x + y| Addition |
|x - y| Subtraction |
|x * y| Multiplication |
|x / y| Division |
|x // y| Quotient |
|x % y| Remainder |
|-x| x reverses |
|+x| x itself |
|abs(x)| Absolute value |
|int(x)| Turn x into an integer |
|float(x)| Turn x into a float |
|complex(re, im)| Return a complex number |
|x.conjugate()| x’s conjugate |
|divmod(x, y)| Return (Quotient, Remainder) |
|pow(x, y)| x ^ y |
|x ** y| x ^ y |
布爾值 Boolean
False的情況:- 定義為
False的對象:None,False - 數值為0的數字類型:
0,0.0,0j,Decimal(0),Fraction(0, 1) - 空的序列和集合:
"",(),[],{},set(),range(0)
- 具體編碼:
1
2
3
4
5bool(123) # True
bool(' ') # True
bool('') # False
bool(0) # False
bool(False) # False
- 定義為
- 邏輯運算符:
and,or,not1
2
33 < 4 and 4 < 5 # True
3 < 4 or 6 < 5 # True
not 3 < 4 # False
- 短路求值 Short-Circuit Evaluation:
- 從左往右,只當第一個操作數的值無法確定邏輯運算結果時,才對第二個操作數進行求值
1
2
3
43 and 4 # 4
3 or 4 # 3
(not 1) or (1 and 0) or (3 and 4) or (5 and 6) or (7 and 8 and 9) # False or 0 or 4 or 6 or 9 == 4
not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9 # False or 0 or 4 or 6 or 9 == 4
- 從左往右,只當第一個操作數的值無法確定邏輯運算結果時,才對第二個操作數進行求值
- 運算符優先級 Precedence of Operators:
| Operator | Meaning |
| :—-: | :—-: |
|()| Parentheses |
|**| Exponent |
|+x,-x,~x| Unary plus, Unary minus, Bitwise NOT |
|*,/,//,%| Multiplication, Division, Floor division, Modulus |
|+,-| Addition, Subtraction |
|<<,>>| Bitwise shift operators |
|&| Bitwise AND |
|^| Bitwise XOR |
|\|| Bitwise OR |
|==,!=,>,>=,<,<=,is,is not,in,not in| Comparisons, Identity, Membership operators |
|not| Logical NOT |
|and| Logical AND |
|or| Logical OR |
條件語句
- 比較運算符:
| Symbol | Meaning |
| :—-: | :—-: |
| < | smaller than |
| <= | smaller than or equal to |
| > | greater than |
| >= | greater than or equal to |
| == | equal to |
| != | not equal |
| is | Objects’ id are equal |
| is not | Objects’ id are not equal |
if-else語句:- 使用縮進調整執行的語句
1
2
3
4
5
6if x < 8:
print('smaller than 8')
elif x == 8:
print('equal to 8')
else:
print('greater than 8')
- 使用縮進調整執行的語句
if-else表達式:- 將
if-else語句縮減成一行的表達式1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# if-else 語句
if 0 <= score < 60:
level = 'D'
elif 60 <= score < 80:
level = 'C'
elif 80 <= score < 90:
level = 'B'
else:
level = 'A'
# if-else 表達式
level = ('D' if 0 <= score < 60 else
'C' if 60 <= score < 80 else
'B' if 80 <= score < 90 else
'D')
- 將
循環結構
while循環
while循環語句:- 語法:
while condition:1
2
3
4count = 10
while count > 0:
print(count)
count -= 1 break語句:跳出此次循環continue語句:跳到下次循環
- 語法:
while-else循環語句:- 當不滿足
while條件時執行else語句1
2
3
4
5
6count = 10
while count > 0:
print(count)
count -= 1
else:
print('count is not greater than 0') - 由於
else是位於while語句中,因此break跳出while循環並不會執行else語句
- 當不滿足
- 循環嵌套 nested loop
while循環中嵌套第二組while循環1
2
3
4
5
6
7
8i = 1
while i <= 9:
j = 1
while j <= i:
print(j, ' * ', i, ' = ', i*j, end='\t') # end 表示末尾字符
j += 1
print()
i += 1break和continue只能作用於一層循環體
for循環
for循環語句:- 語法:
for variable in iterableObject - 可迭代對象 Iterable Object:
String,Array,Object,range()
- 語法:
- 遍歷字符串
String:variable表示字符串中每一個字符1
2
3
4
5
6
7
8
9
10
11str = 'Hello World'
# for實現
for char in str:
print(char, end = ' ') # H e l l o W o r l d
# while實現
i = 0
while i < len(str):
print(str[i])
i += 1
- 遍歷次數:
- 使用
range(start, stop, step)函數返回一個可迭代的對象1
2
3
4
5
6
7
8
9
10
11# 指定結束 stop
for i in range(5):
print(i, end = ' ') # 0 1 2 3 4
# 指定開始於結束 start & stop
for i in range(5, 10):
print(i, end = ' ') # 5 6 7 8 9
# 指定跨度 step
for i in range(5, 10, 2):
print(i, end = ' ') # 5 7 9
- 使用
- 實用效果:
- 遍歷所有質數
1
2
3
4
5
6
7for n in range(2, 100):
for x in range(2, n):
# 如果其中有可被整除則不是質數,跳出循環
if n % x == 0:
break
else:
print(n, end = ' ')
- 遍歷所有質數
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.


