L=[]
for a in '123456789':
for b in '0123456789':
for c in '123456789':
if a==c:
L.append(a+b+c)
print L
for a in '123456789':
for b in '0123456789':
for c in '123456789':
if a==c:
L.append(a+b+c)
print L
2015-07-31
isinstance(object,class-or-type-or-tuple)->bool
Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument,return whether that is the object's type
The form using a tuple,isinstance(x,(A,B,...)),is a shortcut for
isinstance(x,A) or isinstance(x,B) or.
Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument,return whether that is the object's type
The form using a tuple,isinstance(x,(A,B,...)),is a shortcut for
isinstance(x,A) or isinstance(x,B) or.
def firstCharUpper(s):
return s[0].upper()+s[1:]
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
return s[0].upper()+s[1:]
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
2015-07-31
def average(*args):
if sum(args)==0:
return 0.0
else:
return sum(args)*1.0/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
if sum(args)==0:
return 0.0
else:
return sum(args)*1.0/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2015-07-31
def average(*args):
if sum(args)==0:
return 0
else:
return sum(args)/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
if sum(args)==0:
return 0
else:
return sum(args)/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2015-07-31
def greet(a='word'):
print 'Hello,'+a+'.'
greet()
greet('Bart')
print 'Hello,'+a+'.'
greet()
greet('Bart')
2015-07-31
def move(n, a, b, c):
if n==1:
print a,'-->',c
return
move(n-1,a,c,b)
print a,'-->',c
move(n-1,b,a,c)
move(4, 'A', 'B', 'C')
if n==1:
print a,'-->',c
return
move(n-1,a,c,b)
print a,'-->',c
move(n-1,b,a,c)
move(4, 'A', 'B', 'C')
2015-07-31
由于dict也是集合,len() 函数可以计算任意集合的大小:
>>> len(d)
3
注意: 一个 key-value 算一个,因此,dict大小为3。
>>> len(d)
3
注意: 一个 key-value 算一个,因此,dict大小为3。
2015-07-31