Python tips
Random Python tips I collect recently.
- IMMUTABLE:
String
,Tuple
,Bytes
, Booleans, Numbers - MUTABLE:
List
,Dic
,Set
Tuple()
can use [], but NOadd
orappend
Set {}
, no order, can NOT use [], {1,2}.add(3), NO DUPLICATEString
can use [], but immutable, str[0] = c would fail- [x]*10 will generate 10 refer to the SAME x, change 1 will change ALL
- [[] for _ in range(10)] instead
- print(f’{value=}’) I love it
- list(dict) == list(disk.keys())
- a=b=123, other useage may have side effect
- 10<= a <30 works in Python
- list(range(1,n+1))
- ans.append(list_b) vs ans.append(list_b[:]). Future modification of
list_b
may have side effect