Python入门实战:Python的编程规范

26 阅读9分钟

1.背景介绍

为什么要写这篇文章呢?回答这个问题之前,先来说说我为什么对编程有兴趣、编程有热情。其实很简单,因为作为一个计算机从业者,我们都需要经历磨砺,在工作中学习,在学习中进步。而编程也是一样,它能给我们带来意想不到的收获和快乐。当然,编程中也存在很多坎坷,但如果我们能够保持初心,坚持走下去,并从中得到成长,那么无论遇到什么样的问题,我们总会找到解决办法的。所以,写这样的一篇文章,我想传递的是一种思维方式和工作态度。即通过分享一些经验、教训,帮助读者快速上手Python,理解编程思想,提升自身能力。希望通过本文,能够帮助更多的人快速上手编程、掌握Python语言。 接下来,让我们来聊一下“Python的编程规范”到底是个什么东西。“Python的编程规范”这几个字听起来很吓人,其实它包含的内容真的很多,但本文着重讨论其中的编程风格、命名习惯等相关内容,让读者可以快速上手,提高编码效率。这些内容是学完这篇文章后,读者会有以下这些收获: 1.了解Python编程语言的基本语法规则; 2.能够编写符合Python编程风格的代码; 3.能够更好地组织项目,解决编码问题; 4.了解如何有效地利用Python的内置模块; 5.对于面试官或者其他同事提供更加规范的代码和文档; 好了,关于“Python的编程规范”的前言就到这里啦!下面开始正文。

2.核心概念与联系

“Python的编程规范”这篇文章最核心的部分就是将代码按照一定规则进行编排、命名,并充分利用Python的特性实现功能。 首先,介绍两个Python的基本概念。

1.模块(Module):模块指的是python文件,用来存放Python代码的集合。它提供了一个范围限制,避免不同名称之间的冲突。比如,我们可以在一个文件中定义多个函数,然后在另一个文件中调用。

2.包(Package):包(Package)是模块的容器,里面可以包含多个模块。它提供了一个管理层次结构的方法,方便开发者按照特定逻辑组织代码。

了解了这两个概念之后,再来谈谈如何编排代码以及命名。

  1. PEP 8 -- Style Guide for Python Code: www.python.org/dev/peps/pe….

    PEP 8 是一份为 Python 代码制定的风格指南,主要涵盖了如下方面:

    • Naming conventions

      • Functions and variables names are written in lowercase with words separated by underscores as necessary to improve readability. For example::

        def my_function(): pass

      • Classes use the CapWords convention. The first word is always capitalized and all other words start with a lower case letter. For example::

        class MyClass: pass

    • Indentation: All code blocks are indented at least four spaces. This helps ensure that code blocks are well-nested within each other and also helps prevent issues with mixing tabs and spaces in indentation.

    • Line length: Each line of code should be limited to a maximum of 79 characters. Longer lines can be broken into logical chunks which improves code legibility.

    • Whitespace: Avoid extraneous whitespace anywhere except inside parentheses or brackets. Use single quotes instead of double quotes where possible to avoid unnecessary escaping.

    • Imports: Always put imports on separate lines::

      Yes: import os import sys No: import sys,os

    • Comments: Use comments to explain difficult sections of code or remind yourself why you did something later.

  2. 命名规范:

    • 函数名采用小写+下划线的格式,如:my_func()
    • 模块名采用小写+下划线的格式,如:my_module.py
    • 类名采用驼峰命名法,首字母大写,每个单词的首字母均大写,如:MyClass
    • 变量名采用小写+下划线的格式,如:my_variable
    • 配置项名采用全小写的形式,用下划线连接单词。如:item_name

3.核心算法原理和具体操作步骤以及数学模型公式详细讲解

让我们继续讨论Python的编程规范。

3.1 字符串拼接

Python提供了多种方式进行字符串的拼接,常见的有3种方法:

    • 运算符:将两个或多个字符串拼接到一起。例如:
     name = "John"
     age = 30
    
     # Using '+' operator
     message = "Hello, my name is "+name+" and I am "+str(age)+" years old."
     print(message)   # Output: Hello, my name is John and I am 30 years old.
    
  1. % 操作符:%s 表示插入字符串参数,%d表示插入整数参数。

     name = "John"
     age = 30
     
     # Using '%' operator
     message = "Hello, my name is %s and I am %d years old." %(name, age)
     print(message)   # Output: Hello, my name is John and I am 30 years old.
    
  2. format() 方法:format() 方法用于将占位符 {} 和值对应起来,使得字符串更加灵活易于维护。

     name = "John"
     age = 30
    
     # Using 'format()' method
     message = "Hello, my name is {} and I am {} years old.".format(name, age)
     print(message)   # Output: Hello, my name is John and I am 30 years old.
    

    当然还有其他的方法也可以完成相同的工作,不过使用以上三种方法进行字符串的拼接足以应付一般需求。

3.2 文件读取及写入

Python 提供了 open() 函数打开文件,读取或写入文件内容,包括文本文件和二进制文件。

文本文件的读写

with open('test.txt', 'r') as file:   # 以只读的方式打开文件
    data = file.read()                # 从文件中读取数据
print(data)                           # 打印读取的数据

with open('output.txt', 'w') as file:  # 以可写的方式打开文件
    file.write("This is a test.")    # 将数据写入文件
    

注意:open() 函数中的 mode 参数可取值:

  • 'r':以只读模式打开文件,文件指针将会放在文件的开头。这是默认模式。
  • 'w':以写入模式打开文件,如果该文件已存在则覆盖文件,创建新文件。
  • 'x':以新建模式打开文件,如果该文件已存在则将无法打开文件。
  • 'a':以追加模式打开文件,如果该文件已存在,文件指针将会放在文件的结尾。
  • 't':文本模式(默认)。
  • 'b':二进制模式。

当以只读模式打开文件时,文件指针将会放在文件的开头,此时调用 read() 方法即可读取文件的内容。

当以可写模式打开文件时,如果该文件不存在,会创建一个新的文件;若该文件存在,则覆盖原有的文件。写入文件的内容可以使用 write() 方法。

二进制文件的读写

with open('test.bin', 'rb') as file:  # 以二进制读模式打开文件
    data = file.read()                 # 从文件中读取数据
    
with open('output.bin', 'wb') as file:  # 以二进制写模式打开文件
    file.write(b'\xff\xd8\xff')        # 将数据写入文件

以二进制模式打开文件时,只能读取二进制格式的文档。当以 'rb' 打开文件时,文件指针指向文件的开头,调用 read() 方法即可读取文件内容。当以 'wb' 打开文件时,会创建一个新的文件,或覆盖原有的文件,并将内容写入其中。

注意:务必使用正确的读写模式。否则,可能会导致文件无法正常读写,造成数据丢失或损坏。

3.3 字典排序

字典类型的值存储的是键-值对的集合。字典的 key 必须是不可变对象(哈希值),因此不能修改,如果 key 不是不可变对象,则可以通过 list() 或 tuple() 方法转化为可变对象。

字典排序可以根据 value 的大小或者 key 的大小进行排序。

dict = {'banana': 3, 'apple': 4, 'orange': 1}

sorted_by_value = sorted(dict.items(), key=lambda x: x[1])     # 根据 value 大小排序
sorted_by_key = sorted(dict.items())                        # 根据 key 大小排序

for item in sorted_by_value:
    print(item)                                               # [(u'orange', 1), (u'banana', 3), (u'apple', 4)]

for item in sorted_by_key:
    print(item)                                               # [(u'apple', 4), (u'banana', 3), (u'orange', 1)]

注意:sorted() 函数返回的是一个列表。

3.4 生成器表达式

生成器表达式是 Python 中的一个非常有用的工具,它的语法类似列表推导式,但是它使用圆括号 () 来代表生成器。生成器表达式允许用户创建生成器,而不是完整的列表,节省内存。

squares = (i**2 for i in range(10))
print(type(squares))             # <class 'generator'>

for num in squares:
    print(num)                   # Output: 0, 1, 4, 9,..., 81


sum_of_squares = sum((i**2 for i in range(10)))
print(sum_of_squares)            # Output: 285

注意:生成器表达式会在每次循环的时候才生成数据。

3.5 Lambda 表达式

Lambda 表达式是一种匿名函数,只能有一个表达式,并且只能有限定作用域,不能访问外部变量。

add = lambda x, y: x + y           # add(x,y): 返回 x + y

result = add(2, 3)                  # result = 5

multiply = lambda x, y, z : x * y * z

result = multiply(2, 3, 4)          # result = 24

注意:lambda 表达式有限定作用域,只能访问自己定义的变量。

4.具体代码实例和详细解释说明

4.1 Fibonacci 数列生成

斐波那契数列是数学家列弛蒂洛·斐波那契(Leonardo Fibonacci)在著名的《连城诗》一书中描述的一个寓言故事,也是影响广泛的科幻作品。

斐波那契数列由 0 和 1 开始,每一步,将前面的两数相加,形成新的数列的数字。即,

fib(n) = fib(n-1) + fib(n-2) if n > 1
          1                    otherwise

编写一个 Python 程序,根据指定的数值 n,生成第 n 个斐波那契数:

def fib(n):
    """
    Generate nth number in Fibonacci sequence.
    """
    if n <= 0:
        return None
    elif n == 1:
        return 0
    elif n == 2:
        return 1
    else:
        return fib(n-1) + fib(n-2)
        
print(fib(5))       # Output: 5

4.2 数据转换

编写一个 Python 程序,把数字转换为二进制、八进制、十六进制:

def convert_to_binary(number):
    """
    Convert decimal number to binary string.
    """
    bin_string = ""
    while number!= 0:
        remainder = number % 2
        bin_string = str(remainder) + bin_string
        number //= 2
        
    return bin_string

def convert_to_octal(number):
    """
    Convert decimal number to octal string.
    """
    oct_string = ""
    base = 8
    digits = "01234567"
    
    if number < 0:
        negative = True
        number *= -1
    else:
        negative = False
        
    while number!= 0:
        digit = int(number / base)
        oct_string = digits[digit] + oct_string
        number -= digit*base
        
    if negative:
        oct_string = "-" + oct_string
        
    return oct_string

def convert_to_hexadecimal(number):
    """
    Convert decimal number to hexadecimal string.
    """
    hex_string = ""
    letters = "0123456789ABCDEF"
    
    if number < 0:
        negative = True
        number *= -1
    else:
        negative = False
        
    while number!= 0:
        digit = int(number % 16)
        hex_string = letters[digit] + hex_string
        number //= 16
        
    if negative:
        hex_string = "-" + hex_string
        
    return hex_string

# Test cases
assert convert_to_binary(10) == "1010", "Test failed!"
assert convert_to_octal(10) == "12", "Test failed!"
assert convert_to_hexadecimal(10) == "A", "Test failed!"
assert convert_to_binary(-10) == "-1010", "Test failed!"
assert convert_to_octal(-10) == "-12", "Test failed!"
assert convert_to_hexadecimal(-10) == "-A", "Test failed!"

4.3 列表的平方

编写一个 Python 程序,计算列表中元素的平方:

lst = [1, 2, 3, 4, 5]

squared_list = []
for num in lst:
    squared_list.append(num ** 2)

print(squared_list)      # Output: [1, 4, 9, 16, 25]

4.4 浮点型的精度控制

浮点型的精度控制有两种方案:一是将 float 转换为 Decimal ,二是使用 round() 函数。

使用 Decimal 类型

from decimal import Decimal

pi = Decimal("3.1415926535")
e = Decimal("2.7182818285")

print(float(pi))         # Output: 3.1415926535
print(round(float(pi), 2))  # Output: 3.14

注意:Decimal 类型的精度比普通浮点型的精度要高。

使用 round() 函数

pi = 3.1415926535
e = 2.7182818285

rounded_pi = round(pi, 2)
rounded_e = round(e, 2)

print(rounded_pi)        # Output: 3.14
print(rounded_e)         # Output: 2.72

注意:round() 函数的第二个参数指定保留几位小数。