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
# def cmp_ignore_case(s1, s2):
# ???
print sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower)
# ???
print sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower)
2016-01-10
怪不得不理解,在最新的2.7 或者3.x 版本中,sorted函数的形式早就变了,这里老师还用的2.4 以前的写法,怪不得我们不理解。大家参考2.7或者3.x的sorted 格式要求,很简洁易懂。key 是一个关键字参数,传入一个函数,作用于每一个元素后再排序
2016-01-10
def count():
fs = []
for i in range(1, 4):
def f(i):
def g():
return i*i
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(i):
def g():
return i*i
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2016-01-09
只要用了reduce函数,那么传入一个空的list 就会出错:'reduce() of empty sequence with no initial value'
不用reduce用for循环像这样也会出错,调用一个没有任何元素的空list 就会返回所有元素乘积为1
def calc_prod(lst):
def prod():
a = 1
for i in lst:
a *= i
return a
return prod
不用reduce用for循环像这样也会出错,调用一个没有任何元素的空list 就会返回所有元素乘积为1
def calc_prod(lst):
def prod():
a = 1
for i in lst:
a *= i
return a
return prod
2016-01-09
import math
def is_sqr(x):
return type(math.sqrt(x)) == type(1)
print filter(is_sqr,range(1,101))
def is_sqr(x):
return type(math.sqrt(x)) == type(1)
print filter(is_sqr,range(1,101))
2016-01-08
def cmp_ignore_case(s1, s2):
c1 = s1.lower()
c2 = s2.lower()
if c1 < c2:
return -1
if c1 > c2:
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
c1 = s1.lower()
c2 = s2.lower()
if c1 < c2:
return -1
if c1 > c2:
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-01-08