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-07-30
sum = 0
x = 1
n = 1
while True:
sum += n
n *= 2
x += 1
if x>20:break
print sum
x = 1
n = 1
while True:
sum += n
n *= 2
x += 1
if x>20:break
print sum
2015-07-30
sum = 0
x = 1
while x <= 100:
if x /2 != 0:
sum += x
x += 1
print sum
x = 1
while x <= 100:
if x /2 != 0:
sum += x
x += 1
print sum
2015-07-30
age = 20
if age >= 6 and age <18:
print 'teenager'
elif age >= 18:
print 'adult'
else:
print 'kid'
if age >= 6 and age <18:
print 'teenager'
elif age >= 18:
print 'adult'
else:
print 'kid'
2015-07-30
L = ['Adam', 'Lisa', 'Bart']
A = L[0]
L[0] = L[2]
L[2] = A
print L
A = L[0]
L[0] = L[2]
L[2] = A
print L
2015-07-30
# -*- coding: utf-8 -*-
print u'''静夜思
床前明月光,
疑是地上霜。
举头望明月,
低头思故乡。'''
print u'''静夜思
床前明月光,
疑是地上霜。
举头望明月,
低头思故乡。'''
2015-07-29
print r'''"To be,or not to be":that is the question.
Whether it's nobler in the mind to suffer.'''
Whether it's nobler in the mind to suffer.'''
2015-07-29
s = 'Python was started in 1989 by "Guido".\nPython is free and easy to learn.'
print s
print s
2015-07-29
<script>alert('what are you 弄啥嘞');</script>
2015-07-29
要避免 KeyError 发生,有两个办法:
一是先判断一下 key 是否存在,用 in 操作符:
if 'Paul' in d:
print d['Paul']
如果 'Paul' 不存在,if语句判断为False,自然不会执行 print d['Paul'] ,从而避免了错误。
二是使用dict本身提供的一个 get 方法,在Key不存在的时候,返回None:
>>> print d.get('Bart')
59
>>> print d.get('Paul')
None
一是先判断一下 key 是否存在,用 in 操作符:
if 'Paul' in d:
print d['Paul']
如果 'Paul' 不存在,if语句判断为False,自然不会执行 print d['Paul'] ,从而避免了错误。
二是使用dict本身提供的一个 get 方法,在Key不存在的时候,返回None:
>>> print d.get('Bart')
59
>>> print d.get('Paul')
None
2015-07-29