def __mul__(self, r):
return Rational(self.p*r.p,self.q*r.q)
def __div__(self, r):
return Rational(self.p*r.q,self.q*r.p)
def __str__(self):
g=gcd(self.p,self.q)
return '%s/%s'%(self.p/g,self.q/g)
return Rational(self.p*r.p,self.q*r.q)
def __div__(self, r):
return Rational(self.p*r.q,self.q*r.p)
def __str__(self):
g=gcd(self.p,self.q)
return '%s/%s'%(self.p/g,self.q/g)
2019-08-21
def gcd(a,b):
if b==0:
return a
return gcd(b,a%b)
class Rational(object):
def __init__(self, p, q):
self.p = p
self.q = q
def __add__(self, r):
return Rational(self.p * r.q + self.q * r.p, self.q * r.q)
if b==0:
return a
return gcd(b,a%b)
class Rational(object):
def __init__(self, p, q):
self.p = p
self.q = q
def __add__(self, r):
return Rational(self.p * r.q + self.q * r.p, self.q * r.q)
2019-08-21
from functools import reduce
def prod(x, y):
return x*y
print(reduce(prod, [2, 4, 5, 7, 12]))
def prod(x, y):
return x*y
print(reduce(prod, [2, 4, 5, 7, 12]))
2019-08-21
def format_name(s):
return s[0].upper()+s[1:].lower()
print(list(map(format_name, ['adam', 'LISA', 'barT'])))
return s[0].upper()+s[1:].lower()
print(list(map(format_name, ['adam', 'LISA', 'barT'])))
2019-08-21
def __
self.fib=[]
if num==0:
self.fib=[]
elif num==1:
self.fib=[0]
elif num==2:
self.fib=[0,1]
else:
self.fib=[0,1]
for i in range(2,num):
self.fib.append(self.fib[i-2]+self.fib[i-1])
self.fib=[]
if num==0:
self.fib=[]
elif num==1:
self.fib=[0]
elif num==2:
self.fib=[0,1]
else:
self.fib=[0,1]
for i in range(2,num):
self.fib.append(self.fib[i-2]+self.fib[i-1])
2019-08-21
感觉这种写法是值得注意和思考的:
from os import path
print path.isdir(r'/data/webroot/resource/python')
print path.isfile(r'/data/webroot/resource/python/test.txt')
from os import path
print path.isdir(r'/data/webroot/resource/python')
print path.isfile(r'/data/webroot/resource/python/test.txt')
2019-08-21
def __cmp__(self, s):
if self.score<s.score:
return 1
elif self.score>s.score:
return -1
else:
if self.name<s.name:
return -1
elif self.name>s.name:
return 1
else:
ret
if self.score<s.score:
return 1
elif self.score>s.score:
return -1
else:
if self.name<s.name:
return -1
elif self.name>s.name:
return 1
else:
ret
2019-08-20
def __str__(self):
return '(Student: %s,%s,%s)' %(self.name, self.gender, self.score)
return '(Student: %s,%s,%s)' %(self.name, self.gender, self.score)
2019-08-20
lass Person(object):
def __init__(self, name, gender, **kw):
self.name =name
self.gender=gender
for k,v in kw.iteritems():
setattr(self,k,v)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
def __init__(self, name, gender, **kw):
self.name =name
self.gender=gender
for k,v in kw.iteritems():
setattr(self,k,v)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
2019-08-20
def read(self):
return '["Tim","Bob","Alice"]'
增加read方法
return '["Tim","Bob","Alice"]'
增加read方法
2019-08-20
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender
class Teacher(Person):
def __init__(self, name, gender, course):
super(Teacher,self).__init__(name,gender)
self.course=course
def __init__(self, name, gender):
self.name = name
self.gender = gender
class Teacher(Person):
def __init__(self, name, gender, course):
super(Teacher,self).__init__(name,gender)
self.course=course
2019-08-20
class Person(object):
count=0
def __init__(self,name):
self.name=name
Person.count+=1
count=0
def __init__(self,name):
self.name=name
Person.count+=1
2019-08-20