python个人笔记,纯属方便查询。

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
- - - - - - - - - - - - - - - - - - - - - - - python异常处理 - - - - - - - - - - - - - - - - - - - - - - - - -
try :
         name = [ 'a' , 'b' , 'c' ]
         name[ 3 ]
except  IndexError:
         print  'you list is out of range.........'
意思是让原本的错误改成自己想要的错误提示。
try :
         name = [ 'a' , 'b' , 'c' ]
         #name[3]
         info_dic = {}
         info_dic[ 'alex' ]
#except (IndexError,KeyError):
except  IndexError:
         print  'you list is out of range.........'
except  KeyError:
         print  'The key error...............'
自定义异常:
class  myexception(exception):
pass
try :
raise  myexception
name = raw_input ().strip()
if  name ! =  'darren' :
raise  myexception
except  myexception:
print  'no valid name specil!'
总是打印一个错误:
try :
name = raw_input ()
if  len (name) = 2 :
print  'aaa.........'
else :
print  'bbb..........'
finally :
print  'going to shutdown........'
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - python类 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
类的语法:
class  dog:
def  name( self ):
print  "hello,master,my name is python!"
D = dog()     #将类赋给D这个变量
D.name()    #引用dog类下面的name方法。
 
- - - - - - - - - - - - - - - -
#初始化函数:def __init__为初始化函数,函数里的第一个变量就是类的名字,等于把类Person传给self,
#!/usr/bin/env python
class  Person:
         def  __init__( self ,name,age):
                 print  "i am being called right now"
                 self .Name = name
                 self .Age = age
         def  sayhi( self ):
                 print  "hi,my name is %s,my age:%s"  % ( self .Name, self
.Age)
p =  Person( 'darren' , 22 )
p.sayhi()
##################################################
class  Dog:
         name = 'hanxin'
         def  sayhi( self ):
         print  "hello,master,my name is python!"
             print  "my name is:" , self .name
def  saying( self ,food):
if  food  = =  'bone' :
print  'i like it very much'
else :
print   "do not give me"
=  Dog()
d.sayhi()
d.saying(bone)
#这里的类等于把dog传给self,self就是类。类下的多个函数是不能相互通信的,如果要通信要先传给类
#########################################
class  Dog:
         name = 'hanxin'
         def  sayhi( self ):
                 print  "hello,master,my name is python!"
                 print  "my name is:" , self .name
                 likefood = 'bone'
                 self .likefood = likefood   #把函数变量变为类变量
         def  saying( self ,food):
                 if  food  = =  self .likefood:  #调用其他函数的变量这里不能直接调用,而是需要调用类变量才可以。
                         print  'i like it very much'
                 else :
                         print   "do not give me"
=  Dog()
d.sayhi()
d.saying( 'bone' )
########################################
#类的继承:
class  schoolmember:
def  __init__( self ,name,sex,national = 'china' ):
self .name = name
self .sex = sex
self .national = national
def  tell( self ):
print  "hi,my name is %s,i am from %s" % ( self .name, self .national)
class  student(schoolmember):
def  __init__( self ,NAME,SEX,CLASS,SCORE,NATIONAL):
schoolmember.__init__( self ,NAME,SEX,NATIONAL)
self .CLASS = CLASS
self .SCORE = SCORE
def  paytuition( self ,amount):
if  amount <  6499 :
print  "get the fuck off....."
else :
print  'welcome onboard!'
class  teacher(schoolmember):
def  __init__( self ,NAME,SEX,COURSE,SALARY,NATIONAL):
schoolmember.__init__( self ,NAME,SEX,NATIONAL)
self .COURSE = COURSE
self .SALARY = SALARY
def  teachering( self ):
print  "i am teaching %s,i am makeing %s per month!" % ( self .COURSE, self .SALARY)
s1 = student( 'wangzhendong' , 'man' , 'python' , 'A+' , 'china' )
s1.tell()
s2 = student( 'darren' , 'man' , 'linux' , 'a+' , 'us' )
s2.tell()
t1 = teacher( 'darren' , 'man' , 'couse' , '7000' , 'hanguo' )
##############################################################