python 第二次預習課補充

set 排重
frozenset={[1,2,3,3,4]}

>>> e=1+1j
>>> type(e)
<class 'complex'>
>>> list=[]
>>> del list
>>> i=[]
>>> type(i)
<class 'list'>
>>> t=(1,2)
>>> type(t)
<class 'tuple'>
>>> d={1:2}
>>> type(d)
<class 'dict'>
>>> s=set([1,2,2,2])
>>> type(s)
<class 'set'>
>>> s
{1, 2}
>>> d1=frozenset([1,2,2,2,3])
>>> type(d1)n
  File "<stdin>", line 1
    type(d1)n
            ^
SyntaxError: invalid syntax
>>> type(d1)
<class 'frozenset'>
>>> d1
frozenset({1, 2, 3})
True
False
type(True) bool

內置類型
dir

運算方法:

>>> a=1
>>> b+1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
>>> b=1
>>> a+b
2
>>> a-b
0
>>> a*b
1
>>> a/b
1.0
>>> a%b
0
>>> a**b
1
>>> import math
>>> math.sqrt(b)
1.0

>>> import math
>>> math.sqrt(b)
1.0
>>> pow(2,3)
8
>>> 2//4
0
>>> 6//2
3
>>> floor(1/2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'floor' is not defined
>>> math.floor(1/2) 地板向下
0
>>> math.ceil(1/2) 地板向下
1
>>> round(0.3) 四捨五入
0
>>> round(0.)
0
>>> round(0.9)
1

‘list’ object is not callable    xx對象不能被調用
保留字被改變
恢復:
del list

>>> list("a,b,c")
['a', ',', 'b', ',', 'c']
>>> list=1
>>> list("abc")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> del list
>>> list("a,b,c")
['a', ',', 'b', ',', 'c']
>>>

字符串
編碼:
ascii 碼

gb2312
gbk
big5

unicode: key 1–>value字符(各種語言:韓文、中文、英文)

utf-8
utf-16
utf-32
保存文件 Unicode的存儲編碼
py3
s=”中國”
type(s) str類型 Unicode
s1=b”avc” –tytes類型 — (gbk,utf-8)

s.encode(“utf-8”)—> bytes類型
s.encode(“utf-8”).decode(“utf-8”)—>unicode 類型

py2
s=”中國”
type(s) str類型 不是Unicode 累死bytes對象
s1=b”avc” –tytes類型 — (gbk,utf-8)

s.decode(“utf-8”)—> Unicode
s.decode(“utf-8”).encode(“utf-8”)—>str 類型

unicode(s=”abc”) –encode–tytes
bytes(s=b”abc”)—decode–unicode

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章