Python Study Note(1)

变量

  • 除法
>>> 9 / 3
3.0
>>> 10 // 3
3
  • 字符串多个空行
print('''line1
line2
line3''')

list,tuple

  • list:可变数组classmates = ['Michael', 'Bob', 'Tracy']
classmates[-1] #末尾元素
classmates.pop() #删除末尾元素
classmates.pop(1) #删除索引1元素
classmates[1] = "Chen"  ##替代元素
classmates.insert(1,'Jack')
classmates.append('Bai')
  • tuple:不可变数组classmates = ('Michael', 'Bob', 'Tracy')
    t = (1,) #只有一个元素时须加,

条件判断

age = 3
if age >= 18:
    print('your age is', age)
    print('adult')
else:
    print('your age is', age)
    print('teenager')

":"代表后面缩进的部分都是他的块。

循环

  • for x in ...: 遍历
sum = 0
for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
    sum = sum + x
print(sum)
  • random()函数:
>>> range(5)
[0, 1, 2, 3, 4]
  • while
sum = 0
n = 99
while n > 0:
    sum = sum + n
    n = n - 2
print(sum)
  • break:在循环过程中直接退出循环
  • continue:提前结束本轮循环,并直接开始下一循环

字典

判断key是否存在
  • in
>>> 'Thomas' in d
False
  • get
>>> d.get('Thomas') #返回None的时候,交互式命令行不显示结果
>>> d.get('Thomas', -1) #如果key不存在,返回None或者指定的value
-1
基本操作
  • 删除key >>> d.pop('Bob')
set:也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key
>>> s = set([1, 1, 2, 2, 3, 3])
>>> s
{1, 2, 3}

函数

if not isinstance(x, (int, float)):
        raise TypeError('bad operand type')

函数参数

  • 位置参数 def power(x):
  • 默认参数 def power(x, n=2): # power(5)相当于power(5,2)

注意: 默认参数必须指向不变对象

  • 可变参数:可变参数允许你传入0个或任意个参数,这些可变参数在函数调用时自动组装为一个tuple。
def calc(*numbers): #在函数内部,参数numbers接收到的是一个tuple
    sum = 0
    for n in numbers:
        sum = sum + n * n
    return sum
calc(1, 2)

如果已经有一个list或者tuple

>>> nums = [1, 2, 3]
>>> calc(*nums)
  • 关键字参数:关键字参数允许你传入0个或任意个含参数名的参数,这些关键字参数在函数内部自动组装为一个dict。
def person(name, age, **kw):
    print('name:', name, 'age:', age, 'other:', kw)

>>> person('Adam', 45, gender='M', job='Engineer')
name: Adam age: 45 other: {'gender': 'M', 'job': 'Engineer'}

如果已经有一个dict

>>> extra = {'city': 'Beijing', 'job': 'Engineer'}
>>> person('Jack', 24, **extra)
  • 命名关键字参数:如果要限制关键字参数的名字,就可以用命名关键字参数。
def person(name, age, *, city, job): #只接受city,job作为关键字参数
    print(name, age, city, job)

如果函数定义中已经有了一个可变参数,后面跟着的命名关键字参数就不再需要一个特殊分隔符*了:

def person(name, age, *args, city, job):
    print(name, age, args, city, job)
  • 参数组合:参数定义的顺序必须是:必选参数、默认参数、可变参数、命名关键字参数和关键字参数。

使用args和*kw是Python的习惯写法,当然也可以用其他参数名,但最好使用习惯用法。

切片

L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']

>>> L[:3]
['Michael', 'Sarah', 'Tracy']
>>> L[1:3]
['Sarah', 'Tracy']

>>> L[-2:]
['Bob', 'Jack']
>>> L[-2:-1]
['Bob']

tuple和字符串也可以使用。

>>> L[:10:2]  前10个数,每两个取一个:
[0, 2, 4, 6, 8]

高级特性

迭代

dict迭代:默认情况下,dict迭代的是key。如果要迭代value,可以用for value in d.values(),如果要同时迭代key和value,可以用for k, v in d.items()。

其他语言中是通过下标完成,如下

for (i=0; i<list.length; i++) {
    n = list[i];
}

如果我们希望在python中使用,Python内置的enumerate函数可以把一个list变成索引-元素对,这样就可以在for循环中同时迭代索引和元素本身:

>>> for i, value in enumerate(['A', 'B', 'C']):
...     print(i, value)
...
0 A
1 B
2 C

以上形式,在python中较为常用。

>>> for x, y in [(1, 1), (2, 4), (3, 9)]:
...     print(x, y)
...
1 1
2 4
3 9
列表生成式
>>> [x * x for x in range(1, 11) if x % 2 == 0]
[4, 16, 36, 64, 100]
>>> [m + n for m in 'ABC' for n in 'XYZ']
['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']
生成器

将列表生成器变为生成器

>>> L = [x * x for x in range(10)]
>>> g = (x * x for x in range(10))

>>> next(g)  #一个一个打印
>>> g = (x * x for x in range(10))   #循环打印
>>> for n in g:
...     print(n)
... 

将函数变为生成器:

def fib(max):
    n, a, b = 0, 0, 1
    while n < max:
        yield b               #加入yield
        a, b = b, a + b
        n = n + 1
    return 'done'

>>> for n in fib(6):
...     print(n)
...
1
1
2
3
5
8

例子

def triangles():
    n, L = 1, []
    while True:
        n, L = n+1, [1 if(i == len(L) or i == 0) else  L[i - 1] + L[i] for i in range(len(L)+1)]
        yield L
n = 0

for t in triangles():
    print(t)
    n = n + 1
    if n == 10:
        break

[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
迭代器
  • 可迭代对象Iterable:可以直接作用于for循环的对象统称为可迭代对象。一类是集合数据类型,如list、tuple、dict、set、str等;一类是generator,包括生成器和带yield的generator function。

可以使用isinstance()判断一个对象是否是Iterable对象:

>>> from collections import Iterable
>>> isinstance([], Iterable)
True
>>> isinstance({}, Iterable)
True
>>> isinstance('abc', Iterable)
True
>>> isinstance((x for x in range(10)), Iterable)
True
>>> isinstance(100, Iterable)
False
  • 迭代器Iterator:可以被next()函数调用并不断返回下一个值的对象称为迭代器,表示的是一个数据流。

可以使用isinstance()判断一个对象是否是Iterator对象:

>>> from collections import Iterator
>>> isinstance((x for x in range(10)), Iterator)
True
>>> isinstance([], Iterator)
False
>>> isinstance({}, Iterator)
False
>>> isinstance('abc', Iterator)
False
  • 把list、dict、str等Iterable变成Iterator可以使用iter()函数:
>>> isinstance(iter([]), Iterator)
True
>>> isinstance(iter('abc'), Iterator)
True

函数式编程

  • map:map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回。
>>> def f(x):
...     return x * x
...
>>> r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> list(r)            #list()函数让它把整个序列都计算出来并返回一个list。
[1, 4, 9, 16, 25, 36, 49, 64, 81]
  • reduce:reduce把一个函数作用在一个序列[x1, x2, x3, ...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,其效果就是:
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

将str变为int并计算

>>> from functools import reduce
>>> def fn(x, y):
...     return x * 10 + y

...
>>> def char2num(s):
...     return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
...
>>> reduce(fn, map(char2num, '13579'))
13579

  • filter:主要用于过滤,把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。
def is_odd(n):
    return n % 2 == 1

list(filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15]))
# 结果: [1, 5, 9, 15]
  • sort:除普通排序外,它还可以接收一个key函数来实现自定义的排序,例如按绝对值大小排序。
>>> sorted([36, 5, -12, 9, -21], key=abs)
[5, 9, -12, -21, 36]
L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)] 
def by_name(t): 
    return t[0] 
L2=sorted(L,key=by_name) 

key=str.lower 忽略大小写 reverse=True 反向排序

匿名函数
lambda x: x * x 

**等价于下列内容**
def f(x):
    return x * x
装饰器
def log(func):
    @functools.wraps(func)
    def wrapper(*args, **kw):
        print('call %s():' % func.__name__)
        return func(*args, **kw)
    return wrapper

@log  #把@log放到now()函数的定义处,相当于执行了语句:now = log(now)

def now(): 
    print('2015-3')

now()

$ python a.py
call now():
2015-3

带参数的decorator

import functools

def log(text):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kw):
            print('%s %s():' % (text, func.__name__))
            return func(*args, **kw)
        return wrapper
    return decorator

@log('execute')   #相当于>>> now = log('execute')(now)
偏函数
ef int2(x, base=2):
    return int(x, base)

##把一个函数的某些参数给固定住(也就是设置默认值),返回一个新的函数,调用这个新函数会更简单。

>>> import functools
>>> int2 = functools.partial(int, base=2)
内置函数

https://docs.python.org/3/library/functions.html

模块

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' a test module '    #文档注释

__author__ = 'Michael Liao'    

import sys

def test():
    args = sys.argv
    if len(args)==1:
        print('Hello, world!')
    elif len(args)==2:
        print('Hello, %s!' % args[1])
    else:
        print('Too many arguments!')

if __name__=='__main__':    #当我们在命令行运行hello模块文件时,Python解释器把一个特殊变量__name__置为__main__,而如果在其他地方导入该hello模块时,if判断将失败,因此,这种if测试可以让一个模块通过命令行运行时执行一些额外的代码,最常见的就是运行测试。
    test()


使用域
  • 公开的,可以被直接引用:比如:abc,x123,PI等;
  • 特殊的:类似xxx这样的变量是特殊变量,可以被直接引用,但是有特殊用途,比如上面的authorname就是特殊变量。
  • 非公开的:类似_xxx和__xxx这样的函数或变量就是非公开的(private),不应该被直接引用,比如_abc,__abc等。

面向对象编程

访问限制
class Student(object):
    """docstring for Student."""
    def __init__(self, name, score):
        self.__name = name      #__name私有变量
        self.__score = score

    def set_score(self, score):   #从外部修改私有变量,
        self._score = score
    def set_name(self, name):
        self._name =name

    def get_score(self):    #从外部访问获取私有变量
        return self.__score
    def get_name(self):
        return self.__name

    def print_score(self):
        print('%s:%s' % (self._name,self._score))

Bai = Student('Bai', 100)

BaiName = Bai.get_name() 
获取对象信息
  • type
>>> import types
>>> def fn():
...     pass
...
>>> type(fn)==types.FunctionType
True
>>> type(abs)==types.BuiltinFunctionType
True
>>> type(lambda x: x)==types.LambdaType
True
>>> type((x for x in range(10)))==types.GeneratorType
True
>>> type('abc')==str
True
>>> type(123)==int
True
  • isinstance:相比较type,更能反映继承关系,同时可以判断是否是某些类型中的一种
>>> isinstance([1, 2, 3], (list, tuple))
True
>>> isinstance((1, 2, 3), (list, tuple))
True
  • dir:获得一个对象的所有属性和方法。
>>> dir('ABC')
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

类似xxx的属性和方法在Python中都是有特殊用途的,下列就是等价的

>>> len('ABC')
3
>>> 'ABC'.__len__()
3

配合getattr()、setattr()以及hasattr(),我们可以直接操作一个对象的状态。

>>> class MyObject(object):
...     def __init__(self):
...         self.x = 9
...     def power(self):
...         return self.x * self.x
...

>>> obj = MyObject()

>>> hasattr(obj, 'x') # 有属性'x'吗?
True
>>> obj.x
9
>>> hasattr(obj, 'y') # 有属性'y'吗?
False
>>> setattr(obj, 'y', 19) # 设置一个属性'y'
>>> hasattr(obj, 'y') # 有属性'y'吗?
True
>>> getattr(obj, 'y') # 获取属性'y'
19
>>> obj.y # 获取属性'y'
19
##也可以获取方法
>>> getattr(obj, 'power') # 获取属性'power'
<bound method MyObject.power of <__main__.MyObject object at 0x10077a6a0>>
  • slots:想要限制实例的属性.对继承的无效。
class Student(object):
    __slots__ = ('name', 'age') # 用tuple定义允许绑定的属性名称

>>> s = Student() # 创建新的实例
>>> s.name = 'Michael' # 绑定属性'name'
>>> s.age = 25 # 绑定属性'age'
>>> s.score = 99 # 绑定属性'score'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Student' object has no attribute 'score'

-@property:绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改。Python内置的@property装饰器就是负责把一个方法变成属性调用。

class Student(object):

    def get_score(self):
         return self._score

    def set_score(self, value):
        if not isinstance(value, int):
            raise ValueError('score must be an integer!')
        if value < 0 or value > 100:
            raise ValueError('score must between 0 ~ 100!')
        self._score = value

上述写法过于复杂,所以需要简化

class Student(object):
#可读可写
    @property
    def birth(self):
        return self._birth

    @birth.setter
    def birth(self, value):
        self._birth = value
#只可写
    @property
    def age(self):
        return 2015 - self._birth

  • strrepr:用于优化打印结果
class Student(object):
    def __init__(self, name):
        self.name = name
    def __str__(self):
        return 'Student object (name=%s)' % self.name
    __repr__ = __str__

# __str__部分
>>> print(Student('Michael'))    
Student object (name: Michael)

# __repr__部分
>>> s = Student('Michael')
>>> s
<__main__.Student object at 0x109afb310>
  • getattr:如果一个类没有相应属性,那么还可以写一个getattr()方法,动态返回一个属性。一般默认返回None.(只有在没有找到属性的情况下,才调用getattr)
class Student(object):

    def __init__(self):
        self.name = 'Michael'

    def __getattr__(self, attr):
        if attr=='score':
            return 99

  • 枚举
from enum import Enum

Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))

for name, member in Month.__members__.items():
    print(name, '=>', member, ',', member.value)     #value默认从1开始

如果需要更精确地控制枚举类型,可以从Enum派生出自定义类:

from enum import Enum, unique

@unique                              # @unique装饰器可以帮助我们检查保证没有重复值。
class Weekday(Enum):
    Sun = 0 # Sun的value被设定为0
    Mon = 1
    Tue = 2
    Wed = 3
    Thu = 4
    Fri = 5
    Sat = 6

元类
  • type:函数既可以返回一个对象的类型,又可以创建出新的类型。我们说class的定义是运行时动态创建的,而创建class的方法就是使用type()函数。
>>> def fn(self, name='world'): # 先定义函数
...     print('Hello, %s.' % name)
...
>>> Hello = type('Hello', (object,), dict(hello=fn)) # 创建Hello class
>>> h = Hello()
>>> h.hello()
Hello, world.
>>> print(type(Hello))
<class 'type'>
>>> print(type(h))
<class '__main__.Hello'>

要创建一个class对象,type()函数依次传入3个参数:

  1. class的名称;
  2. 继承的父类集合,注意Python支持多重继承,如果只有一个父类,别忘了tuple的单元素写法;

本笔记参考 Linux探索之旅

  1. class的方法名称与函数绑定,这里我们把函数fn绑定到方法名hello上。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容

  • 最近在慕课网学习廖雪峰老师的Python进阶课程,做笔记总结一下重点。 基本变量及其类型 变量 在Python中,...
    victorsungo阅读 1,646评论 0 5
  • 一、python 变量和数据类型 1.整数 Python可以处理任意大小的整数,当然包括负整数,在Python程序...
    绩重KF阅读 1,603评论 0 1
  • Python 是一种相当高级的语言,通过 Python 解释器把符合语法的程序代码转换成 CPU 能够执行的机器码...
    Python程序媛阅读 1,886评论 0 3
  • # 第一优先级规则声明: # 除了梦境,每一个意识主进程都必须与一个身体参与的机械进程相匹配,否则结束意识主进程。...
    李洞BarryLi阅读 3,830评论 0 1
  • 本教程基于Python 3,参考 A Byte of Python v1.92(for Python 3.0) 以...
    yuhuan121阅读 2,997评论 1 6