您好,登錄后才能下訂單哦!
本篇文章為大家展示了python中怎么實(shí)現(xiàn)數(shù)據(jù)類型判定,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
一、 判斷數(shù)據(jù)類型
0、type(x)
type()可以接收任何東西作為參數(shù)――并返回它的數(shù)據(jù)類型。整型、字符串、列表、字典、元組、函數(shù)、類、模塊,甚至類型對(duì)象都可以作為參數(shù)被 type 函數(shù)接受。
>>> type(1)
<type 'int'>
>>> li = []
>>> type(li)
<type 'list'>
>>> import odbchelper
>>> type(odbchelper)
<type 'module'>
>>> import types
>>> type(odbchelper) == types.ModuleType
True
二、 數(shù)據(jù)類型轉(zhuǎn)換
1、chr(i)
chr()函數(shù)返回ASCII碼對(duì)應(yīng)的字符串。
>>> print chr(65)
A
>>> print chr(66)
>>> print chr(65)+chr(66)
AB
2、complex(real[,imaginary])
complex()函數(shù)可把字符串或數(shù)字轉(zhuǎn)換為復(fù)數(shù)。
>>> complex("2+1j")
(2+1j)
>>> complex("2")
(2+0j)
>>> complex(2,1)
(2+1j)
>>> complex(2L,1)
(2+1j)
3、float(x)
float()函數(shù)把一個(gè)數(shù)字或字符串轉(zhuǎn)換成浮點(diǎn)數(shù)。
>>> float("12")
12.0
>>> float(12L)
12.0
>>> float(12.2)
12.199999999999999
4、hex(x)
hex()函數(shù)可把整數(shù)轉(zhuǎn)換成十六進(jìn)制數(shù)。
>>> hex(16)
'0x10'
>>> hex(123)
'0x7b'
5、long(x[,base])
long()函數(shù)把數(shù)字和字符串轉(zhuǎn)換成長(zhǎng)整數(shù),base為可選的基數(shù)。
>>> long("123")
123L
>>> long(11)
11L
6、list(x)
list()函數(shù)可將序列對(duì)象轉(zhuǎn)換成列表。如:
>>> list("hello world")
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
>>> list((1,2,3,4))
[1, 2, 3, 4]
7、int(x[,base])
int()函數(shù)把數(shù)字和字符串轉(zhuǎn)換成一個(gè)整數(shù),base為可選的基數(shù)。
>>> int(3.3)
3
>>> int(3L)
3
>>> int("13")
13
>>> int("14",15)
19
8、min(x[,y,z...])
min()函數(shù)返回給定參數(shù)的最小值,參數(shù)可以為序列。
>>> min(1,2,3,4)
1
>>> min((1,2,3),(2,3,4))
(1, 2, 3)
9、max(x[,y,z...])
max()函數(shù)返回給定參數(shù)的最大值,參數(shù)可以為序列。
>>> max(1,2,3,4)
4
>>> max((1,2,3),(2,3,4))
(2, 3, 4)
10、oct(x)
oct()函數(shù)可把給出的整數(shù)轉(zhuǎn)換成八進(jìn)制數(shù)。
>>> oct(8)
'010'
>>> oct(123)
'0173'
11、ord(x)
ord()函數(shù)返回一個(gè)字符串參數(shù)的ASCII碼或Unicode值。
>>> ord("a")
97
>>> ord(u"a")
97
12、str(obj)
str()函數(shù)把對(duì)象轉(zhuǎn)換成可打印字符串。
>>> str("4")
'4'
>>> str(4)
'4'
>>> str(3+2j)
'(3+2j)'
13、tuple(x)
tuple()函數(shù)把序列對(duì)象轉(zhuǎn)換成tuple。
>>> tuple("hello world")
('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd')
>>> tuple([1,2,3,4])
(1, 2, 3, 4)
上述內(nèi)容就是python中怎么實(shí)現(xiàn)數(shù)據(jù)類型判定,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。