Python基本類型,Python類型

來源:互聯網
上載者:User

Python基本類型,Python類型
     Python是一門動態語言,解釋執行,所有錯誤都是運行時產生的,即使有錯誤和異常,只要沒有被執行到也不會有錯,比如調用不存在的方法;類型是隱式的,也即無需變數型別宣告;類型是動態,運行時根據變數指向的內容來決定類型,但是Python是強型別語言,即每個變數都是有類型的。    Python 基本built-in類型主要有numerics,sequences, mapping, files, classes, instances, exceptions,類型上都會存在的操作有比較、是否為真、轉換為字串toString,Python中使用str/repr(object)可轉換為字串, print(object)時會隱式調用str()。
    numerics:      整形 int,用c語言中的long實現, 取值範圍-sys.maxint-1~sys.maxin, 無sys.minint     長整形 long, 帶有L/l的integer或超出integer範圍的,print時會帶尾碼L,無精度限制,無限大,因此Python中都是有符號數,沒有unsigned類型      浮點型 float,用c中的double實現,sys.float_info,  因此Python中無單雙精確度區分      複數 complex, z.real, z.imag

Operation Result Notes
x + y sum of x and y  
x - y difference of x and y  
x * y product of x and y  
x / y quotient of x and y (1)
x // y (floored) quotient of x and y (4)(5)
x % y remainder of x / y (4)
-x x negated  
+x x unchanged  
abs(x) absolute value or magnitude of x (3)
int(x) x converted to integer (2)
long(x) x converted to long integer (2)
float(x) x converted to floating point (6)
complex(re,im) a complex number with real part re, imaginary part im. im defaults to zero.  
c.conjugate() conjugate of the complex number c. (Identity on real numbers)  
divmod(x, y) the pair (x // y, x % y) (3)(4)
pow(x, y) x to the power y (3)(7)
x ** y x to the power y (7)
       不同類型的numerics可以混合運算,遵循規則類似c,也即小範圍向大範圍轉型,int<long<float<complex       整除/ :  結果總是整數,哪怕除數、被除數不是整數,而且結果總是趨向負無窮大,-1/2=-1       0的0次冪:pow(0,0) =1, 0**0=1       NaN: not a number  ,  INF:無窮大,-inf +inf  ,  float('nan') float('+inf') float('-inf')       int(), long() 都是向下轉型,對應實數int long float還可以用以下方式取捨:
Operation Result Notes
math.trunc(x) x truncated to Integral  
round(x[, n]) x rounded to n digits, rounding ties away from zero. If n is omitted, it defaults to 0.  四捨五入
math.floor(x) the greatest integral float <= x  
math.ceil(x) the least integral float >= x  

   bool布爾:用於if/while後做條件判斷      True:非False即為True      False: None, False, 數字類型0,空容器,包括Null 字元串‘’, class的__nonzero__() 或__len__返回0或False的執行個體      bool運算子:or and not, 遵循類似java/c的short-circuit, not比non-Boolean operator優先順序低,not a==b 等價於not (a==b)
   比較子: 也用於所有類型的比較,優先順序比Boolean operator高,且支援x<y<z這樣的寫法,x<y<z 等價x<y and y < z 且前者y僅計算一次,都遵循短路原則;不同類型的對象比較結果都是False,除非是不同類型數字或字串比較,比如0==0L, ‘abc’==u'abc'返回True
Operation Meaning Notes
< strictly less than  
<= less than or equal  
> strictly greater than  
>= greater than or equal  
== equal  
!= 或 <> not equal (1)
is object identity  
is not negated object identity  

    bitwise operation: 位元運算只對整數操作有意義,位元運算優先順序比數字運算子低,但比比較子高; ~與其他的一元運算子優先順序(+,-)相同,以下表格中優先順序從低到高, 負數移位會拋出ValueError異常
Operation Result Notes
x | y bitwise or of x and y  
x ^ y bitwise exclusive or of x andy  
x & y bitwise and of x and y  
x << n x shifted left by n bits (1)(2)
x >> n x shifted right by n bits (1)(3)
~x the bits of x inverted  
        int.bit_length():擷取int bit表示長度        long.bit_length():擷取long bit表示長度       字元:長度為1的字串,也即沒有單個字元    字串: 單引號'abc' 或雙引號''abc" 或三個連續單/雙引號'''表示多行字串,字串可理解為常量位元組數組或位元組容器,類似Java中String,也不能通過變數改變指向的字串, s='abc'; id(s) == id('abc')。    字串上常用操作:        長度:容器統一用len(),        子串:容器分區操作符[]    'abcd'[1:3]='bc'        分隔:split/rsplit        尋找/替換:find/rfind 沒找到返回-1; index/rindex沒找到拋ValueError, replace        trim:  strip/lstrip/rstrip編/解碼:只能對str解碼 str('漢').decode('UTF-8'), 只能對Unicode編碼 u('漢').encode('UTF-8')        大小寫轉換: lower/uper        判斷:isalnum/isalpha/isdigit/islower/isspace/isupper/startwith/endwith格式化: %+tuple/dict,類似c語言sprintf,一個參數'%d' % 1 = '1'  ; 兩個參數'%s, %s' %('a','b') = 'a,b'; 指             定預留位置%(mapping key)+類型字元,mapping key需加括弧'%(key1)s, %(key2)d' %{'key1':'a', 'key2':1}='a,1'

     Python中很容易擷取協助:               help(object):顯示協助資訊               dir(object) :顯示所有方法               object.__doc__   :顯示文檔


python資料類型

CS是一個引用。
引用和指標的區別你應該很清楚吧,前者是記憶體的別名,後者是記憶體位址的儲存變數。
CS是一個引用,返回C語言自然要用指標變數來接收而不是指標接受。
指標應該接受的是id(CS)。
*指標接受cs。

附:ID函數實際運行
>>> cd
'abcdef'
>>> id(cd)
35255712
 
對於python數實值型別的簡單問題

使用內建函數id,返回對象的標識來理解下:

>>> a=1
>>> id(a)
31118360L
>>> a=2
>>> id(a)
31118336L
>>> b=[]
>>> id(b)
44908360L
>>> b.append(1)
>>> id(b)
44908360L
>>> a="hello"
>>> id(a)
44919944L
>>> a += " world"
>>> id(a)
44958608L

可以看出對於整數和字串,值的改變實際代表對象本身id變了,也就是變數名綁定到了新的對象上
而對於列表,可以改變它的值
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.