Python例子二

例1、构造函数

#-*-coding:utf-8-*-
import sys
class Student:
    def __init__(self,name,age):
        self.__name=name
        self.__age=age
    def getName(self):
        format="my name is %s my age is %d"%(self.__name,self.__age)
        print format
    def __del__(self):
        print "del"
if __name__=="__main__":
   studeng=Student("chu",35)
   studeng.getName()

例二:静态成员与私有成员

#-*-coding:utf-8-*-
import sys
class A:
    y=2
    def __init__(self):
        self.x=1
        self.__z=1
if __name__=="__main__":
    a=A()
    print a.x
    print A.y
    print a.__z

例三:

#-*-coding:utf-8-*-
import sys
class A:
    def prt(self):
        print "my name is A"
    def reprt(self):
        A.prt(self)
if __name__=="__main__":
    a=A()
    a.prt()
    a.reprt()

 例四:字典的散列查找

#-*-coding:utf-8-*-
import sys
if __name__=="__main__":
    dict={"a":"apple","b":"banana","g":"grape","o":"orange"}
    print dict
    print dict["a"]
    dict2={1:"apple",2:"banana",3:"grape",4:"orange"}
    print dict2
    print dict2[1]

 例五:静态方法

#-*-coding:utf-8-*-
import sys
class A:
    def prt(self):
        print "my name is A"
    def reprt(self):
        A.prt(self)
    @staticmethod
    def prt2():
        print "我是静态方法"
if __name__=="__main__":
    a=A()
    a.prt()
    a.reprt()
    A.prt2()

 例六:调用外来模块

#-*-coding:utf-8-*-
import sys
def func():
    print "hello ,调用"
class MyClass:
    def myFunc(self):
        print "myModule.Myclass.myFunc()"
#-*-coding:utf-8-*-
import sys
import random
from _ctypes_test import func
from test32 import func

if __name__=="__main__":
    func()

 例七:单继承

#-*-coding:utf-8-*-
import sys
class A:
    x=1
class B(A):
    y=2
if __name__=="__main__":
    print B.x
    print B.y

 例八:

#-*-coding:utf-8-*-
import sys
class SchoolMember:
    '''Represents any school member.'''
    def __init__(self,name,age):
        self.name = name 
        self.age = age
        print 'Initialized SchoolMember :%s'%self.name
    
    def tell(self):
        '''Tell my details.'''
    print 'Name:"%s" Age:"%s"' % (self.name,self.age)
class Teacher(SchoolMember):
    '''Represents a strudent.'''
    def __init__(self,name,age,salary):
        SchoolMember.__init__(self, name, age)
        self.salary =salary
        
        print '(Initialized Student : %s)' %self.name
    def tell(self):
        SchoolMember.tell(self)
        print 'Marks:"%d"'% self.salary
class Student(SchoolMember):
    '''Represents a strudent.'''
    def __init__(self,name,age,marks):
        SchoolMember.__init__(self, name, age)
        self.marks =marks
        
        print '(Initialized Student : %s)' %self.name
    def tell(self):
        SchoolMember.tell(self)
        print 'Marks:"%d"'% self.marks
if __name__=="__main__":
    t = Teacher('Mrs.Shrividya',40,30000)
    s = Student('Swaroop',22,75)
    

 例九:递归目录,修改文件

#-*-coding:utf-8-*-
import sys
import os
from fileinput import filename

if __name__=="__main__":
    files=os.listdir(".")
    for filename in files:
        print filename
        pos=filename.find(".")
        print pos
        if filename[pos+1:]=="html":
            print filename
            newname=filename[:pos+1]+"htm"
            print newname
            os.rename(filename, newname)
    

 例十:在文本里面,查找hello的字符串个数

#-*-coding:utf-8-*-
import sys
import os
import re

if __name__=="__main__":
    fl=file("aaa.txt","r")
    count=0
    for s in fl.readlines():
        li=re.findall("hello", s)   #把li变成了一个集合
        if len(li)>0:
            count=count+li.count("hello")
    print "查找到"+str(count)+"个hello"   

    fl.close()

 

posted @ 2015-03-06 10:06  kin2321  阅读(226)  评论(0编辑  收藏  举报