练习一:在终端输出如下信息
小明,10岁,男,上山去砍柴
小明,10岁,男,开车去东北
小明,10岁,男,最爱大保健
老李,90岁,男,上山去砍柴
老李,90岁,男,开车去东北
老李,90岁,男,最爱大保健
class Xiaoming: role = '小明' def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex def work(self): hobby = ["上山去砍柴","开车去东北","最爱大保健"] for i in hobby: print(self.name,self.age,self.sex,i) class Laoli: role = '老李' def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex def work(self): hobby = ["上山去砍柴","开车去东北","最爱大保健"] for i in hobby: print(self.name,self.age,self.sex,i) a = Xiaoming('小明','10岁','男') b = Laoli('老李','90岁','男') a.work() b.work() class Men: def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex def hobby(self): hobby = ["上山去砍柴", "开车去东北", "最爱大保健"] for i in hobby: print(('%s,%s岁,%s,%s')%(self.name,self.age,self.sex,i)) a = Men('小明',10,'男') b = Men('老李',90,'男') a.hobby() b.hobby()
圆环的周长与面积
from math import pi class Circle: def __init__(self,r): self.r = r def cal_area(self): return pi*self.r**2 def cal_perimeter(self): return 2*pi*self.r c1=Circle(9) c2=Circle(5) print(c1.cal_area() - c2.cal_area()) print(c1.cal_perimeter() + c2.cal_perimeter())