s = 'Python was started in 1989 by \"Guido\".'
s = s+'\nPython is free and easy to learn.'
print s
s = s+'\nPython is free and easy to learn.'
print s
2015-09-02
L = []
for n1 in range(1,10):
for n2 in range(10):
for n3 in range(10):
if n1 ==n3:
L.append(100 * n1 + 10*n2+n3)
print L
for n1 in range(1,10):
for n2 in range(10):
for n3 in range(10):
if n1 ==n3:
L.append(100 * n1 + 10*n2+n3)
print L
2015-09-02
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]
2015-09-02
注意右边编辑器代码中 list 如下:
L = ['Adam', 'Lisa', 'Paul', 'Bart']
Paul的索引是2,Bart的索引是3,如果我们要把Paul和Bart都删掉,请解释下面的代码为什么不能正确运行:
L.pop(2)
L.pop(3)
执行L.pop(2),List下标变为0,1,2
再执行L.pop(3),下标越界。故无法正常运行
怎样调整代码可以把Paul和Bart都正确删除掉?
修改为
L.pop(2)
L.pop(2)
L = ['Adam', 'Lisa', 'Paul', 'Bart']
Paul的索引是2,Bart的索引是3,如果我们要把Paul和Bart都删掉,请解释下面的代码为什么不能正确运行:
L.pop(2)
L.pop(3)
执行L.pop(2),List下标变为0,1,2
再执行L.pop(3),下标越界。故无法正常运行
怎样调整代码可以把Paul和Bart都正确删除掉?
修改为
L.pop(2)
L.pop(2)
2015-09-02
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-09-02
sum = 0
x = 1
n = 1
while True:
sum = sum + x
x = x *2
n += 1
if n > 20:
break
print sum
x = 1
n = 1
while True:
sum = sum + x
x = x *2
n += 1
if n > 20:
break
print sum
2015-09-02
print [int(str(m)+str(n)+str(m)) for m in range(1,10) for n in range(0,10) ]
2015-09-02
L = []
x=1
while x<100:
L.append(x*x)
x+=1
print sum(L)
1.先用循环确定区间
2.然后用append依次加入元素
3.完事x加1,进入下一次循环
* sum()函数接受一个list作为参数
x=1
while x<100:
L.append(x*x)
x+=1
print sum(L)
1.先用循环确定区间
2.然后用append依次加入元素
3.完事x加1,进入下一次循环
* sum()函数接受一个list作为参数
2015-09-02