zoukankan      html  css  js  c++  java
  • 20201224-4

    1-1
    #
    dog class Dog(): """A simple attempt to model a dog.""" def __init__(self, name, age): """Initialize name and age attributes.""" self.name = name self.age = age def sit(self): """Simulate a dog sitting in response to a command.""" print(self.name.title() + " is now sitting.") def roll_over(self): """Simulate rolling over in response to a command.""" print(self.name.title() + " rolled over!") my_dog = Dog('willie', 6) your_dog = Dog('lucy', 3) print("My dog's name is " + my_dog.name.title() + ".") print("My dog is " + str(my_dog.age) + " years old.") my_dog.sit() print(" My dog's name is " + your_dog.name.title() + ".") print("My dog is " + str(your_dog.age) + " years old.") your_dog.sit() ---> My dog's name is Willie. My dog is 6 years old. Willie is now sitting. My dog's name is Lucy. My dog is 3 years old. Lucy is now sitting.
    2-1
    使用模块collections中的OrderedDict类。OrderedDict实例的行为 几乎与字典相同,区别只在于记录了键—值对的添加顺序
    创建了OrderedDict 类的一个实例,并将其存储到favorite_languages中。
    请注意,这里没有使用花括号,而是调用 OrderedDict()来创建一个空的有序字典,并将其存储在favorite_languages中
    这是一个很不错的类,它兼具列表和字典的主要优点(在将信息关联起来的同时保留原来的 顺序)。
    等你开始对关心的现实情形建模时,可能会发现有序字典正好能够满足需求。
    # favorite languages
    
    from collections import OrderedDict
    
    favorite_languages = OrderedDict()
    
    favorite_languages['jen'] = 'python'
    favorite_languages['sarah'] = 'c'
    favorite_languages['edward'] = 'ruby'
    favorite_languages['phil'] = 'python'
    
    for name, language in favorite_languages.items():
        print(name.title() + "'s favorite language is " +
            language.title() + ".")
  • 相关阅读:
    Memcached简介
    TP5 volist
    Getting command line access to PHP and MySQL running MAMP on OSX
    PHP use
    PHP 命名空间(namespace)
    mac 使用 pf 做端口转发
    微信测试帐号如何设置URL和Token,以及相关验证的原理
    ionic开发笔记
    Eclipse下配置Maven
    css引用第三方字体库
  • 原文地址:https://www.cnblogs.com/azxsdcv/p/14185155.html
Copyright © 2011-2022 走看看