数字类型 整数类型 0b101 0o101 0x101 浮点数 13e3 复数类型 1+1j z.real z.imag
运算符
**
内置函数 abs() divmod(x,y) 输出(//,%) pow(x,y,[100]) x的y次方,截取前100位 round(x,[3]) x四舍五入,保留3位小数
多行字符串使用’’‘或""" print(’’‘第一行 第二行 第三行’’’)
转义符 \
format()
“{}说{}你好”.format(‘他’,‘老师’) 位置 “{1}说{0}你好”.format(‘老师’,‘他’) 编号 格式控制: :引导符号 <填充> < <^>,对齐> <宽度> <,千位> <.精度> <类型>
s = '12345' print('{:25}'.format(s)) #12345 25位默认左对齐 print('{:^25}'.format(s)) # 12345 25位居中 print('{:*^25}'.format(s)) #***12345***25位*填充 print('{:<^25}'.format(s)) #<<<12345<<<25位<填充<.精度> .2f保留小数 .2保留两位 <类型> b二进制 cUnicode字符 %百分制
print('{:e}'.format(3.12)) print('{:.2e}'.format(3.12))字符串操作符 + * in 布尔
字符串处理函数 len() str() chr() unicode对应的字符 ord() x对应的unicode hex() 小写16进制 oct() 小写8进制
字符串处理方法 s.lower() s.upper() s.split(a) 根据a分割s,返回列表 s.count() 计数 s.replace(old,new) s.center(width,fillchar) 居中 s.strip() 去头尾的字符 lstrip() rstrip() s.join(iter) s迭代进iterate的元素中
类型转换
int() float() str()
凯撒密码 abcd > cdef
#加密 ptxt = input('原文:') for p in ptxt: if 'a' <= p <= 'z': print(chr(ord('a')+(ord(p)-ord('a')+3) % 26), end='') elif 'A' <= p <= 'Z': print(chr(ord('A')+(ord(p)-ord('A')+3) % 26), end='') else: print(p, end='') #解密 ctxt = input('密文:') for c in ctxt: if 'a' <= c <= 'z': print(chr(ord('a') + (ord(c) - ord('a') - 3) % 26), end='') elif 'A' <= c <= 'Z': print(chr(ord('A') + (ord(c) - ord('A') - 3) % 26), end='') else: print(c, end='')