python学习日记(一)

tuple元胞数组:

1
2
3
4
5
numl=set([1,2,3,4,5,3,1])
temp=[]
for each in num1:
if each not in temp:
temp.append(each)

函数嵌套:汉诺塔 PythonObj/IDLE练习/hanoi.py

1
2
3
4
5
6
7
8
9
10
def hanoi(n,x,y,z):
if n==1:
print(x,'-->',z)
else:
hanoi(n-1,x,z,y)#前n-i个从X挪到Y
print(x,'-->',z)#第n个从X挪到Z
hanoi(n-1,y,x,z)#前n-1个从Y挪到Z

n=int(input("请输入汉诺塔的层数:"))
hanoin(n,'X','Y','Z')

dic的使用:

1
2
3
4
5
dict1=dict((('a',10),('b',20),('c',30),('d',40),('e',50),('f',60)))
type(('a',10))
dict2=dict(a=10,b=20)
dict.fromkeys(range(12),'赞')
dict.

file的使用

1
2
3
4
5
6
7
8
9
10
11
12
with open("C:/Users/10921/Desktop/1.txt") as b:#默认rb二进制只读模式,且使用完毕自动删除b
for line in b.readlines():
print (line)

a=open("C:/Users/10921/Desktop/1.txt",'rb+')
for line in a.readlines():
print(line)
a.close
file.read([size])#若无size默认返回整个文件
file.readlines([size])
for line in f:print line#通过迭代器访问
f.write("hello\n")#

文件系统

1
2
3
4
5
6
7
8
9
10
11
12
13
import os
os.getcwd()
os.chdir(path)#改变目录
listdir(path='.')#列出路径
mkdir(path)
removedirs(path)
rename(old,new)
system(command)#运行系统的shell
os.curdir#当前目录 = .
os.pardir#指代上一级目录 = ..
os.sep#输出操作系统路径分隔符
os.linesep#输出操作系统行终止符
os.name#输出操作系统名称

永久存储:
pickle包:pkl文件,dump,load

1
2
3
4
5
6
7
8
9
10
11
12
13
import pickle
import os
#序列化对象,将对象list1保存到文件pickle_filewb中去
my_list1 = [123,3.14,'小智',['another list']]
pickle_filewb = open('my_list.pkl','wb')
pickle.dump(my_list,pickle_filewb)
pickle_filewb.close()
#反序列化对象,将文件中的数据解析为一个python对象,file中有read()接口和readline()接口
pickle_filerb=open('my_list.pkl','rb')
pickle_filerb=open('my_list.pkl','rb')
my_list2=pickle.load(pickle_filerb)

os.system('del my_list.pkl')#删除

类和对象:
self

1
2
3
4
5
6
7
8
9
10
11
12
class Ball:
def setName(self,name):
self.name=name
def kick(self):
print("我叫%s,该死的,谁踢我" %(self.name))
#创建对象空间后再构造
>>> a=Ball()
>>> a.setName('王大锤')
>>> b=Ball()
>>> b.setName('谢大脚')
>>> a.kick()
我叫王大锤,该死的,谁踢我

魔法方法(构造方法):

| __init__(self,parame1,param2)```
1
2
3
4
5
6
7
8
9
10

```python
>>> class Ball
def __init__(self,name):
self.name=name
def kick(self):
print("我叫%s,该死的,谁踢我" %(self.name))
>>> d=Ball('王大锤')
>>> d.kick()
我叫王大锤,该死的,谁踢我

公有私有变量的定义和调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>>> class Person:
__name='王大锤'
def getName(self):
return self.__name#前面双下划綫表示私有变量,不加默认公有
>>> p=Person()
>>> p.name#错误
Traceback (most recent call last):
File "<pyshell#76>", line 1, in <module>
p.name
AttributeError: 'Person' object has no attribute 'name'
>>> p.getName()
'王大锤'
#还有一种方法可以直接得到私有变量 obj._类名__私有变量名
>>> p._Person__name
'王大锤'

继承inherit语法:

1
2
3
4
5
6
7
>>> class Parent:
def hello(self):
>>> class Child(Parent):
pass
>>> c=Child()
>>> c.hello()
正在调用父类方法。。。
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
mudule:fish_inherit.py
import random as r

class Fish:
def __init__(self):
self.x=r.randint(0,10)
self.y=r.randint(0,10)

def move(self):
self.x -= 1
print('我的位置是:',self.x,self.y)

class Goldfish(Fish):
pass
class Carp(Fish):
pass
class Salmon(Fish):
pass
class Shark(Fish):
def __init__(self):#重写了__init__函数覆盖父类的构造函数,无self.x和self.y
self.hungry=True
def eat(self):
if self.hungry:
print('吃货的梦想就是天天有的吃^_^')
self.hungry = False
else:
print('太饱了,吃不下了!')
运行:
>>> goldfish=Goldfish()
>>> goldfish.move()
我的位置是: 5 10
>>> shark=Shark()
>>> shark.eat()
吃货的梦想就是天天有的吃^_^
>>> shark.move()
Traceback (most recent call last):
File "<pyshell#132>", line 1, in <module>
shark.move()
File "E:/PythonObj/IDLE练习/fish继承.py", line 9, in move
self.x -= 1
AttributeError: 'Shark' object has no attribute 'x'
1
2


1
2