几道Python的小习题
学习Python的道路漫漫,光看不练比较无聊。
找了个网页,上面有几道习题,无聊之余拿来练手,还有些乐趣。
是这里:http://www.cnblogs.com/belaliu/archive/2006/11/25/572140.html
注:习题后面贴的代码不一定是最优的。
大部分比较好解决,有点难度的是第4题做去除字符串内的空格的操作。
找了网上的解决方案,有这样的好做法:
stringReplace=lambda x:''.join(x.split(' '))
调用时只要stringReplace(myString)就行了。
相关文档:
源代码下载:下载地址在这里
# 024
dict1 = {
'5064001':'Mememe',
'5064002':'tutu',
'5064003':'thrthr',
'5064004':'fofo'
}
print dict1['5064003']
# 也可以使用整型作为唯一的编号
dict2 = {
5064001:'Mememe',
506400 ......
源代码下载:下载地址在这里
# 029
aFile = file(r'C:\in.txt', 'r')
while True:
line = aFile.readline()
if len(line) == 0:
break
# end of if
print line,
# end of while
aFile.close()
output:
>>>
This is the first line.
This is the second line.
This is ......
源代码下载:下载地址在这里
A Byte Of Python
中关于继承有这样的文字:
Suppose you want to write a program which has to keep track of the
teachers and students in a college. They have some common
characteristics such as name, age and address. They also have specific
characteristics such as sala ......
源代码下载:下载地址在这里
raise有两个参数,第一个是由我们自己定义的异常类型,第二个是关于此异常的少量说明信息。
# 038
def getAge():
age = input('Input your age:')
if (age < 0 or age > 160):
raise 'BadAgeError', 'It is impossible!!!!!'
# end of if
return age
# ......