Python數(shù)據(jù)類型

Numbers:
Python's built-in core data types are in some cases also called object types. There are four built-in data types for numbers:

Integer
Normal integers
e.g. 4321
Octal literals (base 8)
A number prefixed by a 0 (zero) will be interpreted as an octal number
example:

>>> a = 010
>>> print a
8 Alternatively, an octal number can be defined with "0o" as a prefix: >>> a = o10
>>> print a
8

Hexadecimal literals (base 16)
Hexadecimal literals have to be prefixed either by "0x" or "0X".
example:

>>> hex_number = 0xA0F
>>> print hex_number
2575

Long integers
these numbers are of unlimited size
e.g.42000000000000000000L

Floating-point numbers
for example: 42.11, 3.1415e-10

Complex numbers
Complex numbers are written as <real part> + <imaginary part>j
examples:

>>> x = 3 + 4j
>>> y = 2 - 3j
>>> z = x + y
>>> print z
(5+1j)

String:
有三種方式定義字符串:
1.'xxxxx'
2."xxxxxx"
3.'''xxxxxxxx'''

字符串的一些操作:
1."Hello"+"World"
2."xx"*3 -> "xxxxxx"
3."Python"[0] will return in "P"
4."Python"[2:4]
4.len("Python")

字符串不可被改變

If both a and b are strings, "a is b" checks if they have the same identity, i.e. share the same memory location. If "a is b" is True, then it trivially follows that "a == b" has to be True as well. But "a == b" True doesn't imply that "a is b" is True as well!

>>> a = "Linux"
>>> b = "Linux"
>>> a is b
True

The Pitfalls of Repetitions

In our previous examples we applied the repetition operator on strings and flat lists. We can apply it to nested lists as well:

>>> x = ["a","b","c"]
>>> y = [x] * 4
>>> y
[['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c']]
>>> y[0][0] = "p"
>>> y
[['p', 'b', 'c'], ['p', 'b', 'c'], ['p', 'b', 'c'], ['p', 'b', 'c']]
>>> 
image.png

complex(real[,imag]])
創(chuàng)建一個(gè)值為real + imag * j的復(fù)數(shù)或者轉(zhuǎn)化一個(gè)字符串或數(shù)為復(fù)數(shù)。如果第一個(gè)參數(shù)為字符串,則不需要指定第二個(gè)參數(shù)。
參數(shù)real: int, long, float或字符串;
參數(shù)imag: int, long, float。

>>> complex(1, 2)
(1 + 2j)
#數(shù)字
>>> complex(1)
(1 + 0j)
#當(dāng)做字符串處理
>>> complex("1")
(1 + 0j)
#注意:這個(gè)地方在“+”號(hào)兩邊不能有空格,也就是不能寫成"1 + 2j",應(yīng)該是"1+2j",否則會(huì)報(bào)錯(cuò)
>>> complex("1+2j")
(1 + 2j)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容