Python笔记
一、基础
1.导入: import module from module_name import someone
from __future__ import division 不用整除,如果需要整除 //
2.获取输入:input(tip) raw_input(tip)
3.pow(x, y, z): (x ** y) % z
abs(x): |x|, round(x): 四舍五入
模块: import math
int(x)
复数导入模块cmath
4.#! /usr/bin/python
5.字符串表示
str: 把值转换成合理的字符串
repr: 创建合法的python表达式表示值
r'string' u'string'
6.函数总结
float(x) long(x)
二、列表
1.lst = [...]
索引[index]和分片[from:to[:step]]
加乘:l1 + l2 l1 * n
len(o) max(o) min(o)
e in list/string
list(str)函数
lst[index] = new_value del lst[index]
方法:append(e) count(e) extend(lst) index(e) insert(index, e) pop([index]) remove(e) reverse()
sort()
sorted(o)函数 cmp(x, y)
2.元组(...)不可变,支持分片
tuple(lst)
3.函数总结 reversed(o)
三、字符串
1.格式化 “...%s...%s...” % (...)/value
2.from string import template
s=Template("$x....") s.substitude(x="") safe_substitude()
3.split strip find join lower replace translate
4.函数总结
string.maketrans(to, from)
string.capwords(s[, sep])
四、哈希
hash = {key1: value1.....}
hash = dict([(k1,v1), (k2, v2)])
len(hash) del hash[k]
k in hash
clear copy fromkeys get has_key items iteritems keys iterkeys
pop popitem update setDefault values itervalues
函数总结
dict(seq)