@zhengyuhong
2014-10-03T02:10:17.000000Z
字数 891
阅读 1098
Python 读书笔记
str与repr
str函数,它会把值转换为合理形式的字符串,以便用户可以理解
repr会创建一个字符串,它以合法的Python表达式的形式来表示值。
>>> print repr("hello, world!")'hello, world!'>>> print repr(1000L)1000L>>> print str("hello, wolrd!")hello, wolrd!>>> print str(1000L)1000
repr(x)的功能也可以用`x`实现(注意, `是反引号,而不是单引号,键盘的左上角那个),例如
>>> temp = 42>>> print "The temperature is " + `temp`The temperature is 42>>>
input与raw_input区别
raw_input更符合用户输入的习惯,把任何用户输入都转换成字符串存储,在需要其它类型的数据时,调用相应的函数进行转换
input用户输入什么就存储什么,所以用户输入必须符合python语法要求,否则会出错,譬如输入字符串,要使用双引号或者单引号括着"hello world",直接输入Hello world会报错
长字符串
当需要些一个跨行的字符串时,可以摔死三个引号代替普通引号,如
print '''1234'''
如果一行之中最后一个字符是反斜杠,那么换行符本身就“转义”了,也就是忽略了。例如
print "hello,\world!"
与
print "hello, world!"
效果一样
原始字符串,在字符串前面添加r表示不要转义
path = 'C\nD'print pathCDpath = r'C\nD'print pathC\nD
from math import sqrtd = {}exec 'sqrt = 1' in d#将sqrt明明在d命名空间中,就是sqrt是key,对应的键是1sqrt(4)d['sqrt']
创建函数
def functionName(args):functionBodyreturn returnValue#可有可无
