安装
一般linux系统默认都安装python,在终端键入python检查是否已经安装成功,若没有,用yum安装。
yum install python27 python27-devel
helloworld脚本
键入python,可以在python的shell下运行print "hello world"
,也可以通过python脚步运行。
vim helloworld.py
#! /usr/bin/python
print "hello world!"
运行脚本 python helloworld.py
修改权限允许运行 chmod +x helloworld.py
运行脚本 ./helloworld.py
基本语法
python要保证严格对齐,不然无法识别闭包——即程序块
流程控制
###if
if (5>7):
print "5>7"
elseif (5>6):
print "5>6"
elseif:
print "5"
###while
while (true):
print "in while"
else :
print "else"
###for
for i in range(1,5):
print i
else:
print "else"
内建数据结构
列表(list)
1)创建
list = [‘1’,(1,2),’1’, ‘2’]
2) 得到list 长度
>>>
print len(list)
4
3) 删除
>>>
del list[0]
>>>
print list
[(1, 2), ‘1’, ‘2’]
>>>
del list[0:2]
>>>
print list
[‘2’]
4) 添加
>>>
list.append(‘3’)
>>>
print list
[‘2’, ‘3’]
5) 插入
>>>
list[0:0] = [‘sample value’]
>>>
print list
[‘sample value’, ‘2’, ‘3’]
>>>
list[0:0] = [‘sample value’, ‘sample value 1’]
>>>
print list
[‘sample value’, ‘sample value 1’, ‘sample value’, ‘2’, ‘3’]
>>>
list[1:2] = [‘sample value 2’, ‘sample value 3’]
>>>
print list
[‘sample value’, ‘sample value 2’, ‘sample value 3’, ‘sample value’, ‘2’, ‘3’]
6) 取值,遍历
取值单个值
>>>
print list[0]
sample value
>>>
print list[1]
sample value 2
>>>
print list[2]
sample value 3
取片段
>>>
print list[2:4]
[‘sample value 3’, ‘sample value’]
遍历
>>>
for line in list:
… print line
…
sample value
sample value 2
sample value 3
sample value
2
3
list的方法
L.append(var) #追加元素
L.insert(index,var)
L.pop(var) #返回最后一个元素,并从list中删除之
L.remove(var) #删除第一次出现的该元素
L.count(var) #该元素在列表中出现的个数
L.index(var) #该元素的位置,无则抛异常
L.extend(list) #追加list,即合并list到L上
L.sort() #排序
L.reverse() #倒序
元祖(tuple)
元组和列表十分类似,只不过元组和字符串一样是
不可变的 即你不能修改元组
tuple = (‘a’, ‘b’, ‘c’, ‘d’, ‘e’)
>>>
print tuple[0]
a
>>>
print tuple[0:2]
(‘a’, ‘b’)
字符串(string)
string = “Hello My friend”
>>>
print string[0]
H
>>>
print string[0:5]
Hello
字符串包含判断操作符:in,not in
>>>
print ‘He’ in string
True
>>>
print ‘sHe’ in string
False
*后面跟数字表示字符串重复的次数,比如
print ‘hello’*5
>>>
hellohellohellohellohello
string查找
S.find(substring, [start [,end]]) #可指范围查找子串,返回索引值,否则返回-1
S.rfind(substring,[start [,end]]) #反向查找
S.index(substring,[start [,end]]) #同find,只是找不到产生ValueError异常
S.rindex(substring,[start [,end]])#同上反向查找
S.count(substring,[start [,end]]) #返回找到子串的个数
S.lowercase()
S.capitalize() #首字母大写
S.lower() #转小写
S.upper() #转大写
S.swapcase() #大小写互换
S.replace(str,’replace’) #替换
S.split(str, ’ ‘) #将string转list,以空格切分
S.join(list, ’ ‘) #将list转string,以空格连接
处理字符串的内置函数
len(str) #串长度
cmp(“my friend”, str) #字符串比较。第一个大,返回1
max(‘abcxyz’) #寻找字符串中最大的字符
min(‘abcxyz’) #寻找字符串中最小的字符
string的转换
float(str) #变成浮点数,float(“1e-1”) 结果为0.1
int(str) #变成整型, int(“12”) 结果为12
int(str,base) #变成base进制整型数,int(“11”,2) 结果为2
long(str) #变成长整型,
long(str,base) #变成base进制长整型,
字符串的格式化
(注意其转义字符,大多如C语言的,略)
str_format % (参数列表) #参数列表是以tuple的形式定义的,即不可运行中改变
>>>
print “”%s’s height is %dcm” % (“My brother”, 180)
结果显示为 My brother’s height is 180cm
字典(dict)
key-value的数据结构,跟c++中的stl:map类似。
创建字典:
1)基本
d = {} #空字典
d = {‘name’:’tom’, ‘age’:22}
#等价
d = {}
d[‘name’] = ‘tom’
d[‘age’] = 22
2)dict
d = dict() #空
d = dict(name=’tom’, age=22)
d = dict([(‘name’,’tom’), (‘age’,22)])
#等价
keys = [‘name’,’age’]
values = [‘tom’, 22]
d = dict(zip(keys,values))
3) fromkeys
>>>
dict.fromkeys([‘name’,’age’],’default_value’)
{‘age’: ‘default_value’, ‘name’: ‘default_value’}
判断key是否存在
if k in d: #k not in
dosomething()
读取
print d[‘name’] #存在得到结果,但是若键不存在,将引发异常KeyError。慎用,建议不使用
print d.get(‘name’, ‘jack’) #存在得到,若键不存在,返回第二个参数default_value.若是没有设default_value返回None
#使用用例
if k in d:
print d[k]
try:
print d[k]
except KeyError:
dosomething()
print d.get(k, default)
等价 d[k] if k in d else default
遍历
for key in d:
print key, d[key]
#等价 for key in d.keys()
for key,value in d.items():
print key, value
修改
d[‘name’] = ‘tom’
d.update({‘name’:’tom’}) #这里支持一整组值
d.update( [ (‘name’,’tom’), (‘age’,2) ] ) #每个元组两个元素,(key,value)
d.update(‘name’=’tom’, ‘age’=4)
删除
del d[‘key’]
value = d.pop(‘key’) #删除并返回值
d.clear() #清空
排序
d = {‘a’:10, ‘c’:8, ‘b’:9, ‘d’:7}
字典排序 按照key排序
keys = d.keys()
keys.sort()
for key in keys:
print d.get(key)
结果为:
10
9
8
7按照value进行排序
sorted(d.items(), lambda x,y: cmp(x[1],y[1]))
结果为:
[(‘d’, 7), (‘c’, 8), (‘b’, 9), (‘a’, 10)]另一种排序方法
sorted(d)
>>>
print d
{‘a’: 10, ‘c’: 8, ‘b’: 9, ‘d’: 7}
其他
dictionary的方法
D.get(key, 0) #同dict[key],多了个没有则返回缺省值,0。[]没有则抛异常
D.has_key(key) #有该键返回TRUE,否则FALSE
D.keys() #返回字典键的列表
D.values() #以列表的形式返回字典中的值,返回值的列表中可包含重复元素
D.items() #将所有的字典项以列表方式返回,这些列表中的每一项都来自于(键,值),但是项在返回时并没有特殊的顺序
D.update(dict2) #增加合并字典
D.popitem() #得到一个pair,并从字典中删除它。已空则抛异常
D.clear() #清空字典,同del dict
D.copy() #拷贝字典
D.cmp(dict1,dict2) #比较字典,(优先级为元素个数、键大小、键值大小)
#第一个大返回1,小返回-1,一样返回0
dictionary的复制
dict1 = dict #别名
dict2=dict.copy() #克隆,即另一个拷贝。
集合(set)
python 的集合类型和 其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素.集合对象还支持union(联合), intersection(交), difference(差)和sysmmetricdifference(对称差集)等数学运算,和我们初中数学学的集合的非常的相似。
创建
a = [2,3,4,2,1]
seta = set(a)
>>>
print seta
set([1, 2, 3, 4]) #重复的2被删除掉了
setb = set(‘abracadabra’)
setc = set(‘alacazam’)
>>>
print setc
set([‘a’, ‘c’, ‘z’, ‘m’, ‘l’])
操作
in or not in
x in seta
x not in seta测试集合是否完全包含
s.issubset(t) #测试是否 s 中的每一个元素都在 t 中
s <= t
s.issuperset(t) #测试是否 t 中的每一个元素都在 s 中
s >= t
- 其他运算符
s.union(t) # 合并
s | t
s.intersection(t) #求交
s & t
s.difference(t) #返回一个新的 set 包含 s 中有但是 t 中没有的元素
s - t
s.symmetric_difference(t) # 返回一个新的 set 包含 s 和 t 中不重复的元素
s ^ t
s.copy() # 返回 set “s”的一个浅复制
s.update(t)
s |= t
s.intersection_update(t)
s &= t
s.difference_update(t)
s -= t
s.symmetric_difference_update(t)
s ^= t
s.add(x) #向 set “s”中增加元素 x
s.remove(x) #从 set “s”中删除元素 x, 如果不存在则引发 KeyError
s.discard(x) #如果在 set “s”中存在元素 x, 则删除
s.pop() #删除并且返回 set “s”中的一个不确定的元素, 如果为空则引发 KeyError
s.clear() #删除 set “s”中的所有元素