#coding=utf-8
#古老的rot13加密法
def rot13Encode(str):
arr = [];
str = str.lower();
for i in range(len(str)):
if str[i]>='a' and str[i]<='z':
ordNum = ord(str[i]);
if ordNum>ord('m'): #ord将字符转化为数字
arr.append(chr(ordNum-13)); #chr将数字转化为对应的字符
else:
arr.append(chr(ordNum+13));
else:
arr.append(str[i]);
return ''.join(arr);
if __name__=='__main__':
#将两个字典合并在一起的方法
a = {'key1':'value1','key2':'value2'};
b = {'key1':'value3','key3':'value4'};
a.update(b); #update()函数把字典b的键/值对更新到a里
print a;
#按照字典的顺序显示一个字典的键
c = {'a':1,'c':2,'ac':3,'b':4};
print sorted(c.keys());
#按照字典的顺序显示一个字典的键和值
for key in sorted(c.keys()):
print key,c[key];
#按照已经排好序的字典的值,显示字典的键和值
for value in sorted(c.values()):
for key in c.keys():
if c[key] == value:
print key,value;
break;
#rot13加密法
str = rot13Encode('Hello world!');
print str.capitalize();