http://woodpecker.org.cn/abyteofpython_cn/chinese/index.html
python基础知识看完了,对python的基本语法是简单的用法有了较浅显的理解。
第一个python程序
#! /usr/bin/env python
# The address module
import cPickle as p
class person:
'''This is my first python programme.
It will realize the person class etc.'''
def __init__(self,tn,adr,email):
if len(tn)< 7:
print 'Telephonenumber is too short'
self.telephonenumber=0
else:
self.telephonenumber=tn
if len(adr)<0:
print 'address is wrong'
self.address=''
else:
self.address=adr
if len(email)<0:
print 'email is wrong'
self.email=''
else:
self.email=email
def modify_telephonenumber(self,tn):
if len(tn)< 7:
print 'Telephonenumber is too short'
else:
self.telephonenumber=tn
def modify_address(self,adr):
if len(adr)<0:
print 'address is wrong'
else:
self.address=adr
def modify_email(self,email):
if len(email)<0:
print 'email is wrong'
else:
self.email=email
def printall(self):
print 'Telephonenumber is %s' %self.telephonenumber
print 'Address is %s' %self.address
print 'Email is %s' %self.email
class myaddress:
'''This is my first python programme.
It will realize address class etc.'''
count = 0
def __init__(self):
'''initiator the class'''
myaddress.count+=1
self.addressbook={}
def add_address(self,key,value):
self.addressbook[key] = value
def delete_address(self,key):
del self.addressbook[key]
def printinfo(self,key):
print 'Name is',key
self.addressbook[key].printall()
def printinfoall(self):
for key in self.addressbook:
print 'Name is',key
self.addressbook[key].printall()
def __del__(self):
pass
def process(iam):
while True:
print '''Enter the infomaiton you want to save (if you want quit you can enter 'quit') '''
name = raw_input('Enter the name you want to save : ')
if name == 'quit':
break
tel = raw_input('Enter the telephonenumber you want to save : ')
if tel == 'quit':
break
address = raw_input('Enter the address you want to save : ')
if address == 'quit':
break
email = raw_input('Enter the email you want to save : ')
if email == 'quit':
break
info = person(tel,address,email)
iam.add_address(name,info)
isgoon = raw_input('Is go on ? Y/N: ')
if isgoon == 'N':
break
def storeinfo(savefile,iam):
f = file(savefile,'w')
p.dump(iam,f)
f.close()
def loadinfo(savefile):
f = file(savefile)
iam = p.load(f)
return iam
iam = myaddress()
process(iam)
savefilename = raw_input('Enter the filename you want to use to save : ')
storeinfo(savefilename,iam)
del iam
iam = loadinfo(savefilename)
iam.printinfoall()