查看 命名

1
dir(__builtins__)

查看input帮助

1
help(input)

字符串

e记法

布尔类型计算

类型转换

查看类型

特有计算

幂运算操作优先级

三元大小比较

elif

三元操作符

断言

for 循环

1
2
3
member = ['小甲鱼','小布丁','小炮冰','大傻逼']
for i in member:
print(i,len(i))
1
2
3
小布丁 3
小炮冰 3
大傻逼 3

range

列表

一个打了激素的列表 ,一个工厂的仓库 , 混合类型的列表



列表 remove() del pop()

列表分片

比较 从最前面的元素依次比较 (类似字符串比较)

列表运算

用分片方式或copy() 才能深拷贝

浅拷贝只是复制了对象的引用地址,两个对象指向同一个内存地址,所以修改其中任意的值,另一个值都会随之变化,这就是浅拷贝

深拷贝是将对象及值复制过来,两个对象修改其中任意的值另一个值不会改变,这就是深拷贝

元祖 ()

元祖不可修改,删除和插入

元祖的修改

元祖的自动回收

手动回收


字符串 (似元祖) (无字符类型)

函数文档

可变参数

返回多个值(元祖)

使用全局变量

内嵌函数

闭包

声明不是局部变量

lambda

filter

map

设置递归内存深度

递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
### 非递归
def factorial_1(n):
result = n ;
for i in range(1,n):
result *=i;
return result

number = int(input('请输入一个正整数:'))
result = factorial_1(number)
print("%d 的阶乘是: %d"%(number ,result))

### 递归
def factorial_2(n):
if n == 1:
return 1
else:
return n*factorial_2(n-1)

number = int(input("请输入一个正整数"))
result = factorial_2(number)
print("%d 的阶乘是: %d"%(number ,result))

字典

1
2
3
4
5
6
7
8
9
10
11
12
13
14
dict1 = {'李宁': '一切皆有可能', 'nike': 'just do it', '阿迪': 'impossible is nothing'}
print(dict1['阿迪'])

dict2 = {1: 'one', 2: 'two', 3: 'three'}
print(dict2[1])

dict3 = dict((('a', 21), ('b', 43), ('c', 23423)))
print(dict3['c'])

dict4 = dict(我='帅哥', 你='傻b')
print(dict4['我'])
dict4['你']='弟弟'
dict4['他']='儿子'
print(dict4)
1
2
3
4
5
impossible is nothing
one
23423
帅哥
{'我': '帅哥', '你': '弟弟', '他': '儿子'}

查找元素

get(key,new value)
如果有项,则返回value,如果没有返回new value

清空和指向

浅拷贝

浅拷贝之后不互相影响

pop

setdefaut

setdefault() 函数和 get()方法 类似, 如果键不存在于字典中,将会添加键并将值设为默认值。

update

set

frozenset

文件

seek(方法) 是指针



写入

读取文件分割字符

os模块

pickle(将字典二进制存放) 黄瓜

1
2
3
4
5
6
7
8
9
10
11
12
import pickle
import os

print(os.getcwd())
my_list=[124,4.23,"we are go" ,['another list']]
pickle_file=open("my_list.pkl","wb")
pickle.dump(my_list,pickle_file)
pickle_file.close();

pickle_file=open("my_list.pkl","rb")
my_list2=pickle.load(pickle_file)
print(my_list2)

异常

with

抽象出try catch中的细节
代码更简洁

EasyGui

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 # -*- coding:utf-8 -*-
import sys

__author__ = 'Missionary'
__time__ = '2019/3/31 0031 8:59'

import easygui as g

while 1:
g.msgbox("欢迎进入界面")
msg= "请问你要干什么?"
title="游戏互动"
choices=['谈恋爱','打篮球','打工','学习']

choices=g.choicebox(msg,title,choices)

g.msgbox("你的选择是"+ str(choices),"结果")
msg="你希望重新开始吗?"
title="请选择"

if g.ccbox(msg,title):
pass
else:
sys.exit(0)

private 伪私有

1
2
3
4
5
6
7
8
9
10
 name:一般变量,在类中,类外都可以使用,对象直接可以访问

__name:双下划线私有变量,在类中可以访问,类外不能直接访问

_name:单下划线的变量,与双下划线有点类似也是私有变量,区别在于from 模块 import *时不能导入,类和对象能直接访问

__init__:类似这样的两边都有的双下划线的方法时系统具有特殊功能的方法,一般不要这样自定义方法,如__new__用来创建对象,__init__用来初始化对象


xx_:单后置下划线,用于避免与Python关键词的冲突

继承

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 class Parent:
def hello(self):
print("调用父类")

def say(self):
print("调用父类say")

class Child(Parent):
def hello(self):
print("调用子类")

c=Child()
c.hello();
c.say();

重写构造方法

super

多继承

静态方法(无self)

self 绑定 变成实例对象的方法

子类

实例

属性

property(get,set, del)

意思应该是用户一直用的是x,所以用户是不需要修改的,但是作为程序员我们需要修改,不使用property的话两者都需要修改,使得用户体验很差

new 极少重写

垃圾回收机制

加减

模板方法

计时器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import time as t

class MyTimer():

def __init__(self):
self.unit=['年','月','日','时','分','秒']
self.prompt="计时未开始"
self.lasted=[]
#self.start=0
#self.stop =0
self.begin=0
self.end =0

def __str__(self):
return self.prompt

__repr__=__str__

def start(self):
self.begin=t.localtime()
self.prompt='请先调用stop()停止计时'
print("计时开始!")

def stop(self):
if not self.begin:
print("请先调用start()进行计时")
else:
self.end=t.localtime()
self._calc()
print("计时结束!")

##内部方法,计算运行时间
def _calc(self):
self.lasted=[]
self.prompt = "总共运行了"
for index in range(6) :
self.lasted.append(self.end[index]-self.begin[index])
if self.lasted[index]:
self.prompt+=(str(self.lasted[index])+self.unit[index])
print(self.prompt)
# 初始化
self.begin=0
self.end=0


t1=MyTimer()
t1.stop()
print(t1)
t1.start()
print(t1)
t.sleep(4)
t1.stop()
print(t1)

输出:
请先调用start()进行计时
计时未开始
计时开始!
请先调用stop()停止计时
总共运行了4秒
计时结束!
总共运行了4秒

getattribute的优先级高于getattr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class C:
def __getattribute__(self, item):
print("getattribute")
return super().__getattribute__(item)

def __getattr__(self, item):
print('getattr')

def __setattr__(self, key, value):
print("setattr")
super().__setattr__(key,value)

def __delattr__(self, item):
print('delattr')
super().__delattr__(item)

c=C()
c.x
print('========')
c.x=1
print('========')
print(c.x)

del c.x

================================================
输出:
getattribute
getattr
========
setattr
========
getattribute
1
delattr

描述符

instance:拥有者的实例对象,owner:拥有者类

定义容器

迭代器

生成器 yield

模块