1、原始字符串和unicode
普通字符串中反斜杠“\”有特殊作用。
原始字符串: print(r'c:\newfile') 输出原始字符串加r,反斜杠不会有特殊作用。但不能再原始字符串结尾输入反斜杠
unicode字符串:使用u前缀。print(u'hello word')
2、常用基本函数:
abs(value) 绝对值
cmath.sqrt(value) 开方
float(object) 转换成浮点数
input(prompt) 获取用户输入
int(object) 转整型
long(object) 转long型
math.ceil(value) 向上取整
math.floor(value) 向下取整
math.sqrt(value) 开方(不用于负数)
pow(x,y) x的y次幂
raw_input(prompt) 获取用户输入,结果被看作原始字符串
repr(object) 返回值的字符串表示形式
round(number[,ndigits]) 根据给定的精度对数字四舍五入
str(object) 将值转换为字符串
3、列表和元组
##tuple元组并不复杂,除了创建元组和访问元组外没有太多其它操作
1,索引
str = 'hello'
str[-1] //o
2,分片
arrary=[1,2,3,4,5,6,7,8,9,10]
arrary[3:6] //[4,5,6]
arrary[-3:-1]//[8,9]
arrary[-3:] //[8,9,10]
arrary[:3] //[1,2,3]
//步长
arrary[0:10:1] //顺序全部输出
arrary[3:6:3] //[4]
arrary[::4] //[1,5,9]
arrary[:5:-2] //[10,8]步长为负数从右往左输出
序列相加
[1,2,3]+[4,5,6]
//[1,2,3,4,5,6]
3,list函数
list(''python")
//['p','y','t','h','o','n']
删除元素:
names=['Alice','Bob','Cecil']
del names[2]
//names=['Alice','Bob']
列表方法:
1,append //列表末尾追加新对象
lst = [1,2,3]
lst.append(4)
//[1,2,3,4]
2,count //统计元素出现的个数
lst.count(1)
//1
3,extend //拓展原来的列表
//lst2=[4,5,6]
lst.extend(lst2)
//[1,2,3,4,5,6]
4,index //从列表中找出摸个值第一个匹配的索引位置
person = ["I","AM","VERY","STRONG","!"]
person.index("I")
//1
5,insert //将对象插入到列表中
lst.insert(2,"good")
//[1,2,"good",3]
6,pop //移除列表中的一个元素,默认最后一个
lst.pop()
//3
lst.pop(0)
//1
7,remove//移除摸个值的第一个匹配项
person.remove("!")
//
8,reverse//列表元素反向存放
lst.reverse()
//[3,2,1]
9,sort //对列表排序
lst.sort()
//y=sorted(lst)
sorted("python")
//['h','n','o','p','t','y']
10,高级排序
使用cmp,key,reverse参数用于sorted函数
numbers=[5,2,7,9]
numbers.sort(cmp)
//[2,5,7,9]
x=['peter','micheal','allice']
x.sort(key=len)
//['peter','allice','micheal']
x=[4,6,2,1,7,8]
x.sort(reverse=True)
//[8,7,6,4,2,1]