python入门到实践-----笔记

第一部分 基础知识

第 1 章 运行第一个程序 打印消息 “Hello world!” 。
第 2 章 变量中存储信息以及如何使用文本和数字。
第 3 4 章 列表 使用列表能够在一个变量中存储任意数量的信息,从而高效地处理数据:只需几行代码,你就能够处理数百、数千乃至数百万个值。
第 5 章 if 条件判断
第 6 章 使用 Python 字典,将不同的信息关联起来。与列表一样,你也可以根据需要在字典中存储任意数量的信息。
第 7 章 交互式程序 while 循环
第 8 章 函数。函数是执行特定任务的被命名的代码块
第 9 章 类,它让你能够模拟实物,如小狗、小猫、人、汽车、火箭等,让你的代码能够表示任何真实或抽象的东西。
第 10 章 如何使用文件,以及如何处理错误以免程序意外地崩溃。你需要在程序关闭前保存数据,并在程序再次运行时读取它们。你将学习 Python 异常,它们让你能够未雨绸缪,从而让程序妥善地处理错误。
第 11 章为代码编写测试,以核实程序是否像你期望的那样工作。这样,扩展程序时,你就不用担心引入新的 bug要想脱离初级程序员的阵容,跻身于中级程序员的行列,测试代码是你必须掌握的基本技能之一。

第一章 第一个python程序

[root@localhost ~/python]# python #进入python2.7交互式界面 相关版本以及提示信息
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>> print("Hello Python World") #也可将代码保存在文件中中使用python命令运行 .py文件
Hello Python World

第二章 变量以及简单数据类型:字符串 数字

2.1变量命名规范

  • 1.变量名只能包含字母、数字和下划线。变量名可以字母或下划线打头,但不能以数字打头,
    例如,可将变量命名为 message_1 ,但不能将其命名为 1_message 。
  • 2.变量名不能包含空格,但可使用下划线来分隔其中的单词。例如,变量名 greeting_message 可行,但变量名 greeting message 会引发错误。
  • 3.不要将 Python 关键字和函数名用作变量名,即不要使用 Python 保留用于特殊用途的单词,如 print (请参见附录 A.4 )。
  • 4.变量名应既简短又具有描述性。例如, name 比 n 好, student_name 比 s_n 好, name_length 比 length_of_persons_name 好。
  • 慎用小写字母 l 和大写字母 O ,因为它们可能被人错看成数字 1 和 0 。

        # vim hello_world.py
        message = "hello world"   #定义一个变量
        print(message)
        message = "hello python world"
        print(message)
    
        输出信息:
        hello world
        hello python world
        python将记录变量最新值

2.2字符串

  • pyhon 中,用引号括起的都是字符串,其中的引号可以是单引号,也可以是双引号。例如:
    print('I told my friend, "Python is my favorite language!"')
    print("The language 'Python' is named after Monty Python, not the snake.")
    print("One of Python's strengths is its diverse and supportive community.")

  • 调用一些简单的方法进行处理 格式: 实例名.方法名
        >>> name = "test_name"
        >>> print(name.title())
        Test_Name
        >>> print(name.upper())
        TEST_NAME
        >>> print(name.lower())
        test_name
        其他方法:
        rstrip()  删除字符串右侧空白
        lstrip()  删除左侧空白
        strip()   删除两端空白
  • 字符串 合并或拼接 用+号
         制表符:"\t"   
         换行符:"\n"
        first_name = "ada"
        last_name = "lovelace"
        full_name = first_name + " " + last_name
        print("Hello, " + full_name.title() + "!")
        print("Languages:\nJavaScript\n\t Python")

    注意:在 Python 2 中,无需将要打印的内容放在括号内。
    从技术上说, Python 3 中的 print 是一个函数,因此括号必不可少。有些 Python 2 print 语句也包含括号,但其行为与 Python 3 中
    稍有不同。简单地说,在 Python 2 代码中,有些 print 语句包含括号,有些不包含。

  • 函数的使用方式 #函数名(参数名)
    使用str()函数
    age = 23
    message = "Happy " + str(age) + "rd Birthday!"
    print(message)

2.3数字

  • 整数 :+ - x / 取余:% 乘方:*
    实例:(2 + 3)
    4
  • 浮点数 Python 将带小数点的数字都称为 浮点数
    从很大程度上说,使用浮点数时都无需考虑其行为。你只需输入要使用的数字, Python 通常都会按你期望的方式处理它们

2.4添加 注释

注释用井号( # )标识。井号后面的内容都会被 Python 解释器忽略,如下所示:
#向大家问好
print("Hello Python people!")

2.5python 之禅 .指导原则

编程语言 Perl 曾在互联网领域长期占据着统治地位,早期的大多数交互式网站使用的都是 Perl 脚本。彼时, “ 解决问题的办法有多个 ” 被 Perl 社区奉为座右铭。这种理念一度深受大家的喜爱,因为这种语言固有的灵活性使得大多数问题都有很多不同的解决之道。在开发项目期间,这种灵活性是可以接受的,但大家最终认识到,过于强调灵活性会导致大型项目难以维护:要通过研究代码搞清楚当时解决复杂问题的人是怎么想的,既困难又麻烦,还会耗费大量的时间

        import this 
    The Zen of Python, by Tim Peters
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
    蒂姆彼得斯的Python的禅宗
    美丽胜于丑陋。
    显式比隐式更好。
    简单胜于复杂。
    复杂比复杂更好。
    Flat比嵌套更好。
    稀疏比密集好。
    可读性计数。
    特殊情况不足以打破规则。
    虽然实用性胜过纯净。
    错误不应该默默通过。
    除非明确沉默。
    面对歧义,拒绝猜测的诱惑。
    应该有一个 - 而且最好只有一个 - 明显的做法。
    尽管这种方式一开始可能并不明显,除非你是荷兰人。
    现在比永远好。
    虽然现在永远不会比*正确*更好。
    如果实现很难解释,这是一个坏主意。
    如果实现很容易解释,这可能是一个好主意。
    命名空间是一个好主意 - 让我们做更多的这些!

第三章 列表( 索引 元素的值 组织列表)

3.1列表 (索引 元素增删改查)

一系列按特定序列排列的元素组成。给列表启一个表示复数的名称是个不错的选择。在 用方括号[ ] 来表示列表,并用逗号来分隔其中的元素。
下面是一个简单的列表示例,这个列表包含几种自行车:
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)

  • 访问列表元素 需要指定元素的位置或索引
    print(bicycles[0])
  • 索引 从0开始不是1
    print(bicycles[3]) ##要访问第四个列表元素,可使用索引 3
    print(bicycles[-1].title()) ##对最后一个元素使用title()方法
  • 元素的增删改
        增:
            列表末尾添加元素 方法 append() 将元素 添加到了列表末尾,而不影响列表中的其他所有元素:
            motorcycles = []  ##定义空列表
            motorcycles.append('honda')  
            motorcycles.append('yamaha')
            motorcycles.append('suzuki')
            print(motorcycles)
        插入:
            使用方法insert()可以在列表的任意位置添加元素,但是需要指定索引以及值。
            motorcycles.insert(0, 'ducati')  #指定索引以及值
            motorcycles = ['honda', 'yamaha', 'suzuki']
            print(motorcycles)
            motorcycles[0] = 'ducati'
            print(motorcycles)
        删:根据位置或值来删除列表中的元素。
    * del 语句  条件是知道其索引,删除后,你就无法再访问它     
              del motorcycles[1]
    * pop() 方法:弹出列表末尾的元素 列表就像一个栈,而删除列表末尾的元素相当于弹出栈顶元素。将元素从列表中删除,并接着使用它的值将其保存在变量中
                popped_motorcycle = motorcycles.pop() 
                first_owned = motorcycles.pop(0) ##指定索引删除特定的值
    * remove()方法:   知道要删除的元素的值,可使用方法 remove() 
                motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
                print(motorcycles)
                tooexpensive = 'ducati'
                motorcycles.remove(tooexpensive)
                print(motorcycles)
                print("\nA " + tooexpensive.title() + " is too expensive for me.")  #变量中保存删除的元素
                回显信息:
                ['honda', 'yamaha', 'suzuki', 'ducati']
                ['honda', 'yamaha', 'suzuki']
                A Ducati is too expensive for me.
    注意:方法 remove() 只删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要使用循环来判断是否删除了所有这样的值。
    将在第七章使用循环处理上述问题 

3.2 组织列表的常见方式:sort()、 倒序打印列表 、 sorted() 、确定列表的长度}

  • 使用方法 sort() 对列表元素按字母排序 永久排序
    cars = ['bmw', 'audi', 'toyota', 'subaru']
    cars.sort() #字母顺序
    cars.sort(reverse=True) #字母倒序 向 sort() 方法传递参数 reverse=True
  • 使用方法 reverse() 反转列表元素的排列顺序 永久排序
    cars = ['bmw', 'audi', 'toyota', 'subaru']
    print(cars)
    cars.reverse()
    print(cars)
  • 使用sorted()函数进行临时性排序 格式: 函数名(传递给函数的参数)
    sorted(cars)
    ['audi', 'bmw', 'subaru', 'toyota']
  • 函数 len() 可快速获悉列表的长度
    len(cars)

第四章 操作列表

遍历列表 切片 遍历切片 复制切片 元组(定义 遍历 修改元组变量)

4.1 遍历列表 创建数字列表 列表解析

  • 使用for循环遍历列表中的元素,缩进的代码行都是循环体的一部分

        magicians = ['alice', 'david', 'carolina']
        for magician in magicians:     ##注意冒号不要丢
                print(magician.title() + ", that was a great trick!")
                print("I can't wait to see your next trick, " + magician.title() + ".\n")
        print("Thank you, everyone. That was a great magic show!")  ##循环结束后的语句
  • 使用range() 函数生成一系列数字 Python 从你指定的第一个值开始数,并在到达你指定的第二个值后停止,因此输出不包含第二个值(这里为 5)
      for value in range(1,5):
              print(value)
  • 创建数字列表 使用list()函数将range()的结果转话为列表,将range()作 为list()的参数,输出一个数字列表。
    numbers = list(range(1,6)) ##range(1,10,2) 可以指定步长(起始,结束,步长)
    print(numbers)
    创建列表实例:
            squares = [] 
            for value in range(1,11):
                    square = value**2
                    squares.append(square)
            print(squares)
            结果:
            [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  • 简单统计计算 函数(参数)
    min(digits)
    max(digits)
    sum(digits)
  • 列表解析
    要使用这种语法,首先指定一个描述性的列表名,如 squares然后,指定一个左方括号,并定义一个表达式,用于生成你要存储到列表中的值。
    在这个示例中,表达式为 value**2 ,它计算平方值。
    接下来,编写一个 for 循环,用于给表达式提供值,再加上右方括号。在这个示例中, for 循环为 for value in range(1,11) ,它将值1~10 提供给表达式 value**2 。

    注意:这里的 for 语句末尾没有冒号。
    squares = [value**2 for value in range(1,11)]
    print(squares)
    结果与你在前面看到的平方数列表相同:
    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

4.2 切片 遍历切片 复制列表

  • 切片:对列表的部分元素进行处理
    players = ['charles', 'martina', 'michael', 'florence', 'eli']
    print(players[0:3])
    print(players[1:4]) #提取列表的第 2~4 个元素
    print(players[:4]) #不指定第一个索引默认为开头
    print(players[2:]) #到列表末尾
    负数索引返回离列表末尾相应距离的元素,因此你可以输出列表末尾的任何切片。例如,如果你要输出名单上的最后三名队员:
    可使用切片 players[-3:]
  • 遍历切片 在for循环中遍历切片

        players = ['charles', 'martina', 'michael', 'florence', 'eli']
        print("Here are the first three players on my team:")
        for player in players[:3]:
            print(player.title())
  • 复制列表 创建一个包含整个列表的切片,同时省略起始索引和终止索引[:]

    ----------------------------------------------------------------
    my_foods = ['pizza', 'falafel', 'carrot cake']
    friend_foods = my_foods[:]     #起始末尾复制列表
    ----------------------------------------------------------------
    my_foods = ['pizza', 'falafel', 'carrot cake']
    friend_foods = my_foods[:]     #复制列表
    my_foods.append('cannoli')
    friend_foods.append('ice cream')
    print("My favorite foods are:")
    print(my_foods)
    print("\nMy friend's favorite foods are:")
    print(friend_foods)
    ----------------------------------------------------------------
    friend_foods = my_foods   #仅仅简单赋值
    my_foods.append('cannoli')
    friend_foods.append('ice cream')
    print("My favorite foods are:")
    print(my_foods)
    print("\nMy friend's favorite foods are:")
    print(friend_foods)

    注意:这里将 my_foods 赋给 friend_foods ,而不是将 my_foods 的副本存储到 friend_foods (见❶)。这种语法实际上是让 Python 将新变量 friend_foods 关联到包含在 my_foods 中的列表,因此这两个变量都指向同一个列表。鉴于此,当我们将 'cannoli' 添加到 my_foods 中时,它也将出现在 friend_foods 中;同样,虽然 'icecream' 好像只被加入到了 friend_foods 中,但它也将出现在这两个列表中。

4.3 元组 (定义 遍历 修改)

  • 元组:
    列表非常适合用于存储在程序运行期间可能变化的数据集。列表是可以修改的,这对处理网站的用户列表或游戏中的角色列表至关重要然而,有时候你需要创建一系列不可修改的元素,元组可以满足这种需求。 Python 将不能修改的值称为 不可变的 ,而不可变的列表被称为 元组。
    dimensions = (200, 50) #(定义元组元素的值,号分割)
    ❶ dimensions[0] = 250 #不可给元组的元组的元素赋值
    ❶处的代码试图修改第一个元素的值,导致 Python 返回类型错误消息。由于试图修改元组的操作是被禁止的,因此 Python 指出不能给元组的元素赋值:
    Traceback (most recent call last):
    File "dimensions.py", line 3, in <module>
    dimensions[0] = 250
    TypeError: 'tuple' object does not support item assignment
  • for遍历元组
        dimensions = (200, 50)
        for dimension in dimensions:
            print(dimension)
  • 修改元组变量
    虽然不能修改元组的元素,但可以给存储元组的变量赋值。因此,如果要修改前述矩形的尺寸,可重新定义整个元组:
        dimensions = (200, 50)
        print("Original dimensions:")
        for dimension in dimensions:
            print(dimension)
        dimensions = (400, 100)
        print("\nModified dimensions:")
        for dimension in dimensions:
            print(dimension)

第五章 条件测试if语句

if 语句的核心都是一个值为 True 或 False 的表达式,这种表达式被称为 条件测试 或者 布尔表达式 ,结果要么为 True ,要么为 False 。Python 根据条件测试的值为 True 还是 False 来决定是否执行 if 语句中的代码如果条件测试的值为 True , Python 就执行紧跟在 if 语句后面的代码;如果为 False , Python 就忽略这些代码。

5.1简单示例:

        cars = ['audi', 'bmw', 'subaru', 'toyota']
        for car in cars:
                if car == 'bmw':
                         print(car.upper())
                else:
                         print(car.title())

5.2 常用条件测试

  • 字符串比较=或 !=

        car = 'bmw' #定义变量的值   
        car == 'bmw' #逻辑判断是否相等 结果为True
    
        requested_topping = 'mushrooms'
        if requested_topping != 'anchovies':       #if判断是否不等注意必须:
                print("Hold the anchovies!")
    
        Python 中检查是否相等时区分大小写
        car.lower() == 'audi' #先进行转换然后比较
  • 数值比较

    >> age = 19
    >> age < 21
    True
    >> age <= 21
    True
    >> age > 21
    False
    >> age >= 21
    False

  • 检测多个条件 关键字and or

    >> age_0 = 22
    >> age_1 = 18
    age_0 >= 21 and age_1 >= 21 ##相当与bash中的&&
    age_0 >= 21 or age_1 >= 21 ##相当与bash中的||

  • 检测特定的值是否在列表中 in 、not in
        banned_users = ['andrew', 'carolina', 'david']
        user = 'marie'
        if user not in banned_users:
             print(user.title() + ", you can post a response if you wish.")
  • 布尔表达式
    布尔表达式 ,它不过是条件测试的别名。与条件表达式一样,布尔表达式的结果要么为 True ,要么为 False 。可以作为条件。
    game_active = True
    can_edit = False

    5.3 if语句

  • if语句
  • if-else
        age = 17
        if age >= 18:
                print("You are old enough to vote!")
        else:
                print("Sorry, you are too young to vote.")
                print("Please register to vote as soon as you turn 18!")
  • if-elif-else
    Python 只执行 if-elif-else 结构中的一个代码块,它依次检查每个条件测试,直到遇到通过了的条件测试。测试通过后, Python 将执行紧跟在它后面的代码,并跳过余下的测试
        age = 12
        if age < 4:
                print("Your admission cost is $0.")
        elif age < 18:
                print("Your admission cost is $5.")
        else:
                print("Your admission cost is $10.")
  • 多个elif 代码块
        age = 12
        if age < 4:
                price = 0
        elif age < 18:
                price = 5
        elif age < 65:
                price = 10
        else:
                price = 5
        print("Your admission cost is $" + str(price) + ".")
  • 省略代码块
    Python 并不要求 if-elif 结构后面必须有 else 代码块。在有些情况下, else 代码块很有用;而在其他一些情况下,使用一条 elif 语句来处理特定的情形更清晰
  • 测试多个条件
    if-elif-else 结构功能强大,但仅适合用于只有一个条件满足的情况:遇到通过了的测试后, Python 就跳过余下的测试。这种行为很好,效率很高,让你能够测试一个特定的条件。然而,有时候必须检查你关心的所有条件。在这种情况下,应使用一系列不包含 elif 和 else 代码块的简单 if 语句。在可能有多个条件为 True ,且你需要在每个条件为 True时都采取相应措施时,适合使用这种方法。
     requested_toppings = ['mushrooms', 'extra cheese']
        if 'mushrooms' in requested_toppings:
                    print("Adding mushrooms.")
        if 'pepperoni' in requested_toppings:
                    print("Adding pepperoni.")
        if 'extra cheese' in requested_toppings:
                    print("Adding extra cheese.")
        print("\nFinished making your pizza!")

    5.4 使用if处理列表

  • 检测特殊元素
    如果比萨店的青椒用完了,该如何处理呢?为妥善地处理这种情况,可在 for 循环中包含一条 if 语句:
        requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
        for requested_topping in requested_toppings:
                    if requested_topping == 'green peppers':
                      print("Sorry, we are out of green peppers right now.")
              else:
                      print("Adding " + requested_topping + ".")
        print("\nFinished making your pizza!")
  • 确定列表是否为空
    在 if 语句中将列表名用在条件表达式中时, Python 将在列表至少包含一个元素时返回 True ,并在列表为空时返回 False 。
        requested_toppings = []
        if requested_toppings:
            for requested_topping in requested_toppings:
                    print("Adding " + requested_topping + ".")
                    print("\nFinished making your pizza!")
        else:
            print("Are you sure you want a plain pizza?")
  • 使用多个列表
        ❶ available_toppings = ['mushrooms', 'olives', 'green peppers',
        'pepperoni', 'pineapple', 'extra cheese']
        ❷ requested_toppings = ['mushrooms', 'french fries', 'extra cheese']
        ❸ for requested_topping in requested_toppings:
                ❹ if requested_topping in available_toppings:
                        print("Adding " + requested_topping + ".")
                ❺ else:
                        print("Sorry, we don't have " + requested_topping + ".")
          print("\nFinished making your pizza!")
    在❶处,我们定义了一个列表,其中包含比萨店供应的配料。请注意,如果比萨店供应的配料是固定的,也可使用一个元组来存储它们。
    在❷处,我们又创建了一个列表,其中包含顾客点的配料,请注意那个不同寻常的配料 —— 'french fries' 。
    在❸处,我们遍历顾客点的配料列表。在这个循环中,对于顾客点的每种配料,我们都检查它是否包含在供应的配料列表中(见❹);
    如果答案是肯定的,就将其加入到比萨中,否则将运行 else 代码块(见❺):打印一条消息,告诉顾客不供应这种配料

    5.5 小结

    在本章中,你学习了如何编写结果要么为 Ture 要么为 False 的条件测试。你学习了如何编写简单的 if 语句、 if-else 语句和 if-elif-else 结构。
    在程序中,你使用了这些结构来测试特定的条件,以确定这些条件是否满足。你学习了如何在利用高效的 for 循环的同时,以不同于其他元素的方式对特定的列表元素进行处理。
    你还再次学习了Python 就代码格式方面提出的建议,这可确保即便你编写的程序越来越复杂,其代码依然易于阅读和理解。
    学习字典让你能够模拟更多现实世界的情形。

第六章 字典 bash关联属组(创建 添加键值对 修改 遍历字典) 嵌套(列表-字典 字典-列表 字典-字典) 集合

字典类似于列表,但让你能够将不同的信息关联起来。你将学习如何创建和遍历字典,以及如何将字典同列表和 if 语句结合起来使用。
字典:是一系列 键 — 值对 。每个 键 都与一个值相关联,你可以使用键来访问与之相关联的值。
与键相关联的值可以是数字、字符串、列表乃至字典。事实上,可将任何 Python 对象用作字典中的值。
键 — 值 对是两个相关联的值。指定键时, Python 将返回与之相关联的值。键和值之间用冒号分隔,而键 — 值对之间用逗号分隔。

6.1 创建一个简单的字典 (访问、添加、修改、删除字典中的键值对)

    alien_0 = {'color': 'green', 'points': 5}  # {}内一组由:号分隔的键值对组成字典
    print(alien_0['color'])                    #返回字典 alien_0 中与键 'color' 相关联的值:
    print(alien_0['points'])
    --------------------------------------------------------------------------------------- 
    alien_0['x_position'] = 0                  #添加键 — 值对
    alien_0['y_position'] = 25
    ---------------------------------------------------------------------------------------
    del alien_0['points']                      #删除键值对
    print(alien_0)
    ---------------------------------------------------------------------------------------
    alien_0 = {}                               #一对空的花括号定义一个字典,再分行添加各个键 — 值对。例如,下例演
    alien_0['color'] = 'green'
    alien_0['points'] = 5
    print(alien_0)

    alien_0['color'] = 'yellow'                #修改键的值,(重新定义)
    ---------------------------------------------------------------------------------------
    修改键值实例:
    alien_0 = {'x_position': 0, 'y_position': 25, 'speed': 'medium'}
    print("Original x-position: " + str(alien_0['x_position']))
    #  向右移动外星人
    #  据外星人当前速度决定将其移动多远
    if alien_0['speed'] == 'slow':
            x_increment = 1
    elif alien_0['speed'] == 'medium':
            x_increment = 2
    else:
            #  这个外星人的速度一定很快
            x_increment = 3
            #  新位置等于老位置加上增量
            alien_0['x_position'] = alien_0['x_position'] + x_increment
    print("New x-position: " + str(alien_0['x_position']))

6.2 遍历字典 (遍历键值对、 键、值)

    user_0 = {
        'username': 'efermi',
        'first': 'enrico',
        'last': 'fermi',
        }                              #创建列表
    for key, value in user_0.items():  #使用字典名\ items()方法 、for循环依次将每个键 — 值对存储到指定的两个变量中。
        print("\nKey: " + key)
        print("Value: " + value)

    ---------------------------------------------------------------------
    for name in favorite_languages.keys():   
      #使用 字典名.keys()方法,方法 keys() 并非只能用于遍历;实际上,它返回一个列表,其中包含字典中的所有键
    for name in sorted(favorite_languages.keys()):  
      #按顺序遍历字典中的所有键
    ---------------------------------------------------------------------
    favorite_languages = {
        'jen': 'python',
        'sarah': 'c',
        'edward': 'ruby',
        'phil': 'python',
        }
    print("The following languages have been mentioned:")  
    for language in favorite_languages.values():   #遍历字典中的所有值
    print(language.title())
    ---------------------------------------------------------------------
    for language in set(favorite_languages.values()):   #将值取出创建一个集合
        #如果被调查者很多,最终的列表可能包含大量的重复项。
        #为剔除重复项,可使用集合( set )。 集合 类似于列表,但每个元素都必须是独一无二的

第七章 用户输入和while循环

学习 while 循环以及如何从用户那里获取输入。以及如何将程序变成交互性的 —— 能够对用户输入作出响应。

7.1 函数input()

函数 input() 让程序暂停运行,等待用户输入,获取用户输入后, Python 将其存储在一个变量中,以便使用。
Python 2.7 ,使用函数 raw_input() 来提示用户输入。

    height = input("How tall are you, in inches? ")    #显示提示信息,将用户输入信息保存在变量中
    height = int(height)                               #函数int(), 改为数值类型
    if height >= 36:
    print("\nYou're tall enough to ride!")
    else:
    print("\nYou'll be able to ride when you're a little older.")
    ----------------------------------------------------------------
    多行提示
    prompt = "If you tell us who you are, we can personalize the messages you see."
    prompt += "\nWhat is your first name? "      # +=

    也可使用\n 换行

7.2 while循环

while条件为真时进行循环,注意避免无限循环
break :立即退出 while 循环:
continue :跳至循环体开头进行下一次循环,不执行循环体contine后续代码

```
prompt = "\nTell me something, and I will repeat it back to you:"
    prompt += "\nEnter 'quit' to end the program. "
    message = ""
    while message != 'quit':
          message = input(prompt)
          if message != 'quit':
            print(message)
使用标志:
    prompt = "\nTell me something, and I will repeat it back to you:"
    prompt += "\nEnter 'quit' to end the program. "
    active = True    #####通过改变标志的值来控制是否进行循环
    while active:
        message = input(prompt)
        if message == 'quit':
            active = False
        else:
            print(message)
```

7.3 while处理列表和字典

```
* 循环列表直到没有元素,将弹出的列表附加到其他表
    # 首先,创建一个待验证用户列表
    # 和一个用于存储已验证用户的空列表
    unconfirmed_users = ['alice', 'brian', 'candace']
    confirmed_users = []

    #循环列表直到没有元素,将弹出的列表附加到其他表
    while unconfirmed_users:     
        current_user = unconfirmed_users.pop()
        print("Verifying user: " + current_user.title())
        confirmed_users.append(current_user)
    # 显示所有已验证的用户
    print("\nThe following users have been confirmed:")
    for confirmed_user in confirmed_users:
            print(confirmed_user.title())
    ------------------------------------------------------
* 删除包含特定值的所有列表元素
    pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
    print(pets)
    while 'cat' in pets:              #pet中没有cat时推出循环
        pets.remove('cat')
    print(pets)
    输出结果:
    ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
    ['dog', 'dog', 'goldfish', 'rabbit']
    ------------------------------------------------------
* 使用用户输入来填充字典
    responses = {}

    #设置一个标志,指出调查是否继续
    polling_active = True
    while polling_active:

        #提示输入被调查者的名字和回答
        name = input("\nWhat is your name? ")
        response = input("Which mountain would you like to climb someday? ")

        #将答卷存储在字典中
        responses[name] = response

        #看看是否还有人要参与调查
        repeat = input("Would you like to let another person respond? (yes/ no) ")
        if repeat == 'no':
            polling_active = False

    #调查结束,显示结果
    print("\n--- Poll Results ---")

    for name, response in responses.items():   #
            print(name + " would like to climb " + response + ".")
```

第八章 函数(定义 传递参数的不通方式 返回值 传递列表 将函数存储在模块中)

函数:函数是带名字的代码块,用于完成具体的工作
学习如何编写主要任务是显示信息的函数,还有用于处理数据并返回一个或一组值的函数。最后,你将学习如何
将函数存储在被称为 模块 的独立文件中,让主程序文件的组织更为有序

8.1定义函数

    def greet_user(username):    #定义函数格式,将实参的存储在形参中
    """ 显示简单的问候语 """
    print("Hello, " + username.title() + "!")
    greet_user('jesse')

8.2传递参数(实参形参 关键字实参)

鉴于函数定义中可能包含多个形参,因此函数调用中也可能包含多个实参。向函数传递实参的方式很多,可使用 位置实参 ,这要求实参的顺序与形参的顺序相同;也可使用 关键
字实参 ,其中每个实参都由变量名和值组成;还可使用列表和字典。
调用函数时, Python 必须将函数调用中的每个实参都关联到函数定义中的一个形参。

  • 位置实参需要注意顺序

    def describe_pet(animal_type, pet_name):
    """ 显示宠物的信息 """
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")
    describe_pet('harry', 'hamster')
  • 关键字实参
    注意:使用关键字实参时,务必准确地指定函数定义中的形参名。
            def describe_pet(animal_type, pet_name):
            """ 显示宠物的信息 """
            print("\nI have a " + animal_type + ".")
            print("My " + animal_type + "'s name is " + pet_name.title() + ".")
            describe_pet(animal_type='hamster', pet_name='harry'
  • 指定形参的默认值
    编写函数时,可给每个形参指定 默认值 。在调用函数中给形参提供了实参时, Python 将使用指定的实参值;否则,将使用形参的默认值。因此,给形参指定默认值后,可在函数
    调用中省略相应的实参。使用默认值可简化函数调用,还可清楚地指出函数的典型用法。
            def describe_pet(pet_name, animal_type='dog'):    #定义默认值
            """ 显示宠物的信息 """
            print("\nI have a " + animal_type + ".")
            print("My " + animal_type + "'s name is " + pet_name.title() + ".")
            describe_pet(pet_name='willie')   #调用函数时为默认值时可以省略
  • 一些等效的函数调用的实例
            #一条名为 Willie 的小狗
            describe_pet('willie')
            describe_pet(pet_name='willie')
            #  一只名为 Harry 的仓鼠
            describe_pet('harry', 'hamster')
            describe_pet(pet_name='harry', animal_type='hamster')
            describe_pet(animal_type='hamster', pet_name='harry')

    8.3 返回值

    函数并非总是直接显示输出,相反,它可以处理一些数据,并返回一个或一组值。函数返回的值被称为 返回值 。
    在函数中,可使用 return 语句将值返回到调用函数的代码行。
    返回值让你能够将程序的大部分繁重工作移到函数中去完成,从而简化主程序。

  • 返回处理后的字符串

        def get_formatted_name(first_name, last_name, middle_name=''):
            """ 返回整洁的姓名 """               #“”“函数的注释信息,介绍实现的功能”“”
            if middle_name:
                full_name = first_name + ' ' + middle_name + ' ' + last_name
            else:
                full_name = first_name + ' ' + last_name
            return full_name.title()             #return返回给调用函数的行
    
        musician = get_formatted_name('jimi', 'hendrix')
        print(musician)
    
        musician = get_formatted_name('john', 'hooker', 'lee')
        print(musician)
  • 返回字典 函数可返回任何类型的值,包括列表和字典等较复杂的数据结构。

        def build_person(first_name, last_name, age=''):    #包含一个可选的形参
            """ 返回一个字典,其中包含有关一个人的信息 """
            person = {'first': first_name, 'last': last_name}
            if age:      #如果age有值,将键值对加入到字典中
                person['age'] = age
            return person
    
        musician = build_person('jimi', 'hendrix', age=27)
        print(musician)
  • 函数与循环结合使用

        def get_formatted_name(first_name, last_name):
            """ 返回整洁的姓名 """
            full_name = first_name + ' ' + last_name
            return full_name.title()
        while True:
            print("\nPlease tell me your name:")
            print("(enter 'q' at any time to quit)")
            f_name = input("First name: ")                  #定义推出条件
            if f_name == 'q':
            break
            l_name = input("Last name: ")
            if l_name == 'q':
            break
            formatted_name = get_formatted_name(f_name, l_name)
            print("\nHello, " + formatted_name + "!")

    8.4 传递列表(在函数中修改列表 以及禁止函数修改原始列表)

    向函数传递列表很有用,这种列表包含的可能是名字、数字或更复杂的对象(如字典)。将列表传递给函数后,函数就能直接访问其内容。下面使用函数来提高
    处理列表的效率。

    --------------------------------------------------------------
    #向列表中每个用户问候
    def greet_users(names):
        """ 向列表中的每位用户都发出简单的问候 """
        for name in names:
        msg = "Hello, " + name.title() + "!"
        print(msg)
    usernames = ['hannah', 'ty', 'margot']
    greet_users(usernames)
    --------------------------------------------------------------
    #该脚本定义两个列表将其传递给函数
    def print_models(unprinted_designs, completed_models):
        """
        模拟打印每个设计,直到没有未打印的设计为止
        打印每个设计后,都将其移到列表 completed_models 中
        """
        while unprinted_designs:
        current_design = unprinted_designs.pop()
        #  模拟根据设计制作 3D 打印模型的过程
        print("Printing model: " + current_design)
        completed_models.append(current_design)
    def show_completed_models(completed_models):
        """ 显示打印好的所有模型 """
        print("\nThe following models have been printed:")
        for completed_model in completed_models:
        print(completed_model)
    
    unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
    completed_models = []
    
    print_models(unprinted_designs, completed_models)
    show_completed_models(completed_models)
    --------------------------------------------------------------
    #要将列表的副本传递给函数,可以像下面这样做:
    function_name(list_name[:])
    print_models(unprinted_designs[:], completed_models)

    8.5 传递任意数量的实参

  • 传递任意数量的实参
    形参名 中的星号让 Python 创建一个空元组,并将收到的所有值都封装到这个元组中。
    例如:def make_pizza(
    toppings):
  • 结合使用位置实参和任意数量实参
    如果要让函数接受不同类型的实参,必须在函数定义中将接纳任意数量实参的形参放在最后。
    def make_pizza(size, *toppings):
    调用函数:
    make_pizza(16, 'pepperoni')
    make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

  • 使用任意数量的关键字实参
    有时候,需要接受任意数量的实参,但预先不知道传递给函数的会是什么样的信息。
    在这种情况下,可将函数编写成能够接受任意数量的键 — 值对 —— 调用语句提供了多少就接受多少
    例如一个这样的示例是创建用户简介:你知道你将收到有关用户的信息,但不确定会是什么样的信息。
    注意格式:形参 **user_info 中的两个星号让 Python 创建一个名为 user_info 的空字典,并将收到的所有名称 — 值对都封装到这个字典中。

    在这个函数中,可以像访问其他字典那样访问 user_info 中的名称 — 值对。
    在 build_profile() 的函数体内,我们创建了一个名为 profile 的空字典,用于存储用户简介。
    在❶处,我们将名和姓加入到这个字典中,因为我们总是会从用户那里收到这两项信息。
    在❷处,我们遍历字典 user_info 中的键 — 值对,并将每个键 — 值对都加入到字典 profile 中。
    最后,我们将字典 profile 返回给函数调用行。

    示例代码:
    
        def build_profile(first, last, **user_info):
            """ 创建一个字典,其中包含我们知道的有关用户的一切 """
            profile = {}
            ❶ profile['first_name'] = first
            profile['last_name'] = last
            ❷ for key, value in user_info.items():
            profile[key] = value
            return profile
        user_profile = build_profile('albert', 'einstein',
                                    location='princeton',
                                    field='physics')
        print(user_profile)

    8.6 将函数存储在模块中

    函数的优点之一是,使用它们可将代码块与主程序分离。通过给函数指定描述性名称,可让主程序容易理解得多。还可以更进一步,将函数存储在被称为 模块 的独立文件中,再将模块 导入 到主程序中。 import 语句允许在当前运行的程序文件中使用模块中的代码。

  • 导入整个模块
    import module_name
    要让函数是可导入的,得先创建模块。 模块 是扩展名为 .py 的文件,包含要导入到程序中的代码。需要在主程序文件使用import导入。
    示例:

        pizza.py
    
                def make_pizza(size, *toppings):
                    """ 概述要制作的比萨 """
                    print("\nMaking a " + str(size) +
                    "-inch pizza with the following toppings:")
                    for topping in toppings:
                    print("- " + topping)
        接下来,我们在 pizza.py 所在的目录中创建另一个名为 making_pizzas.py 的文件,这个文件导入刚创建的模块,再调用 make_pizza() 两次:
    
        making_pizzas.py
                import pizza
                pizza.make_pizza(16, 'pepperoni')   #主程序调用模块中的函数
                pizza.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
    
        Python 读取这个文件时,代码行 import pizza 让 Python 打开文件 pizza.py并将其中的所有函数都复制到这个程序中。
        你看不到复制的代码,因为这个程序运行时, Python 在幕后复制这些代码。
        你只需知道,在 making_pizzas.py 中,可以使用 pizza.py 中定义的所有函数。
        要调用被导入的模块中的函数,可指定导入的模块的名称 pizza 和函数名 make_pizza() ,并用句点分隔它们。    
  • 导入特定的函数
    from module_name import function_namefrom
  • 导入任意数量的函数
    from module_name import function_0, function_1, function_2
  • 使用 as 给函数指定别名
    如果要导入的函数的名称可能与程序中现有的名称冲突,或者函数的名称太长,可指定简短而独一无二的 别名 —— 函数的另一个名称,类似于外号。
    指定别名的通用语法如下:
    from module_name import function_name as fn #使用 as 给函数指定别名
  • 使用 as 给模块指定别名
    import module_name as mn
  • 导入模块中的所有函数
    使用星号( * )运算符可让 Python 导入模块中的所有函数:

        from pizza import *          ####
        make_pizza(16, 'pepperoni')
        make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
    
        注意:############ import 语句中的星号让 Python 将模块 pizza 中的每个函数都复制到这个程序文件中。由于导入了每个函数,可通过名称来调用每个函数,而无需使用句点表示法。
        然而,使用并非自己编写的大型模块时,最好不要采用这种导入方法:如果模块中有函数的名称与你的项目中使用的名称相同,
        可能导致意想不到的结果: Python 可能遇到多个名称相同的函数或变量,进而覆盖函数,而不是分别导入所有的函数。
        最佳的做法是,要么只导入你需要使用的函数,要么导入整个模块并使用句点表示法。这能让代码更清晰,更容易阅读和理解。
    

注意:
给形参指定默认值时,等号两边不要有空格:
def function_name(parameter_0, parameter_1='default value')
对于函数调用中的关键字实参,也应遵循这种约定:
function_name(value_0, parameter_1='value')
所有的 import 语句都应放在文件开头,唯一例外的情形是,在文件开头使用了注释来描述整个程序。

第九章 类(创建使用 实例化 继承 导入类 python标准库)

可将类视为有关如何创建实例的说明。 让 Python 知道如何创建表示特定的实例。

类:类是面向对象程序设计中的概念,是面向对象编程的基础。抽象概念。
方法:类中函数成为方法。只是调用方法不一样。
属性:可通过实例访问的变量称为 属性
接口:类和外界发生交互的操作称为接口。

你将学习编写类。类将函数和数据整洁地封装起来,让你能够灵活而高效地使用它们。
在面向对象编程中,你编写表示现实世界中的事物和情景的类,并基于这些类来创建对象。编写类时,你定义一大类对象都有的通用行为。
基于类创建 对象 时,每个对象都自动具备这种通用行为,然后可根据需要赋予每个对象独特的个性。

百度百科:
类的实质是一种数据类型,类似于int、char等基本类型,不同的是它是一种复杂的数据类型。因为它的本质是类型,而不是数据,所以不存在于内存中,不能被直接操作,只有被实例化为对象时,才会变得可操作。

类是对现实生活中一类具有共同特征的事物的抽象。如果一个程序里提供的类型与应用中的概念有直接的对应,这个程序就会更容易理解,也更容易修改。一组经过很好选择的用户定义的类会使程序更简洁。此外,它还能使各种形式的代码分析更容易进行。特别地,它还会使编译器有可能检查对象的非法使用。

类的内部封装了方法,用于操作自身的成员。类是对某种对象的定义,具有行为(be-havior),它描述一个对象能够做什么以及做的方法(method),它们是可以对这个对象进行操作的程序和过程。它包含有关对象行为方式的信息,包括它的名称、方法、属性和事件。

类的构成包括数据成员和成员函数。数据成员对应类的属性,类的数据成员也是一种数据类型,并不需要分配内存。成员函数则用于操作类的各项属性,是一个类具有的特有的操作,比如“学生”可以“上课”,而“水果”则不能。

9.1 创建类根据类创建实例

根据该类创建的实例都存储名字和年龄,并有两种方法。

    [root@localhost ~/test/class]# vim dog.py 
    # -*- incoding=UTF-8 -*-
    class Dog():
            """ 一次模拟小狗的简单尝试 """

            def __init__(self, name, age):
                    """ 初始化属性 name 和 age""" 
                    self.name = name     #获取存储在形参 name 中的值,并将其存储到变量 name 中,然后该变量被关联到当前创建的实例
                    self.age = age

            def prt(self):
                    print(self)                           #一个指向实例的引用
                    print(self.__class__)                 #

            def sit(self):
                    """ 模拟小狗被命令时蹲下 """
                    print(self.name.title() + " is now sitting.")

            def roll_over(self):
                    """ 模拟小狗被命令时打滚 """
                    print(self.name.title() + " rolled over!")
            #类的实例化
            my_dog = Dog('willie', 6)
            you_dog = Dog('willie', 6)

            print("My dog's name is " + my_dog.name.title() + ".")
            print("My dog is " + str(my_dog.age) + " years old.")

            print(my_dog.name)
            print(you_dog.name)
            print(my_dog.age)
            print(you_dog.age)

            my_dog.sit()
            my_dog.roll_over()

            my_dog.prt()

            输出###############################输出
            My dog's name is Willie.
            My dog is 6 years old.
            willie
            willie
            6
            6
            Willie is now sitting.
            Willie rolled over!
            <__main__.Dog object at 0x7fcc2b716828>      #print(self)   
            <class '__main__.Dog'>                       #print(self.__class__) 

解释:
1.init() 是一个特殊的方法,每当你根据 Dog 类创建新实例时, Python 都会自动运行它。
在这个方法的名称中,开头和末尾各有两个下划线,这是一种约定,旨在避免 Python 默认方法与普通方法发生名称冲突。

2.Python 调用这个 init() 方法来创建 Dog 实例时,将自动传入实参 self 。每个与类相关联的方法调用都自动传递实参 self ,它是一个指向实例本身
的引用,让实例能够访问类中的属性和方法。

3.形参 self 必不可少,还必须位于其他形参的前面,只需给最后两个形参( name 和 age )提供值。
❹处定义的两个变量都有前缀 self 。以 self 为前缀的变量都可供类中的所有方法使用,我们还可以通过类的任何实例来访问这些变量。 self.name = name 获取存储在形
参 name 中的值,并将其存储到变量 name 中,然后该变量被关联到当前创建的实例。

4.在 Python 2.7 中创建类时,需要做细微的修改 —— 在括号内包含单词 object :
class ClassName(object):
5.访问属性 实例名.属性名
my_dog.name
6.调用方法 实例名.方法名
my_dog.sit()
my_dog.roll_over()

9.2 使用类和实例

* 给属性指定默认的值

* 修改属性的值
1.直接通过实例进行修改: my_new_car.odometer_reading = 23
2.通过方法进行设置:
def update_odometer(self, mileage):
    """ 将里程表读数设置为指定的值 """
    self.odometer_reading = mileage
my_new_car.update_odometer(23)   #根据事先定义好的方法修改属性的值
3.通过方法进行递增(增加特定的值)。
❶ def increment_odometer(self, miles):
        """ 将里程表读数增加指定的量 """
        self.odometer_reading += miles

9.3 继承

编写类时,并非总是要从空白开始。如果你要编写的类是另一个现成类的特殊版本,可使用 继承 。
一个类 继承 另一个类时,它将自动获得另一个类的所有属性和方法
原有的类称为 父类 ,而新类称为 子类 。子类继承了其父类的所有属性和方法,同时还可以定义自己的属性和方法。

  • 字类继承的方法init()

        ❶ class Car():
            """ 一次模拟汽车的简单尝试 """
            def __init__(self, make, model, year):
                self.make = make
                self.model = model
                self.year = year
                self.odometer_reading = 0
            def get_descriptive_name(self):
                long_name = str(self.year) + ' ' + self.make + ' ' + self.model
                return long_name.title()
            def read_odometer(self):
                print("This car has " + str(self.odometer_reading) + " miles on it.")
            def update_odometer(self, mileage):
                if mileage >= self.odometer_reading:
                    self.odometer_reading = mileage
                else:
                    print("You can't roll back an odometer!")
            def increment_odometer(self, miles):
                self.odometer_reading += miles
    
        ❷ class ElectricCar(Car):
            """ 电动汽车的独特之处 """
            def __init__(self, make, model, year):
                """ 初始化父类的属性 """
                super().__init__(make, model, year)
        my_tesla = ElectricCar('tesla', 'model s', 2016)
        print(my_tesla.get_descriptive_name())
    
        创建子类时,父类必须包含在当前文件中,且位于子类前面。
        在❷处,我们定义了子类 ElectricCar 。定义子类时,必须在括号内指定父类的名称。
        方法 __init__() 接受创建 Car 实例所需的信息(见❸)。
        ❹处的 super() 是一个特殊函数,帮助 Python 将父类和子类关联起来。
        这行代码让 Python 调用 ElectricCar 的父类的方法 __init__() ,让 ElectricCar 实例包含父类的所有属性。
        父类也称为 超类 ( superclass ),名称 super 因此而得名。
            ------------------------------------------------------------
            Python 2.7 中的继承  与 python3 的不同
            在 Python 2.7 中,继承语法稍有不同, ElectricCar 类的定义类似于下面这样:
            class Car(object):
                def __init__(self, make, model, year):
                    --snip--
            class ElectricCar(Car):
                def __init__(self, make, model, year):
                    super(ElectricCar, self).__init__(make, model, year)      #super(ElectricCar, self) 与 super() 
            ------------------------------------------------------------
    
             class Car():
                --snip--
             class ElectricCar(Car):
                """ 电动汽车的独特之处 """
                def __init__(self, make, model, year):
                    """ 初始化父类的属性 """
                    super().__init__(make, model, year)
            --snip--
            ------------------------------------------------------------
  • 给字类定义属性和方法
    self.odometer_reading = 0 #设定属性的初始值后,可不必在创建类时给定形参
  • 重写父类的方法
    对于父类的方法,只要它不符合子类模拟的实物的行为,都可对其进行重写。
    为此,可在子类中定义一个这样的方法,即它与要重写的父类方法 同名 。这样, Python 将不会考虑这个父类方法,而只关注你在子类中定义的相应方法。
  • 将实例用作属性

    class Car():
        --snip--
    ❶ class Battery():
        """ 一次模拟电动汽车电瓶的简单尝试 """
        ❷ def __init__(self, battery_size=70):
            """ 初始化电瓶的属性 """
            self.battery_size = battery_size
        ❸ def describe_battery(self):
            """ 打印一条描述电瓶容量的消息 """
            print("This car has a " + str(self.battery_size) + "-kWh battery.")
    class ElectricCar(Car):
        """ 电动汽车的独特之处 """
        def __init__(self, make, model, year):
            """
            初始化父类的属性,再初始化电动汽车特有的属性
            """
        super().__init__(make, model, year)
        ❹ self.battery = Battery()                    #创建一个Battery实例保存在
    
    my_tesla = ElectricCar('tesla', 'model s', 2016)
    
    print(my_tesla.get_descriptive_name())
    my_tesla.battery.describe_battery()
    
    ❹ self.battery = Battery() 
    #这行代码让 Python 创建一个新的 Battery 实例(由于没有指定尺寸,因此为默认值 70 ),
    并将该实例存储在属性 self.battery 中。每当方法 __init__() 被调用时,都将执行该操作;
    因此现在每个 ElectricCar 实例都包含一个自动创建的 Battery 实例。

9.4 导入类

* 导入单个类
    from car import Car
* 从一个模块中存储多个类
    可根据需要在一个模块中存储任意数量的类。
* 从一个模块中导入多个类
    from car import Car, ElectricCar
    调用方式
      my_beetle = Car('volkswagen', 'beetle', 2016)
* 导入整个模块
    导入了整个 car 模块。接下来,我们使用语法 module_name.class_name  访问需要的类。
    import car
    my_beetle = car.Car('volkswagen', 'beetle', 2016)   
    print(my_beetle.get_descriptive_name())
* 导入模块中的所有类
    from module_name import *
    不推荐使用这种导入方式,其原因有二。
    首先,如果只要看一下文件开头的 import 语句,就能清楚地知道程序使用了哪些类,将大有裨益;
    但这种导入方式没有明确地指出你使用了模块中的哪些类。这种导入方式还可能引发名称方面的困惑。
    如果你不小心导入了一个与程序文件中其他东西同名的类,将引发难以诊断的错误。
    这里之所以介绍这种导入方式,是因为虽然不推荐使用这种方式,但你可能会在别人编写的代码中见到它。
    需要从一个模块中导入很多类时,最好导入整个模块,并使用 module_name.class_name  语法来访问类。
* 在一个模块中调用另一个模块
* 自定义工作流程

9.5 Python标准库

Python 标准库 是一组模块,安装的 Python 都包含它。你现在对类的工作原理已有大致的了解,可以开始使用其他程序员编写好的模块了。可使用标准库中的任何函数和类,为此只需在程序开头包含一条简单的 import 语句。

9.6 类编码风格

类名应采用 驼峰命名法 ,即将类名中的每个单词的首字母都大写,而不使用下划线。
实例名和模块名都采用小写格式,并在单词之间加上下划线。
对于每个类,都应紧跟在类定义后面包含一个文档字符串。这种文档字符串简要地描述类的功能,并遵循编写函数的文档字符串时采用的格式约定。每个模块也都应包含一个文档字符串,对其中的类可用于做什么进行描述。
可使用空行来组织代码,但不要滥用。在类中,可使用一个空行来分隔方法;而在模块中,可使用两个空行来分隔类。

需要同时导入标准库中的模块和你编写的模块时,先编写导入标准库模块的 import 语句,再添加一个空行,然后编写导入你自己编写的模块的 import 语句。在包含多
条 import 语句的程序中,这种做法让人更容易明白程序使用的各个模块都来自何方。

第十章 文件和异常

第十一章 测试代码

猜你喜欢

转载自blog.51cto.com/mengyao/2129542