python中/表示浮点数,//表示整除,另外还有导入from __future__ import division,要不会报错
2018-04-03
def calc_prod(lst):
def prod(x,y):
return x*y
def lazy_prod():
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod(x,y):
return x*y
def lazy_prod():
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-04-02
def format_name(s):
return s.capitalize()
# return s.title()
# return s[0].upper()+s[1:].lower()
# return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.capitalize()
# return s.title()
# return s[0].upper()+s[1:].lower()
# return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-04-02
class Fib(object):
def __init__(self, num):
self.num=num
def __str__(self):
L=[0,1]
for x in range(0,self.num-2):
L.append(L[-1]+L[-2])
return str(L)
def __len__(self):
return self.num
f = Fib(10)
print f
print len(f)
def __init__(self, num):
self.num=num
def __str__(self):
L=[0,1]
for x in range(0,self.num-2):
L.append(L[-1]+L[-2])
return str(L)
def __len__(self):
return self.num
f = Fib(10)
print f
print len(f)
2018-04-02
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-04-02
import math
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
2018-04-02
为什么老报这个错误:TypeError: '<' not supported between instances of 'Student' and 'Student'
2018-04-02
class Person(object):
__count = 0
def __init__(self, name):
self.name=name
Person.__count += 1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except:
print 'AttributeError'
__count = 0
def __init__(self, name):
self.name=name
Person.__count += 1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except:
print 'AttributeError'
2018-04-02
class Person(object):
count=0
def __init__(self,name):
self.name=name
p1 = Person('Bob')
Person.count=Person.count+1
print Person.count
p2 = Person('Alice')
Person.count=Person.count+1
print Person.count
p3 = Person('Tim')
Person.count=Person.count+1
print Person.count
count=0
def __init__(self,name):
self.name=name
p1 = Person('Bob')
Person.count=Person.count+1
print Person.count
p2 = Person('Alice')
Person.count=Person.count+1
print Person.count
p3 = Person('Tim')
Person.count=Person.count+1
print Person.count
2018-04-02
class Person(object):
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except: print 'attributeerror'
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except: print 'attributeerror'
2018-04-02
L1=[p1,p2,p3]
L2=sorted(L1, key=lambda x: x.name)
for item in L2:
print(item.name)
L2=sorted(L1, key=lambda x: x.name)
for item in L2:
print(item.name)
2018-04-02
isinstance(s, unicode) 里的unicode不识别啊?!!!!!!!,一直是红色下划线报错,改成str就好了
2018-04-02