6-1.字符串。string模块中是否有一种字符串方法或者函数可以帮我鉴定下一个字符串是否是另一个大字符串的一部分?
判断一个字符串是否包含另一个字符串: find()、index()、rfind()、rindex()
import string
s = 'sdadaadaa'
print s.find('dad')
print s.index('sda')
6-2.字符串标识符.修改例 6-1 的 idcheck.py 脚本,使之可以检测长度为一的标识符,并且可以识别 Python 关键字,对后一个要求,你可以使用 keyword 模块(特别是 keyword.kwlist)来帮你.
import string
import keyword
alphas = string.letters + '_'
nums = string.digits
kw = keyword.kwlist
print 'Welcome to the Identifier Checker v1.1'
print 'Now we can check keyword.'
print 'Teatees must be at least 1 chars long.'
myInput = raw_input('Identifier to test?')
if len(myInput) > 0:
if myInput[0] not in alphas:
print '''invalid: first symbol must be alphabetic'''
elif myInput in kw:
print 'that is a keyword in Python'
else:
for otherChar in myInput[1:]:
if otherChar not in alphas + nums:
print '''invalid: remaining
symbols must be alphanumeric'''
break
else:
print "okay as an identifier"
6-3.排序
(a).输入一串数字,并从小到大排列之。
aList = [1, 2, 7, 8 ,4, 3]
aList.sort()
print aList
(b).跟a一样。不过要用字典序从小到大排列
aList = {'name': 'cytus', 'age': 18, 'like': 'dog'}
print sorted(aList.keys())
print sorted(aList.items(), key= lambda item:item[1])
6-4.算术。更新上一章里面你的得分测试练习方法方案,把测试得分放到一个列表中去。你的代码可以算出一个平均数。
def testScore():
if score >= '90' and score <= '100':
print("Your testscore is A.")
elif score >= '80' and score <= '89':
pr