1. 程式人生 > >python 學習筆記3------概述3

python 學習筆記3------概述3

1、python物件3個屬性:身份(id()獲取),型別(type()獲取),值

2、物件值的比較和物件身份的比較:前者就不說了,後者用is或is not,表示兩個變數是否實質為同一個物件的引用。

3、檢查型別

#!/usr/bin/env python

def displayNumType(num):
    print num, 'is',
    if isinstance(num, (int, long, float, complex)):
        print 'a number of type:', type(num).__name__
    else:
        print 'not a number at all!!'


import types
def displayNumType2(num):
    print num, 'is a number of type: ',
    if type(num) == types.IntType:
        print types.IntType
    elif type(num) == types.StringType:
        print types.StringType
    elif type(num) == types.FloatType:
        print types.FloatType
    elif type(num) == types.ComplexType:
        print types.ComplexType
    elif type(num) == types.LongType:
        print types.LongType
    else:
        print 'not a number at all!!'
        
        
displayNumType(-69)
displayNumType(9999999999999999999999L)
displayNumType(98.6)
displayNumType(-5.2+1.9j)
displayNumType('xxx')

print '==========================================='

displayNumType2(-69)
displayNumType2(9999999999999999999999L)
displayNumType2(98.6)
displayNumType2(-5.2+1.9j)
displayNumType2('xxx')

4、內建函式總結