python 学习笔记3------概述3

本文详细解析了Python对象的三个关键属性:身份、类型和对象比较,并通过实例展示了如何使用内建函数检查对象类型。文章还提供了一个内建函数总结,帮助读者快速了解Python中用于类型检查的内建函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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、内建函数总结



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值