def square_of_sum(L):
sum = 0
for lv in L:
sum = sum + (lv*lv)
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
sum = 0
for lv in L:
sum = sum + (lv*lv)
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2015-03-17
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for x in d:
print x,":",d[x]
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for x in d:
print x,":",d[x]
2015-03-17
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
print 'adam:',d.get('Adam')
print 'Lisa:',d.get('Lisa')
print 'Bart:',d.get('Bart')
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
print 'adam:',d.get('Adam')
print 'Lisa:',d.get('Lisa')
print 'Bart:',d.get('Bart')
2015-03-17
for x in [ 1,2,3,4,5,6,7,8,9 ]:
for y in [ 1,2,3,4,5,6,7,8,9 ]:
if(x<y):
print x,y
for y in [ 1,2,3,4,5,6,7,8,9 ]:
if(x<y):
print x,y
2015-03-17
sum = 0
x = 0
while True:
x = x + 1
if x > 100:
break
if x%2 == 0:
continue
sum=sum+x
print sum
x = 0
while True:
x = x + 1
if x > 100:
break
if x%2 == 0:
continue
sum=sum+x
print sum
2015-03-17
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for x in L:
if x in s:
s.remove(x)
else:
s.add(x)
print s
留个脚印
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for x in L:
if x in s:
s.remove(x)
else:
s.add(x)
print s
留个脚印
2015-03-17
s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])
for x in s:
print x[0] + ':'+ str(x[1])
留个脚印,set 中每个item都是tuple 所以呢来取就可以。
for x in s:
print x[0] + ':'+ str(x[1])
留个脚印,set 中每个item都是tuple 所以呢来取就可以。
2015-03-17
s = set(['Adam', 'adam', 'Lisa', 'lisa', 'Bart', 'bart', 'Paul', 'paul'])
print 'adam' in s
print 'bart' in s
留个脚印
print 'adam' in s
print 'bart' in s
留个脚印
2015-03-17
s = set(['Adam','Lisa','Bart','Paul'])
print s
这个就是oc中的nsset
print s
这个就是oc中的nsset
2015-03-17
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for key in d:
print key +': '+str(d[key])
留个脚印,写个笔记
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for key in d:
print key +': '+str(d[key])
留个脚印,写个笔记
2015-03-17
d = {
95: 'Adam',
85: 'Lisa',
59: 'Bart'
}
d[72]='Paul'
print d
d[xxx] = xxx 这种赋值方法是在iOS46 之后才出现的,以前都是setvalue forkey ,oc 中貌似 key只能是字符串。
95: 'Adam',
85: 'Lisa',
59: 'Bart'
}
d[72]='Paul'
print d
d[xxx] = xxx 这种赋值方法是在iOS46 之后才出现的,以前都是setvalue forkey ,oc 中貌似 key只能是字符串。
2015-03-17
def average(*args):
if args !=():
return 1.0*sum(args)/len(args)
return 0
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
乘1.0可以解决整数的问题
if args !=():
return 1.0*sum(args)/len(args)
return 0
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
乘1.0可以解决整数的问题
2015-03-17