Python语言知识总结

1. 环境

1.1 Anaconda

抛弃python原生安装方式吧,使用Anaconda才是最省心的。

1.2 Miniconda

Anaconda 太大了,Miniconda才是王道!下载链接:https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/

2.语法

2.1 文件开头前两行

# !/usr/bin/python
# -*- coding: utf-8 -*-

2.2 函数注解(Parameter annotations)

这是python有一种基于语言层面的函数注释方法

def foo()->'这是函数的注解':
    pass

2.3 访问控制

python默认都是public的访问权限。如果需要private的权限,只需在方法属性的前面加上两个下划线。

class Foo:
    def func_public(self)->'公有方法':
        pass
    def __func_private(self)->'私有方法':
        pass
    #公有属性
    attribute_public = 10
    #私有属性
    __attribute_private = 20

2.4 字符串拼接数字

利用 str() 函数

s = 'a' + str(1)  # s = 'a1'

2.5 多维数组

1.利用NumPy生成数值型多维数组

numpy提供了一种优雅的(一行代码)方式:numpy.zeros()

import numpy as np
table = np.zeros((2, 3), dtype=np.float)

生成的数组如下

[[ 0.  0.  0.]
 [ 0.  0.  0.]]

2. List Comprehensions

生成一个 3 * 4 的矩阵,内中元素都为 0.

delta =  [[0 for c in range(4)] for r in range(3)]
print(delta)

程序输出

[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

2.6 遍历

2.6.1 字典

dict = {'first_name': 'Feng', 'last_name': 'Yubo'}
for (k, v) in dict
    print('my ' + k + 'is' + v)

2.7 模块

if __name__ == '__main__':
    pass

2.8 字符串访问

s = "abcd"
print(a[0:1]) # a
print(a[1:2]) # b
print(a[1:]) # bcd

2.9 对字典进行排序

按key排序

l = sorted(d.items(), key = lambda x:x[0])  # 默认增序
l = sorted(d.items(), key = lambda x:x[0], reverse=True)  # 减序

按value排序

l = sorted(d.items(), key = lambda x:x[1])  # 默认增序
l = sorted(d.items(), key = lambda x:x[1], reverse=True)  # 减序

3. GUI

3.1 PyQt5

PyQt5 runs on Python3 and Qt5.8

3.1.1 Installing on windows/Linux

不建议使用pip原生安装,推荐使用Anaconda

3.2 Installing on Anaconda

方法1

开始> 所有程序> anaconda> anaconda prompt

pip install SIP
pip install PyQt5

这时我们使用的是 Anaconda 下的 pip

方法2

我们可以直接在cmd下使用 conda install 这条命令

conda install SIP
conda install PyQt5

3.2 Tkinter

优点:

  • 足够简单,一天就能开发出来完整的GUI程序

缺点:

  • 开发文档不易检索
  • 互联网上中文教程和资源较少
  • 遇到有深度的问题不易解决,比如如何设置输入类型控件的默认值都查询不到
  • 控件普遍比较低级

3.2.1 控件

Label

obj = tkinter.Label(self)
obj['text'] = 'text of label'
obj.grid(row=r, column=0)

Entry 输入框

obj = thinter.Entry(self)
obj.grid(row=r, column=1)

Combobox 下拉框

该控件需要导入ttk

import tkinter.ttk
obj = ttk.Combobox(self, values=(1,2,3))
obj = grid(row=r, column=1)

3.2.2 布局

grid

需要在构造方法内执行 self.grid(),该布局模式才能被启用

row : 设置控件所在的行
column : 设置控件所在的列
clomnspan : 设置单元格横向跨越的列数

4. 编码规范

完整变量名或含义 临时变量名
list() l
str() s
tuple() t
a single word w

5. 标准库

collections

This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.

deque

Deques are a generalization of stacks and queues (the name is pronounced “deck” and is short for “double-ended queue”). Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

api : reverse()

>>> from collections import deque
>>> l = ['brown', 'read', 'holy', 'bible']
>>> dq = deque(l)
>>> dq.reverse()
deque(['bible', 'holy', 'read', 'brown'])

math.mean in python3 平均数

def mean(numbers):
    return float(sum(numbers)) / max(len(numbers), 1)

Reference : https://stackoverflow.com/questions/7716331/calculating-arithmetic-mean-average-in-python

6. 源

6.1 pypi 源

国内源的地址

名称 地址
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣 http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/

使用方法

$ pip3 install {package name} -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

比如,安装 scikit-learn 的命令用例如下

$ pip3 install scikit-learn -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

参考资料

Python3 中文手册

原文地址:https://www.cnblogs.com/fengyubo/p/6438319.html