Python从小白到大牛

第1 章 开篇综述

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 之祥by Tim Peters
优美胜于丑陋
明了胜于晦涩
简洁胜于复杂
复杂胜于凌乱
扁平胜于嵌套
宽松胜于紧凑
可读性很重要
即使是特例,也不可违背这些规则
不要捕获所有错误,除非你确定需要这样做
如果存在多种可能,不要猜测
通常只有唯一一种是最佳的解决方案
虽然这并不容易,因为你不是Python 之父
做比不做要好,但不假思索就动手还不如不做
如采你的方案很难懂,那肯定不是一个好方案,反之亦然
命名空间非常有用,应当多加利用

Python 是动态类型语言,它不会检查数据类型,在变量声明时不需要指定数据类型。

Python 官方常用网址:

PEP 是Python Enhancement Proposals 的缩写。PEP 是一种提案,用于为Python 社区提供各种增强功能的技术规格说明书,提交新特性, 以便让社区指出问题、改进技术文档。

第2 章 搭建开发环境

推荐使用miniconda, IDE用spyder

第3 章 第一个Python程序

Python 源代码中不需要有 main 主函数。

第4 章 Python 语法基础

Python 是动态类型语言,在变量声明时不需指定数据类型。同一变量指向的数据类型随时都可以改变.

>>> a = 1
>>> type(a)
<class 'int'>
>>> a='h'
>>> type(a)
<class 'str'>
>>> a=[1,2,3]
>>> type(a)
<class 'list'>

常量

python没有关键字定义常量,python程序一般通过约定俗成的变量名全大写的形式表示这是一个常量.

实际上可以用元组定义常量, 只是引用需要下标, 比较丑啊.

>>> t_PI = (3.14159265,)
>>> t_PI[0]
3.14159265
>>> t_PI[0]=3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

代码如能清晰的表达意图, 不用添加注释. 当觉得被迫添加注释的时候,不妨考虑一下重写代码使其更清晰。

模块与包

  • 模块是一个文件, 模块事实上提供一种命名空间( namespace)
  • 包是一个文件夹加上一个空的__init__.py 文件.如果有两个相同名字的模块,应如何防止命名冲突呢?那就是使用包( package ),包也提供了一种命名空间。

目录结构

$ tree -L 3
.
├── com
│   ├── pkg_1
│   │   ├── __init__
│   │   ├── __pycache__
│   │   └── hello.py
│   └── pkg_2
│       ├── __init__.py
│       ├── __pycache__
│       └── hello.py
└── temp.py

可见, 只需要在模块级别有 __init__ 就可以了

temp.py

import com.pkg_1.hello
import com.pkg_2.hello

print(com.pkg_1.hello.s)
print(com.pkg_2.hello.s)

com/pkg_1/hello.py

s='s: com/pkg_1/hello.py'

com/pkg_2/hello.py

s='s: com/pkg_2/hello.py'

运行结果:

$ python3 temp.py
s: com/pkg_1/hello.py
s: com/pkg_2/hello.py

第5 章 Python 编码规范

命名规范

本书的Python 编码规范借鉴了Python 官方的PEP8 编码规范谷歌Python 编码规范

  • 包名: 全部小写字母,中间可以由的隔开,不推荐使用下画线。作为命名空间,包名应该具有唯一性,推荐采用公司或组织域名的倒置,如com.apple.quicktime.v2 。
  • 模块名: 全部小写字母,如果是多个单词构成, 可以用下画线隔开, 如dummy_threading 。
  • 类名: 采用大驼峰法命名,如SplitViewController 。
  • 异常名:异常属于类, 命名同类命名,但应该使用Error 作为后缀。如FileNotFoundError 。
  • 变量名: 全部小写字母,如果由多个单词构成,可以用下画线隔开。
    • 如果变量应用于模块或函数内部,则变量名可以由单下画线开头: 变量类内部私有使用变量名可以双下画

线开头。
– 不要命名双下画线开头和结尾的变量,这是Python 保留的。
– 另外,避免使用小写L 、大写字母 O 和大写字母 I 作为变量名。

  • 函数名和方法名: 命名同变量命名,如balance_account 、push_cm exit 。
  • 常量名: 全部大写字母,如果是由多个单词构成,可以用下画线隔开,如YEAR 和WEEK_OF_MONTH 。

大驼峰法命名是驼峰命名的一种,驼峰命名是指混合使用大小写字母来命名。驼峰命名分为小驼峰法和大驼峰法。小驼峰法就是第一个单词全部小写,后面的单词首字母大写,如m yRoomCount ; 大驼峰法是第一个单词的首字母也大写,如ClassRoom.

注释规范

正文完
 
评论(没有评论)