• Python(二)之对象类型


    本篇记录Python对象类型

    开始仍然是先掌握几个查看对象有用的函数,id,type,print

    • 查看对象身份、类型、值:记住以下两个命令id,type

    • 两个对象的比较

      以上三个分别是 身份比较、对象值比较、对象类型比较。

    核心数据类型:

           数字:int,long,float,complex 复数,bool

           字符:str,unicode

           列表:list

           字典:dict

           元祖:tuple

           文件:file

           其他类型:集合(set),frozenset,类类型,None

       其他文件类工具:pipes,fifos,sockets .....

    • 类型转换:

                  str(),repr()或format()将非字符型数据转化为字符;

                         str输出的结果和print输出一样,repr表示程序中某个对象的精确值,format利用指定的特殊格式转化。

                         int():转化为整数

                         float():转化为浮点型

                         list(s):将子串转化为列表

                         tuple(s):将子串转化为元组

                         set(s):将子串转化为集合

                         frozen(s):将子串s转化为不可变集合

                         dict(d):创建字典,d是元组的序列

                         In [25]: list=[('a',1),('b',11),('c',111)]

                         In [27]: dict1 = dict(list)

                         In [28]: print dict1

                         {'a': 1, 'c': 111, 'b': 11}

                         chr(x):将整数转化为字符

                         ord(x):将字符转化为整数

                         hex(x):将整数转化为16进制字符串

                         bin(x):转化为二进制字符串

                         oct(x):转化为八进制字符串

                         In [34]: type(oct(8))

                         Out[34]: str

                         In [35]: type(bin(8))

                         Out[35]: str

                         In [36]: type(hex(8))

                         Out[36]: str

                         In [37]: type(ord(8))

    数字类型:

    •        Python的数字字面量:布尔型、整数、浮点型、复数

                  布尔型:True 1

                             False 0

     

    高级算术函数:

    In [8]: import math

    In [9]: print math.

    math.acos       math.copysign   math.floor      math.log        math.sinh

    math.acosh      math.cos        math.fmod       math.log10      math.sqrt

    math.asin       math.cosh       math.frexp      math.log1p      math.tan

    math.asinh      math.degrees    math.fsum       math.modf       math.tanh

    math.atan       math.e          math.hypot      math.pi         math.trunc

    math.atan2      math.exp        math.isinf      math.pow       

    math.atanh      math.fabs       math.isnan      math.radians   

    math.ceil       math.factorial  math.ldexp      math.sin

    序列类型:

    • 字符类型:

                  字符串字面量:把文本放入单引号、双引号或三引号中;三者并无明显区别,三引号可以跨行,‘’‘,”””

    python2要保存unicode编码,则在子串之前使用字符u进行标识,如下:

    In [12]: str1 = u"hello world"

    In [13]: type(str1)

    Out[13]: Unicode

    In [21]: str.

    str.capitalize  str.format      str.isupper     str.rfind       str.startswith

    str.center      str.index       str.join        str.rindex      str.strip

    str.count       str.isalnum     str.ljust       str.rjust       str.swapcase

    str.decode      str.isalpha     str.lower       str.rpartition  str.title

    str.encode      str.isdigit     str.lstrip      str.rsplit      str.translate

    str.endswith    str.islower     str.mro         str.rstrip      str.upper

    str.expandtabs  str.isspace     str.partition   str.split       str.zfill

    str.find        str.istitle     str.replace     str.splitlines 

    注意strip,[chrs] 是集合对象。

    In [26]: str3="098python980"

    In [27]: str3.strip('890')

    Out[27]: 'python'

    In [30]: str4.strip('098')

    Out[30]: 'python'

    In [31]: str4="0098python0009989"

    In [32]: str4.strip('098')

    Out[32]: 'python'

    • 文档子串: 模块、类或函数的第一条语句是一个字符的话,该字符串就成为文档字符串,可以使用__doc__属性引用。

    In [15]: def printName(): 

       ....:     "test function"

       ....:     print "hello python"

       ....:    

    In [16]printName()

    hello python

    #带有小括号,是调用运算符,调用函数本身

    In [17]: printName().__doc__

    hello python

    不加括号,表示引用函数对象

    In [18]: printName.__doc__

    Out[18]: 'test function'

    运算符:

    索引、切片、扩展切面(带有步长),切面后的结果会生成的新对象。i和j支持负数。

    正向是0计数,反向是-1开始计数

    内置函数:min(),max()

     

    • 列表:

           容器类型

                  任意对象的有序集合,通过索引访问其中的元素,可变对象,支持异构(一个列表中包含不同类型的元素),任意嵌套

           支持在原处修改:

                  修改指定的索引元素,修改指定分片,删除语句,内置方法。

                  list1+list2  列表合并,返回一个新列表,

                  str1+str2   子串相加,返回一个新子串

                  list1*N:把list1重复N次,返回一个新列表

                  in:成员关系判断字符,用法obj in container

                  not in:

                  列表复制:list1=list2[:]

                  deepcopy:

                  In [36]: import copy

    In [37]: copy.

    copy.Error           copy.copy            copy.dispatch_table  copy.name

    copy.PyStringMap     copy.deepcopy        copy.error           copy.t

    In [37]: list1=[1,2,3,4,5]

    In [38]: list2=copy.deepcopy(list1)

    In [39]: print list2

    [1, 2, 3, 4, 5]

    In [40]: id(list1)

    Out[40]: 154380428

    In [41]: id(list2)

    Out[41]: 154128620

     

    In [13]: list1=[1,2,3,[4,5],"y",{"x":5}]

    In [14]: print list1

    [1, 2, 3, [4, 5], 'y', {'x': 5}]

    In [15]: list1[1]=66

    In [16]: print list1

    [1, 66, 3, [4, 5], 'y', {'x': 5}]

    In [17]: list1[1:3]

    Out[17]: [66, 3]

    In [18]: list1[1:3]=[]

    In [19]: print list1

    [1, [4, 5], 'y', {'x': 5}]

    In [21]: del(list1[1:])

    In [22]: print list1

    In [23]: list3=[1,2,3,4,5]

    In [24]: list

    list   list1  list2  list3 

    In [24]: list3.

    list3.append   list3.extend   list3.insert   list3.remove   list3.sort    

    list3.count    list3.index    list3.pop      list3.reverse 

    In [24]: list3.append('x')

    In [25]: print list3

    [1, 2, 3, 4, 5, 'x']

    • 元组:

           表达式符号:()

           容器类型

                  任意对象的有序的集合,通过索引访问其中的元素,不可变对象,长度固定,异构,

                  嵌套。

           常见操作:

                  t1=()

                  t2=(1,)

                  t3=(1,2)

                  t1+t2、t1*N、in、not in

           虽然元组本身不可变,但如果元组内嵌套了可变类型的元素,那么此类元素的修改不会返回新元组。

    字典:

    dict:

           字典在其他编程语言中又称作关联数组或散列表;

           通过键实现元素存取;无序集合;可变类型容器,长度可变,异构,嵌套。

           {‘key’:value1,’key2:value2’}

           {}空字典;

           In [1]: d1={}

    In [2]: d1.

    d1.clear       d1.get         d1.iteritems   d1.keys        d1.setdefault 

    d1.copy        d1.has_key     d1.iterkeys    d1.pop         d1.update     

    d1.fromkeys    d1.items       d1.itervalues  d1.popitem     d1.values

           字典复制:d2 = d1.copy()

           d1.iteriteams:返回一个迭代器对象

    集合:

    集合:无序排列、可哈希的的元素集合

                  支持集合关系测试

                  成员关系测试

                  in ,not in,迭代

                  class set(object)

     |  set() -> new empty set object

     |  set(iterable) -> new set object

                  不支持:索引、元素获取、切片

    集合的类型:set(),frozenset()不可变集合

    没有特定语法格式,只能通过工厂函数创建

    总结:容器、类型、对象

    1、列表,元素,字典可以不使用续行符进行多行定义。

    2、所有对象子昂都有引用计数,使用sys的计数函数可以获取引用次数sys.getrefcount(name),使用del(name),删除引用。

    3、对于列表和字典都支持两种类型的复制操作,浅复制和深复制;深复制的方法可使用copy模块中的deepcopy()实现。

    4、Python中的所有对象都是“第一类的”,这意味着使用标识符命名的所有的对象都具有相同状态,于是,能够命名所有对象都可以直接当数据进行处理

    5、所有序列都支持迭代

    6、所有序列都支持的操作和方法:

           s[i]

           s[i:j]

           s[i:j:stride]

           len(s)

           min(s)

           max(s)

           sum(s)

           all(s)

           any(s)

           s1+s2:连接

           s1 * N:重复

           obj in s1:成员关系判断

           obj not in

    7、可变序列的操作:

           s1[index] = value:元素赋值

           s1[I:j] = t :切片赋值

           del s1[index]

           del s1[i:j]

           del s1[i:j:stride]

  • 相关阅读:
    Spring、Spring Boot 和 Spring Cloud 的关系
    Spring Boot 如何设置支持跨域请求?
    什么是嵌入式服务器?我们为什么要使用嵌入式服务器呢?
    nginx配置域名,不要端口
    域名解析以及nginx服务器设置
    为什么使用nginx
    RabbitMQ下载与安装(window版)
    spring的@ComponentScan注解
    spring的@EnableScheduling注解
    日志切割: logrotate、python、shell实现
  • 原文地址:https://www.cnblogs.com/jjzd/p/6624495.html
Copyright © 2020-2023  润新知