变量定义和数据类型

变量定义

Python的变量命名规则:

  • 硬性规则:
    • 变量由字母、数字和下划线构成,不能以数字开头
    • 大小写敏感
    • 不能跟python的关键字和系统保留字冲突
  • PEP8要求:
    • 用小写字母拼写,多个单词用下划线连接
    • 受保护的实例属性用单个下划线开头
    • 私有实例属性用两个下划线开头

Python关键字列表:

False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as if/else/elif or yield assert
import break pass except raise
in _* __* *

数据类型

基本数据类型:

  • 整型:对整数的处理

  • 浮点型:对小数的处理

  • 字符串型:单引号和双引号引起来的任意文本

  • 布尔型:True和False两种值,也可通过计算得出

  • 复数型:数学表示法7i+8j

  • int():将一个数值或字符串转换成整数,可以指定进制。

  • float():将一个字符串转换成浮点数。

  • str():将指定的对象转换成字符串形式,可以指定编码。

  • chr():将整数转换成该编码对应的字符串(一个字符)。

  • ord():将字符串(一个字符)转换成对应的编码(整数)。

复杂数据类型:

  • 列表:[ ],可以存储任意类型,任意数量的变量
  • 集合:{ },可以存储任意类型变量,定以后不能更改其元素
  • 字典:{ },以键值对形式存储数据元素

运算操作符

+ / « 左移 & 按位与 <= 小于等于 is/is not 身份运算符
- // » 右移 | 按位或 >= 大于等于 in/not in 成员运算符
* % 整除 < 小于 ^ 按位异或 == 等于 not/and/or 逻辑运算符
** 幂运算 @ > 大于 ~ 按位取反 != 不等于

在实际开发中,运算会有不同的优先级,可以用括号保证运算的执行顺序。

Delimiters

( ) , : - > *= %= |= «=
[ ] . ; += /= @= ^= **=
{ } @ = -= //= &= »=

分支结构

在Python中,构造分支结构使用 if、else 和 elif 关键字。与其他语言不同 Python 没有使用花括号构建代码块,而是使用其独特的缩进方式来设置代码的层次结构。连续的代码保持相同的缩进那么就属于同一个代码块。

分支结构是可以嵌套使用的。

List

list.append(x)
list.extend(iterable)
list.insert(i,x)
list.remove(x)
list.pop([i])
from collections import deque  :  deque([])   deque.popleft()
list.clear()   =  del list
list.index(x[,start[,end]])
list.count(x)
list.sort(key = None,reverse=False)
list.reverse()
list.copy() = a[:]

Tuples & Sequences

1. enumerate(): 
   for i, v in enumerate(['tic', 'tac', 'toe']):
	...     print(i, v)
	...
	0 tic
	1 tac
	2 toe
2. zip()  reversed()  sorted()
3. 

JSON

Python  JSON 对照
dict              {}
list              []
str               'string'
int/float         1234.56
True/False        true/false
None              null
>import json
>json.dumps([1, 'simple', 'list'])
>'[1, "simple", "list"]'
pickle : Pickle Module
	    pickle.dump().
	    pickle.load()

函数

Python内置函数

位置参数:根据参数位置匹配。

默认参数:def fun(arg1,arg2,arg = 'xxx')

  • 定义默认参数时,,默认参数必须指向不变对象
  • 当函数有多个参数时,将变化大的参数放前面,变化小的放后面。变化小的可以作为默认参数。
  • 默认按参数位置匹配,可以通过名字进行相应的默认字段赋值

可变参数:def fun(arg1,arg2,*numbers)

  • 允许传入多个参数,在函数调用时自动组装成一个 tuple
  • 当参数为 list 或则 tuple 时,可以使用*list*tuple

关键字参数:def fun(arg1,arg2,**kw)

  • 允许传入多个含参数名的参数,这些关键字参数在函数内部自动组装为一个 dict
  • 传入 dict 时,只是将所有的参数传入函数,对参数的改变不会影响到函数外的 dict

命名关键字参数:def fun (arg1,arg2,*,arg3,arg4)

  • 对于关键字参数,需要在函数内部进行检查,使用命名关键字必须传入参数名

参数组合:参数定义的顺序(必选参数,默认参数,可变参数,命名关键字,关键字参数)

对于任意函数,都可以通过类似 func(*args, **kw) 的形式调用它,无论它的参数是如何定义的。

递归函数

注意栈溢出问题:解决递归调用栈溢出的方法是通过尾递归优化,事实上尾递归和循环的效果是一样的,所以,把循环看成是一种特殊的尾递归函数也是可以的。

尾递归:指在函数返回的时候,调用自身本身,并且,return语句不能包含表达式。这样,编译器或者解释器就可以把尾递归做优化,使递归本身无论调用多少次,都只占用一个栈帧,不会出现栈溢出的情况。

迭代器(Iterable)

原理

For调用了var = iter(str),next(var),当 next() 没有元素时,raise a Stop Iteration。

集合数据类型:list、tuple、dict、set、str等

generator:sum(i*i for i in range(10))
# 包括生成器和带yield的generator 
function def reverse(data):
  for index in range(len(data)-1, -1, -1):
     yield data[index]

模块(Mod)

导入整个模块:import modname. from modname import *. 导入模块部分:from modname import name1[, name2[, ... nameN]].

OS Interface:
  os:getcwd()  chdir()  system() open()
  shutil:copyfile('source','aim')     move('source','aim')
File Wildcards: 
  glob:glob.glog('*.py')
Output Formatting:
  reprlib: reprlib.repr(set('adfasfsadfsfsfsfs'))
  pprint: pprint.pprint((source,width = vaule))
  textwrap:格式化输出段落适应屏幕宽度  textwrap.fill(source,width=value)
  locale:culture specific data formats	
      x = 1234567.8
      locale.format_string("%s%.*f", (conv['currency_symbol'],conv['frac_digits'], x),
                           grouping=True)
      '$1,234,567.80'
Command Line Arg:	
  sys: sys. args  sys.stdin sys.stdout, sys.stderr.write('Error msg') , sys.exit()
  getopt:
  argparse:
String Pattern Matching:
  re:re.fundall(), re.sub  etc.
Templating:
  string(Template):t = Template('${village}folk send $$10 to $cause.')
      t.substitute(village='Nottingham', cause='the ditch fund')
      >>>'Nottinghamfolk send $10 to the ditch fund.'
Mathematics:
  math:math.cos(),math.log() etc.
  random:random.choice([]),random.sample(range(100),10) etc,
  statistics:(mean,median,variance)etc.
Internet Access:
  urllim.request(retrieving data from URLs):
  smtplib(sending mail): server = smtplib.SMTP('xxx') 
  server.sendmail('from_email','to_email')
  server.quit()
Dates and Times:
  datetime(date) :  date.today()  
Data Compression: (zlib,gzip,bz2,lzma,zipfile,tarfile)
  zlib:zlib.compress('source')   zlib.decompress('zlib_source')
Quality Control:
  doctest: doctest.testmod()
  unittest: unittest.TestCase   assertRaises():   unittest.amin()
Multi-threading:;
  threading: threading.Thread
Logging: 
  logging: sys.stderr(file)   logging.debug()  
  logging.info()  logging.warning()  logging.error() logging.critical()
Weak References:
  wearkref: 
Tools For Lists:
  array: a = array('H',[1213,1414,4124])  sum(a)-只计算list中的内容对a的操作只对list有效
  collections(deque):
  bisect:manipulation sorder lists  bisect.insort(aim,source)
  heapq:use for repeatedly access the smallest element do not run a full list sort(堆排序)
  heapify(list)  heappush(list,value)   [headppop(list) for i in range(x)] 
Decimal Floating Point Ath:(精准计算)
  decimal:  
    help for 1> financial applications and other uses which require exact decimal representation
             2> control over precision
             3> control over rounding to meet legal or regulatory requirements
             4> tracking of significant decimal places, or
             5> applications where the user expects the results to match calculations done by hand

包管理(pip)

Create

On Windows, invoke the venv command as follows:

python -m venv c:\path\to\myenv
venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
     [--upgrade] [--without-pip] ENV_DIR [ENV_DIR ...]

Activate:安装名\Scripts\activate.bat

Managing Packages pip(On Virtual)

https://pypi.org/
pip search xxxx.()
pip install xxxx.()
pip install xxxx=version.()
pip install --upgrade xxxx.()
pip show xxxxx.()
pip list:()
pip uninstall xxx,xxxx,xxxx.()
pip freeze > requirements.txt.()

异常处理

捕捉异常

try:
  <>        #运行别的代码
except <>
  <>        #如果在try部份引发了'name'异常
except <name1,name2><>:
  <>        #如果引发了'name'异常,获得附加的数据
else:
  <>        #如果没有异常发生

最终处理

try:
finaly:
raise:

自定义异常

class Error(Exception):
    """Base class for exceptions in this module."""
    pass
class InputError(Error):
    """Exception raised for errors in the input.
    Attributes:
        expression -- input expression in which the error occurred
        message -- explanation of the error
    """
    def __init__(self, expression, message):
        self.expression = expression
        self.message = message
class TransitionError(Error):
    """Raised when an operation attempts a state transition that's not
    allowed.
    Attributes:
        previous -- state at beginning of transition
        next -- attempted new state
        message -- explanation of why the specific transition is not allowed
    """
    def __init__(self, previous, next, message):
        self.previous = previous
        self.next = next
        self.message = message

异常1

异常2 异常3

面向对象(OO)

定义

类帮助信息:ClassName._doc_查看

class_suite:有数据属性,类成员,方法组成

self:代表类的实例,当前对象的地址,而非类,self.class则指向类

class ClassName:
  """所有员工的基类"""  #类文档字符串
  empCount = 0    #数据属性
  def __init__(self, name, salary):   #该方法接收参数
    self.name = name
    self.salary = salary
    Employee.empCount += 1
  def displayCount(self):
    print "Total Employee %d" % Employee.empCount
  def displayEmployee(self):
    print "Name : ", self.name,  ", Salary: ", self.salary

添加,删除,修改,访问类的属性

demo.attr = value1
demo.attr = value2
del demo.attr
getattr(obj,name[,default])访
hasattr(obj,name[,default])
setattr(obj,name[,default])
delattr(obj,name)

内置属性类

__dict__
__doc__
__name__
_module_
_bases_
__del__
__class__:

对象销毁(垃圾回收)

采用引用计数来跟踪和回收垃圾

封装

如果类具有__setattr__()__delattr__()方法,则调用此方法而不是直接更新实例字典。

__getattr__(self,attr1,attr2):
setattr(self,attr1,attr2)   
object.__getattriabute__()
get(self,实例,所有者)  set()  delete()  set_name()
slots():为此变量分配字符串可迭代阻止每个实例自动创建__dict__和__weakref__

类的继承

(1) class (basic1,basic2)...
    __init_subclass__
(2)Python @property装饰器就是负责把一个方法变成属性调用
(3)mixln:线Ostrich继承自Bird
   Ostrich除了继承自Bird外Runnable

元类

class OrderedClass(type):
  @classmethod
  def __prepare__(metacls, name, bases, **kwds):
    return collections.OrderedDict()
  def __new__(cls, name, bases, namespace, **kwds):
    result = type.__new__(cls, name, bases, dict(namespace))
    result.members = tuple(namespace)
  return result
class A(metaclass=OrderedClass):
  def one(self): pass
  def two(self): pass
  def three(self): pass
  def four(self): pass
>>> A.members
('__module__', 'one', 'two', 'three', 'four')

正则表达式

匹配

\d:匹配一个数字
\w:匹配一个字母或数字
\s:匹配一个空格(TAB)
.:可以匹配任何字符
*:表示任意个字符(包括0个)
+:表示至少一个字符
?:表示0个或1个字符
{n}:表示n个字符
{n,m}:表示n-m个字符
A|B:可以匹配A或B
^:表示行的开头,^\d表示必须以数字开头。
$表示行的结束,\d$表示必须以数字结束。
[0-9a-zA-Z\_]:可以匹配一个数字、字母或者下划线
[0-9a-zA-Z\_]+:可以匹配至少由一个数字、字母或者下划线组成的字符串,比
如'a100','0_Z','Py3000'等等
[a-zA-Z\_][0-9a-zA-Z\_]*:可以匹配由字母或下划线开头,后接任意个由一个数字、字母或者下划线组成的字符串
[a-zA-Z\_][0-9a-zA-Z\_]{0, 19}:更精确地限制了变量的长度是1-20个字符(前面1个字符+后面最多19个字符)

match()

判断是否匹配,如果匹配成功,返回一个Match对象,否则返回None

print(re.match(r'^\d{3}\-\d{3,8}$', '010-12345'))
re.match(r'^\d{3}\-\d{3,8}$','010 12345')

split()

切割字符串。

st = re.split(r'[\s\,\;]+', 'a,;;b, c  d')    print(st)

group()

分组:如果正则表达式中定义了组,就可以在Match对象上用 group() 方法提取出子串来

  • 0:原始
  • 1:第几个字串
m = re.match(r'^(\d{3})-(\d{3,8})$','010-12314')
print(m)
print(m.group(0))
print(m.group(1))
print(m.group(2))

贪婪匹配

正则匹配默认是贪婪匹配,也就是匹配尽可能多的字符。

print(re.match(r'^(\d+)(0*)$', '102300').groups())
#加个?就可以让\d+采用非贪婪匹配
print(re.match(r'^(\d+?)(0*)$', '102300').groups())

编译

编译正则表达式,如果正则表达式的字符串本身不合法,会报错;用编译后的正则表达式去匹配字符串。