import math
def is_sqr(x):
return True if math.sqrt(x) % 1 == 0 else False
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return True if math.sqrt(x) % 1 == 0 else False
print filter(is_sqr, range(1, 101))
2016-01-14
class Person():
name=''
def __init__(self,n):
self.name=n
xiaoming = Person('xm')
xiaohong = Person('xh')
print xiaoming.name
print xiaohong.name
print xiaoming==xiaohong
name=''
def __init__(self,n):
self.name=n
xiaoming = Person('xm')
xiaohong = Person('xh')
print xiaoming.name
print xiaohong.name
print xiaoming==xiaohong
2016-01-14
def cmp_ignore_case(s1, s2):
if s1.upper() > s2.upper():
return 1
if s1.upper() < s2.upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.upper() > s2.upper():
return 1
if s1.upper() < s2.upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-01-14
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)
2016-01-13
class Person(object):
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count + 1
print person.__count
p1 = Person('Bob')
p2 = Person('Alice')
print Person.__count
在编译器中这样执行后:
1
2
AttributeError: type object 'Person' has no attribute '__count'
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count + 1
print person.__count
p1 = Person('Bob')
p2 = Person('Alice')
print Person.__count
在编译器中这样执行后:
1
2
AttributeError: type object 'Person' has no attribute '__count'
2016-01-13
import os
print os.path.isdir(r'/data/webroot/resource/python')
print os.path.isfile(r'/data/webroot/resource/python/test.txt')
返回是False
print os.path.isdir(r'/data/webroot/resource/python')
print os.path.isfile(r'/data/webroot/resource/python/test.txt')
返回是False
2016-01-13
class Fib(object):
def __call__(self,number):
x = 0
a,b,L = 0,1,[]
while x<number:
L.append(a)
a,b = b,a+b
x +=1
return L
f = Fib()
print f(10)
def __call__(self,number):
x = 0
a,b,L = 0,1,[]
while x<number:
L.append(a)
a,b = b,a+b
x +=1
return L
f = Fib()
print f(10)
2016-01-12
return "(Student:%s,%s,%d)" % (self.name,self.gender,self.score)
2016-01-12
class Person(object):
def __init__(self,name,gender,**kw):
self.name = name
self.gender = gender
for k in kw:
setattr(self,k,kw[k])
kwargs={'age':25,'course':'Python'}
p = Person('LiLi','man',**kwargs)
print p.name
print p.gender
print p.age
print p.course
def __init__(self,name,gender,**kw):
self.name = name
self.gender = gender
for k in kw:
setattr(self,k,kw[k])
kwargs={'age':25,'course':'Python'}
p = Person('LiLi','man',**kwargs)
print p.name
print p.gender
print p.age
print p.course
2016-01-11