六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 36|回复: 0

Python学习系列(五)

[复制链接]

升级  17.33%

18

主题

18

主题

18

主题

秀才

Rank: 2

积分
76
 楼主| 发表于 2013-1-15 02:38:04 | 显示全部楼层 |阅读模式
本节主要学习Python 3.1的复杂数据类型,内存管理和模块,下面做了一个Sample:
'''Created on 2010-4-5@author: Jamson Huang'''import os#import filecmpimport copy#Python常用的module: os#pdb=>调试器#logging=>记录器#profile,hotshot,cProfile:性能调试器#所有类的基类都是Objectif __name__ == '__main__':    #global zone        tempStr = 'start to learn python class'    intA = 5    intB = 6    #type():the type of all objects is 'type'       def typeFunc():        print(type('42'))        print(type(type('42')))        print()    typeFunc()    #NoneType/None(Python 3.1)<=>Null(Python 2.6)    def noneFunc():        print(type(None))        if (None):            print('None is False')        else:            print('None is not True')        a = 'adajkd'    #两个对象之间的比较                if (a is None):            print('a is None')        else:            print('a is not None')            noneFunc()    def moduleFunc():        print(os.name)    moduleFunc()    #CALL self-definiton class#    policy = Policy() #    policy.setPolicyId('1111111')#    print(policy.getPolicyId())       #Python的内存管理:同java语言,程序员本身不需要关心它。由于python采用引用计数的方式,所以垃圾收集器    #会回收那些计数器为0的变量和类,当然,也可以用del来直接做回收处理。其实,这点同java类似。      #Seqence[start1:end1,start2:ed2,...]    def seqFunc():        print(tempStr[::-1])         print(tempStr[::-2])    seqFunc()    #内建函数:cmp(obj1,obj2)/str(obj)/repr(obj),'obj'/type()/isinstance()#    print(cmp(str(intA), str(intB)))     #工厂函数:file()/dict()/bool()/tuple()/basestring()/unicode()    print(str(tempStr))    #id():变量和实例在内存中的标识符    #存储模型:1.标量/原子类型(数值,字符串)2.容器模型(列表,元组,字典)        #更新模型: 1.变量类型:列表,字典;2.不可变类型:数值,字符串,元组    #访问模型: 1.直接访问:数字; 2.顺序访问:字符串,列表,元组;3.映射访问:字典        def complexTypeFunc():        print(id(tempStr))            aList = ['china', 'shanghai']        print(id(aList))        aList.append('Python')        print(aList)        print(id(aList))        #列表/元组类型:列表是可变类型,而元组是不可变类型        aList.append(['3333','4444'])        print(aList)        print(aList[3])        aTuple = ('china','shanghai',['aaaa','bbbb'])        print(aTuple)        print(id(aTuple))        print(aTuple.index('shanghai'))        print(aTuple[::1])        print(aTuple[:1])        print(aTuple[1])        #copy和deepCopy:deepCopy会完全Create一个新的容器类型。                newTuple = copy.copy(aTuple)        print('Copy:', newTuple)        print(id(newTuple))        newDeepTuple = copy.deepcopy(aTuple)        print('deepCopy:', newDeepTuple)        print(id(newDeepTuple))    complexTypeFunc()     
run Python, Console输出如下:
<class 'str'><class 'type'><class 'NoneType'>None is not Truea is not Nonentssalc nohtyp nrael ot tratssacnhy re ttasstart to learn python class1161078412877952['china', 'shanghai', 'Python']12877952['china', 'shanghai', 'Python', ['3333', '4444']]['3333', '4444']('china', 'shanghai', ['aaaa', 'bbbb'])128681041('china', 'shanghai', ['aaaa', 'bbbb'])('china',)shanghaiCopy: ('china', 'shanghai', ['aaaa', 'bbbb'])12868104deepCopy: ('china', 'shanghai', ['aaaa', 'bbbb'])12878432
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表