d = {'Adam': 95,
'Lisa': 85,
'bart': 59
}
d.setdefault('Paul',75)
print(d)
'Lisa': 85,
'bart': 59
}
d.setdefault('Paul',75)
print(d)
2020-03-05
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for k, v in d.iteritems():
a = len(k)
sum = sum + v
sum1 = sum/a
print(k,':',v)
print 'average', ':', sum1
sum = 0.0
for k, v in d.iteritems():
a = len(k)
sum = sum + v
sum1 = sum/a
print(k,':',v)
print 'average', ':', sum1
2020-03-05
为什么我没用上n (哭.jpg)
sum = 0
x = 1
n = 1
while True:
sum=2**(x-1)+sum
x=x+1
if x>20:
break
print sum
sum = 0
x = 1
n = 1
while True:
sum=2**(x-1)+sum
x=x+1
if x>20:
break
print sum
2020-03-05
L = ['Adam', 'Lisa', 'Bart']
X=L[0]
L[0]=L[-1]
L[-1]=X
print L
X=L[0]
L[0]=L[-1]
L[-1]=X
print L
2020-03-05
print 45678 + 0x12fd2
print 'Learn Python in imooc'
print 100 < 99
print 0xff == 255
print 'Learn Python in imooc'
print 100 < 99
print 0xff == 255
2020-03-04
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
2020-03-04
s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])
for x in s:
print(x[0]+':'+str(x[1]))
for x in s:
print(x[0]+':'+str(x[1]))
2020-03-04
此题用两个变量两个for循环做就可以,不需要三个for循环:print [x+10*y+100*x for x in range(1,10) for y in range(0,10)]
2020-03-04
s = set (['Adam', 'Lisa', 'Bart', 'Paul'])
X = set([name.lower() for name in(s)])
print ('Adam' in x)
X = set([name.lower() for name in(s)])
print ('Adam' in x)
2020-03-03
参考里面为什么要return?
是为了打断递推,否则还会进行后面的代码,造成n<1的情况产生错误;
如果不用return,需要改写成if...else语句跳出循环,也可行
def move(n, a, b, c):
if n ==1:
print a, '-->', c
# return
else:
move(n-1, a, c, b)
print a, '-->', c
move(n-1, b, a, c)
move(4, 'A', 'B', 'C')
是为了打断递推,否则还会进行后面的代码,造成n<1的情况产生错误;
如果不用return,需要改写成if...else语句跳出循环,也可行
def move(n, a, b, c):
if n ==1:
print a, '-->', c
# return
else:
move(n-1, a, c, b)
print a, '-->', c
move(n-1, b, a, c)
move(4, 'A', 'B', 'C')
2020-03-03