1.Number
(1) 整數:int (2)浮點數:float(無單精度和雙精度之分) 計算注意點: 2/2=1.0type(2/2) 》》》 <class 'float'>1+1=2 1+1.0 = 2.0 如果想要除法得到整數:
type(2//2) 》》》<class 'int'> 1//2 》》》0
單斜杠是除法,自動轉為浮點型資料,雙斜杠表示整除,只會保留整數部分
關于進制,同其他 語言相同: 二進制:0b10 代表十進制的2 八進制:0b10 代表十進制的8 十六進制:0bff 代表十進制的255 進制轉換法: bin(x): 把其他進制數字轉化為二進制 int(x):把其他進制數字轉化為十進制 hex(x):把其他進制數字轉化為十六進制 oct(x):把其他進制數字轉化為八進制 復數(Complex):如36j2.bool 布林值
bool也是Number的一種 與javascript不同的是, bool([])的結果是false, 而javascript中Boolean([])為true 同時:所有的空值都是轉化為false,如0, 空物件, ‘’,[] 另外一個可以轉為false的是None3.單引號與雙引號和三引號
單引號和雙引號表示字串,三引號字串中可以用回車來表示換行"""hello world hello world hello world"""
當然也可以使用三個單引號, 輸出結果是: 'hello world\nhello world\nhello world'
"""hello world\nhello world\nhello world"""以上代碼的輸出結果是:
'hello world\nhello world\nhello world'
也就是說引號并不決議轉義字串,
我們可以通過print去列印,此時轉義字符會被決議 在print時, 為了不讓部分字符轉義,我們可以在字串前加r>>> print(r"c:\northword\northEast") c:\northword\northEast >>> print("c:\northword\northEast") c: orthword orthEast
加入r或者R后,這個字串就不是普通字串了,而是一個原始字串,
4.字串運算
與javascript相同,字串可以通過方括號來獲取第n位的字符":"hello\nworld"[0] == "h"
但是與javascript不同的是,在python中,可以有負序號:
"hello\nworld"[-1] == "d" //True "hello\nworld"[-13] //>>> "hello\nworld"[-13] //>>>Traceback (most recent call last): //>>> File "<pyshell#0>", line 1, in <module> //>>> "hello\nworld"[-13] //>>>IndexError: string index out of range
//超出范圍報錯
而在JavaScript中,
"hello\nworld"[-1] //undefined
字串截取與拼接:
"hello world"[5:7] //' w' "hello world"[0:-2] //'hello wor'
"hello world"[-1:-2] // ''
"hello world"[-3:-2] //'r'
"hello world"[-3:] //'rld'
"hello world"[:-3] //'hello wo'
"sdfd" * 3 //'sdfdsdfdsdfd'
5.串列-- list
串列訪問>>> ["新月打擊", "蒼白之瀑", "月之降臨", "月神沖刺"] //['新月打擊', '蒼白之瀑', '月之降臨', '月神沖刺'] >>> ["新月打擊", "蒼白之瀑", "月之降臨", "月神沖刺"][0] //'新月打擊' >>> ["新月打擊", "蒼白之瀑", "月之降臨", "月神沖刺"][1] //'蒼白之瀑' >>> ["新月打擊", "蒼白之瀑", "月之降臨", "月神沖刺"][:1] //['新月打擊'] >>> ["新月打擊", "蒼白之瀑", "月之降臨", "月神沖刺"][:3] //['新月打擊', '蒼白之瀑', '月之降臨'] >>> ["新月打擊", "蒼白之瀑", "月之降臨", "月神沖刺"][1:] //['蒼白之瀑', '月之降臨', '月神沖刺'] >>> ["新月打擊", "蒼白之瀑", "月之降臨", "月神沖刺"][-1]
//'月神沖刺' >>> ["新月打擊", "蒼白之瀑", "月之降臨", "月神沖刺"][-1, -2] //[] >>> ["新月打擊", "蒼白之瀑", "月之降臨", "月神沖刺"][-2: -1] //['月之降臨']
串列拼接:
>>> ["新月打擊", "蒼白之瀑", "月之降臨", "月神沖刺"] + ['月之降臨'] //['新月打擊', '蒼白之瀑', '月之降臨', '月神沖刺', '月之降臨'] ["新月打擊", "蒼白之瀑", "月之降臨", "月神沖刺"]*2 //['新月打擊', '蒼白之瀑', '月之降臨', '月神沖刺', '新月打擊', '蒼白之瀑', '月之降臨', '月神沖刺']
6.元組:---tuple
(1,2,‘T’, True) 獲取元組的某一項或某幾項都是與陣列一樣的,包括拼接,*(乘法)等type((1,2,3)) //tuple type((1)) //int --- 元組中只有一個元素時,就會獲取到該元素的型別 type((True)) //<class 'bool'> type(([1,2,])) //<class 'list'> type((1,)) //<class 'tuple'> type(()) //<class 'tuple'> type([1]) //<class 'list'>
7.序列
在python中,list和tuple不叫組,而是叫做序列,str也是序列 字串,陣列,元組的截取叫做切片"hello world"[0:8:2] //'hlow' "hello world"[0:8:3] //'hlw'
由以上代碼可以總結, 對于str【x:y:z】從第x+1個起(從序號為x的開始),每隔z-1個獲取一個值,一直到第y個為止,最后講這些獲取的拼接在一起,
判斷在序列中是否存在:>>> "a" in "asfsdaf" True >>> "3" in "345dfdfdf" True >>> 3 in (123, 4, 5) False >>> 3 in (123, 3, 5) True 3 in "233dddd" //報錯,3是int型別 ,不能在字串中尋找 3 in ["3", 4] //False 型別不同
3 not in [1,2,3] //False
獲取序列的長度:
>>> len([1,2,3,4,5,6]) 6 >>> len((1, '3', 'a', True)) 4 >>> len("d1fdf")
5
取最值:
>>> max((1,2,3,50,800,2)) 800 >>> max([1,2,3,50,800,2]) 800 >>> max('hello world') 'w' >>> min("helloworld") 'd' >>>min("helloworld345") '3' >>> max(["3", "cd"]) 'cd' >>> max([3, "cd"]) //Traceback (most recent call last): // File "<pyshell#13>", line 1, in <module> // max([3, "cd"]) //TypeError: '>' not supported between instances of 'str' and 'int' //型別不同不可比較
獲取字符的ASCII 碼
>>> ord("cc") Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> ord("cc") TypeError: ord() expected a character, but string of length 2 found >>> ord("c") 99
8.集合:{}
set 無序的type({1,23,5,6,8,9})
//<class 'set'>
{1,3,5,48,5,9,1,1,5,64,85}
//{64, 1, 3, 5, 9, 48, 85} 重復的會被剔除
集合支持的操作: len(), in
>>> 15 not in {1,3,5,48,5,9,1,1,5,64,85} True >>> 3 in {1,3,5,48,5,9,1,1,5,64,85} True
元素剔除: -:求兩個集合的差集
>>> {1,2,3,4,5,6} - {3, 4}
{1, 2, 5, 6}
>>> {1,2,3,4,5,6} - {3, 4,7,8,9}
{1, 2, 5, 6}
&: 求兩個集合的公共部分(交集)
>>> {1,2,3,4,5,6} & {3, 4}
{3, 4}
>>> {1,2,3,4,5,6} & {3, 4,7,8,9}
{3, 4}
| : 并集
>>> {1,2,3,4,5,6} | {3, 4,7,8,9}
{1, 2, 3, 4, 5, 6, 7, 8, 9}
型別:
type({}) //<class 'dict'> type(set()) //<class 'set'> set({1,3,5,4}) //{1, 3, 4, 5} type({123,132,}) //<class 'set'>
9.字典:dict
type({1:1,2:2})
//<class 'dict'>
查詢:
{"Q": "新月打擊", "W": "蒼白之瀑","E": "月之降臨", "R": "月神沖刺"}["Q"]
//'新月打擊' {"Q": "新月打擊", "Q": "蒼白之瀑","E": "月之降臨", "R": "月神沖刺"}["Q"]
//'蒼白之瀑' ---- 注意如有相同的key,則取最后一個,這是為啥呢?
>>> {"Q": "新月打擊", "Q": "蒼白之瀑","E": "月之降臨", "R": "月神沖刺"}
//{'Q': '蒼白之瀑', 'E': '月之降臨', 'R': '月神沖刺'}
>>> {1: "新月打擊", "1": "蒼白之瀑","E": {1:1}, "R": "月神沖刺"} //{1: '新月打擊', '1': '蒼白之瀑', 'E': {1: 1}, 'R': '月神沖刺'}
不可變型別才能當做key:
>>> {[1,2]: '新月打擊', '1': '蒼白之瀑', 'E': {1: 1}, 'R': '月神沖刺'}
Traceback (most recent call last): File "<pyshell#2>", line 1, in <module>
{[1,2]: '新月打擊', '1': '蒼白之瀑', 'E': {1: 1}, 'R': '月神沖刺'} TypeError: unhashable type: 'list'
>>> {True: '新月打擊', '1': '蒼白之瀑', 'E': {1: 1}, 'R': '月神沖刺'} {True: '新月打擊', '1': '蒼白之瀑', 'E': {1: 1}, 'R': '月神沖刺'}
life is short, i use python
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/225471.html
標籤:Python
