zoukankan      html  css  js  c++  java
  • [Head First Python]6. summary

    1- 字典-内置数据结构,数据值与键值关联

    键-字典中查找部分

    值-字典中数据部分

    使用dict()工厂函数或者只用{}可以创建一个空字典

    >>> list = {}
    >>> list['name']='hello'
    >>> list['pwd']='world'
    >>> list['name']
    'hello'
    >>> list['Occupation']=['1','2','3']
    >>> list['Occupation'][-1]
    '3'
    >>> list['Occupation'][-2]
    '2'

    2- 类 用def __init__()方法初始化对象实例 

    类中每个方法都必须提供self作为第一个参数

    类中每个属性前面都必须有self,从而将数据与其实例关联

    类可以从0创建,也可以从python内置类或其他定制类继承

    类可以放置到模块中

    从0创建

     1 class hi:
     2     def __init__(self, a_name, a_dob=None, a_times=[]):
     3         self.name = a_name
     4         self.dob = a_dob
     5         self.times = a_times
     6 
     7 sarah = hi('sarah', '2002-1-1', ['1','2','0','-1'])
     8 james = hi('james')
     9 
    10 print(type(sarah))
    11 print(type(james))
    12 
    13 print(sarah)
    14 print(james)
    15 
    16 print(sarah.name + ' ' +str (sarah.dob) + ' ' +str(sarah.times))
    17 print(james.name + ' ' +str (james.dob) + ' ' +str(james.times))

    继承内置类

     1 class NameList(list):
     2     def __init__(self,a_name):
     3         list.__init__([])
     4         self.name = a_name
     5 
     6 johnny = NameList("John Paul Jones")
     7 print(type(johnny))
     8 print(dir(johnny))
     9 
    10 johnny.append("Bass Player")
    11 johnny.extend(["a","b","c"])
    12 print(johnny)
    13 print(johnny.name)
  • 相关阅读:
    html5 sessionStorage util
    配置php redis 扩展
    css颜色字符串转换, 字符串转化为驼峰形式
    根据包名,在指定空间中创建对象
    列表习题
    正则题--什么时候才能到200+
    每日练习0424---函数和列表 元祖 字典的习题 sort和sorted总结
    习题之----列表和字典
    求字母的个数 求单词的个数
    字符串练习题(2)
  • 原文地址:https://www.cnblogs.com/galoishelley/p/3796641.html
Copyright © 2011-2022 走看看