什麼是條件句
條件句 in Python
冒號跟縮排很重要
Python 是少數程式語言中縮排具有意義的語言
如果判斷是不是布林值(True or False),會被強制轉換
笛摩根定理、對偶律
-> wiki <-
「A 且 B」的相反是「非 A 或 非 B」
「A 或 B」的相反是「非 A 且 非 B」
判斷是很複雜時很好用
是 True 還是 False
注意運算子的次序
善用括號,可以大幅增加可讀性
有時候運算子可以接在一起
方便的寫法但有時候會出問題
所以善用括號
不知道要跑幾次
有明確結束條件(沒有也是可以)
明確知道要跑幾次
有可迭代
禁忌:在 for 裡面修改可迭代器
range(start[, stop[, step]])
生成一個型態為 range 類似 list 的迭代器
三個參數都可為負值
基本特性
切片語法
string[start:end:step]
索引跟step也可以是負數
a b c d 0 1 2 3 4 -4 -3 -2 -1 Python 中大多數的範圍都是左閉右開的區間
即包含開頭但不包含結尾
+
可用於連接字串*
可視為+
的展開,用來重複字串replace(old, new[, count])
Return a copy with all occurrences of substring old replaced by new.
find(sub, [start[, end]])
Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end].
回傳第一個找到的索引值,未找到回傳-1
find()
v.s. in
find() 跟 in 都能檢查字串中是否包含查詢的子字串,只有在需要知道子字串位置時才應使用 find()
split(sep[, maxsplit])
Return a list of the words in the string, using sep as the delimiter string.
join(iterable)
Concatenate any number of strings.
strip([chars])
Return a copy of the string with leading and trailing whitespace removed.
If chars is given and not None, remove characters in chars instead.
isalpha()
, isdight()
, islower()
, isupper()
lower()
, upper()
lstrip()
, rstrip()
ljust()
, rjust()
zfill()
把一組年、月、日、時、分、秒串成一個字串
format()
Return a formatted version of S, using substitutions from args and kwargs. The substitutions are identified by braces ('{' and '}').
小數點有點多?
perfect