访问dict
1.d[key] 的形式来查找对应的 value //如果key不存在,会直接报错:KeyError。
1.d[key] 的形式来查找对应的 value //如果key不存在,会直接报错:KeyError。
2019-01-11
sum = 0
x = 1
while x <= 100:
i = 2
while x % i != 0
i += 1
if i==x
x += 1
sum += x
print sum
x = 1
while x <= 100:
i = 2
while x % i != 0
i += 1
if i==x
x += 1
sum += x
print sum
2019-01-10
def square_of_sum(L):
sum =0
for i in L:
sum+=i*i
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
sum =0
for i in L:
sum+=i*i
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2019-01-10
L = []
n=0
for n in range(101):
L.append(pow(n,2))
n=n+1
print sum(L)
n=0
for n in range(101):
L.append(pow(n,2))
n=n+1
print sum(L)
2019-01-10
print [int(a + b + c) for a in '123456789' for b in '0123456789' for c in '0123456789' if a == c]
2019-01-10
L.insert(0,L.pop())
L.insert(2,L.pop(1))
仔细理解这代码的可行性
每步操作的都改变了原有次序
L.insert(2,L.pop(1))
仔细理解这代码的可行性
每步操作的都改变了原有次序
2019-01-10
months = set(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'])
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-09
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for key in d:
print key,':',d.get(key)
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for key in d:
print key,':',d.get(key)
2019-01-09
for i in range(0,101):
if (i//10%10<i%10)
print(i)
感觉这个比答案好点,遍历0到100的数,取出个位和十位,直接比较就可以。
if (i//10%10<i%10)
print(i)
感觉这个比答案好点,遍历0到100的数,取出个位和十位,直接比较就可以。
2019-01-09
for x in ['1','2','3','4','5','6','7','8','9',]:
for y in ['0','1','2','3','4','5','6','7','8','9']:
if x<y:
print x + y
for y in ['0','1','2','3','4','5','6','7','8','9']:
if x<y:
print x + y
2019-01-08
sum = 0
x = 0
while True:
x = x + 1
if x > 100:
break
else:
if x%2==0:
continue
sum =sum +x
print sum
x = 0
while True:
x = x + 1
if x > 100:
break
else:
if x%2==0:
continue
sum =sum +x
print sum
2019-01-08
sum = 0
n = 1
while True:
sum=sum+pow(2,n-1)
n=n+1
if n>20:
break
print sum
n = 1
while True:
sum=sum+pow(2,n-1)
n=n+1
if n>20:
break
print sum
2019-01-08
缩进到底啥规律???
sum = 0
x = 1
while x<100:
if(x%2) == 1:
sum = sum + x
x = x+1
print sum
sum = 0
x = 1
while x<100:
if(x%2) == 1:
sum = sum + x
x = x+1
print sum
2019-01-08