例1
f=open('aa.txt','r') #a.txt的是,1,2,3,4,5,6,7,20,21
c=f.readline()
while len(c)>0:
for d in eval(c):
if d%5==0 or d%7==0:
print(d)
c=f.readline()
f.close()
例2
aa.txt文件:
{‘name’:‘张三’,‘score’:[1,2,3,4]}
(1,2,34)
1+3
3*(5+1)
(1+3*2,7)
f=open('aa.txt','r')
content=f.readlines()#把所有行读到列表里
for line in content: #解析列表
if line[-1]=='\n': #如果此行字符串最后一个字符是\n
t=line[:-1] #去掉最后一个字符\n
else:
t=line #如果是最后一行,那么没有\n
lst=t.split(',')#对字符串进行切片
for i in lst: #对列表进行解析
if int(i)%5==0 or int(i)%7==0:#对元素进行强制类型转换如果能被5或7整除
print(i)
f.close()
例3
(用例2 的文件)
f=open('aa.txt','r',encoding='utf-8')
d=eval(f.readline())
print(d,type(d))
print(d['score'],type(d['score']))
d=eval(f.readline())
print(d,type(d))
d=eval(f.readline())
print(d,type(d))
d=eval(f.readline())
print(d,type(d))
d=eval(f.readline())
print(d,type(d))
f.close()
例4
class Student():
ID=0
name=''
address=''
def say(self):
print(self.ID,self.name,self.address)
zs=Student()
zs.ID=1001
zs.name='张三'
zs.address='山东'
zs.say()
ls=Student()
ls.ID=1002
ls.name='李四'
ls.address='山西'
ls.say()
例5
class Student():
count=0
def __init__(self,name,age):
self.name=name
self.age=age
self.address='山西'
self.tell=1234
Student.count+=1
def say(self):
print(self.name,self.age)
zs=Student('张三',18)
print(zs.name,zs.age,zs.address,zs.tell)
zs.say()
ls=Student('李四',28)
print(ls.name,ls.age,zs.address)
print(Student.count)
例6
class Student():
def __init__(self,name):
self.name=name
def say(self):
print(self.ID,self.sex)
zs=Student('张三')
zs.ID=1001
zs.sex='男'
zs.say()
例7
class Student():
def __init__(self,name):
self.name=name
def say(self):
print(self.ID,self.sex)
def hehe(self):
self.ID = 1001
self.sex = '男'
zs=Student('张三')
zs.hehe()
zs.say()
例8
class Student():
def hehe(self,name):
self.name=name
def haha(self):
self.hh=1002
def p(self):
print(self.zhu)
zs=Student()
zs.hehe('张三')
print(zs.name)
zs.zhu='猪头'
zs.p()
例9
class Student():
count=0#类属性,供所有对象使用
def __init__(self,name,age,sex):#初始化
self.name=name #增加对象属性name
self.age=age
self.sex=sex
Student.count+=1
def say(self):
print(self.name,self.age,self.sex)
zs=Student('张三',10,'男')
ls=Student('李四',20,'女')
zs.say()
ls.say()
print(Student.count)
print(zs.count)
print(ls.count)
例10
class Student():
count=0#类属性,供所有对象使用
def __init__(self,name,age,sex):#初始化
self.name=name #增加对象属性name
self.age=age
self.sex=sex
Student.count+=1
zs=Student('张三',10,'男')
ls=Student('李四',20,'女')
zs.count=10
ww=Student('王五',18,'男')
print(Student.count)
delattr(zs,'count') #删除属性
print(zs.count)
print(ls.count)
例11
class Student():
def __init__(self,name):
self.name=name
zs=Student('张三')
print(zs.name)
delattr(zs,'name')
print(zs.name) #删除后找不到了
例12
class Student():
count=5
def __init__(self,name):
self.name=name
@classmethod #定义类 方法的标志
def haha(cls): #类方法的第一个参数是类
print(cls.count)
def say(self):
print(self.name)
s=Student('张三')
Student.haha()
s.say()
s.haha()
Student.say()
例13
class Student():
count=5
def __init__(self,name):
self.name=name
@staticmethod
def hx():
print('我只是一个方法,跟类和对象没有任何关系')
zs=Student('张三')
Student.hx()
zs.hx()
例14
class Student():
count=5
def __init__(self,name):
self.name=name
@staticmethod
def hx():
print('我只是一个方法,跟类和对象没有任何关系')
@classmethod
def cmethod(cls,cs1,cs2):
print(cls.count*cs1*cs2)
@staticmethod
def smethod(cs1,cs2):
print(cs1*cs2)
def omethod(self,n):
print(self.name*n)
s=Student('张三')
Student.cmethod(3,4)
Student.smethod(3,4)
s.omethod(2)