利用 3 层for循环的列表生成式,找出对称的 3 位数。例如,121 就是对称数,因为从右到左倒过来还是 121。
来自 <https://www.imooc.com/code/3544>
问题:131,141,151,不也是对称数吗?
来自 <https://www.imooc.com/code/3544>
问题:131,141,151,不也是对称数吗?
2019-01-26
迭代dict的value
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
//dict的函数,dict的迭代对象,
sum = 0.0
//和运算的公式,sum=0, sum = sum + ?
for v in d.itervalues():
//dict 有value()的方法还有itvalues()的方法,注意是intervalues()的方法
sum = sum + v
//求和公式
print sum / len(d)
//输出sum,求平均数,所以除以len(d), 表示dict的元素个数
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
//dict的函数,dict的迭代对象,
sum = 0.0
//和运算的公式,sum=0, sum = sum + ?
for v in d.itervalues():
//dict 有value()的方法还有itvalues()的方法,注意是intervalues()的方法
sum = sum + v
//求和公式
print sum / len(d)
//输出sum,求平均数,所以除以len(d), 表示dict的元素个数
2019-01-26
age = 8
if age >= 18:
print 'adult'
elif age >= 6:
print 'teenager'
else:
print 'kid'
if age >= 18:
print 'adult'
elif age >= 6:
print 'teenager'
else:
print 'kid'
2019-01-26
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')
2019-01-25
L = []
x = 1
while x <= 100:
L.append(x*x)
x = x + 1
print sum (L)
x = 1
while x <= 100:
L.append(x*x)
x = x + 1
print sum (L)
2019-01-24
def toUppers(L):
return [x.upper() for x in L if isinstance(x,str)]
print toUppers(['Hello', 'world', 101])
return [x.upper() for x in L if isinstance(x,str)]
print toUppers(['Hello', 'world', 101])
s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])
for x in s:
print x[0] + ':', x [1]
for x in s:
print x[0] + ':', x [1]
2019-01-24
months = set(['Jan','Feb','Mar'])
x1 = 'Feb'
x2 = 'Sun'
if x1 in months:
print 'x1: ok'
else:
print 'x1: error'
if x2 in months:
print 'x2: ok'
else:
print 'x2: error'
x1 = 'Feb'
x2 = 'Sun'
if x1 in months:
print 'x1: ok'
else:
print 'x1: error'
if x2 in months:
print 'x2: ok'
else:
print 'x2: error'
2019-01-24
s = set(['adam','bart','lisa','paul'])
print 'adam' in s
print 'bart' in s
print 'adam' in s
print 'bart' in s
2019-01-24
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for key in d:
print key + ':', d[key]
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for key in d:
print key + ':', d[key]
2019-01-24
d = {
95: 'Adam',
85: 'Lisa',
59: 'Bart'
}
d[72] = 'Paul'
print d
95: 'Adam',
85: 'Lisa',
59: 'Bart'
}
d[72] = 'Paul'
print d
2019-01-24
# -*- coding: utf-8 -*-
d = {
95: 'Adam',
85: 'Lisa',
59: 'Bart'
}
print 59 in d
d = {
95: 'Adam',
85: 'Lisa',
59: 'Bart'
}
print 59 in d
2019-01-24
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')
2019-01-24