简单的脚本用shell脚本比较方便 当shell搞不定时,我们使用python python跨平台语言,现在的系统都支持 PYTHON :一种强类型动态脚本编程语言,不需要编译 openstack-
简单的脚本用shell脚本比较方便
当shell搞不定时,我们使用python
python跨平台语言,现在的系统都支持
PYTHON :一种强类型动态脚本编程语言,不需要编译
openstack---用python写的
在linux中如果卸载python,系统直接崩溃
python.org //pyhon官网
python 2与python 3 不兼容
系统中可同时存在多个python版本,可以自由使用任意版本
运行方式
1.在python环境里 python
2.指定解释器
3.在脚本里面指定解释器,再使用./执行
/usr/bin/env python //env 显示环境。这一句可以不用指定python的绝对路径
encoding:utf8
coding:utf8
python lib/module/package: //python的学习内容,就是学模块的使用
4.python中用sys.path 模块可以查看python默认导入包的路径,我们可以用sys.path.append("路径")增加路径
>>> sys.path['', '/usr/lib/python2.6/site-packages/pip-9.0.1-py2.6.egg', '/usr/lib/python2.6/site-packages/xpinyin-0.5.4-py2.6.egg', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gst-0.10', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib64/python2.6/site-packages/webkit-1.0', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info']>>>
语法:
1.变量直接赋值即可使用
2.字符串用单引号或双引号引起来
3.if 语句 在shell,awk,python中的使用
python的if 后面跟真假,if后面可以跟任何东西
python里的任何东西都是对象(object),值为0为空的都是假,值非0都是真的
True|False 布尔值
4.依靠缩进来区分代码块,指标符和空格最好不要混用
5.while |for 循环
python中的以下序列都可以遍历
1>string "uplooking"
2>list [11,22,33,44] //列表可以修改
3>tuple (11,22,33,44,55) //元组不可以修改
4>dict {"uid":500,"gid":600} //key:value 键值对
6.函数的定义
eg: random 函数 //函数的名字尽量不要和模块的名字相同,否则会出现很多不必要
1 #!/usr/bin/python2.7 2 #coding:utf8 3 #用python写一个猜数字的游戏 4 # 5 import random 6 ran=random.random(0.99) 7 8 #for i in range(5) #让游戏只有5次机会 9 10 #i=0 11 #while i<5: 12 #i = i+1 13 14 #while 1=1: /while 1:/ while True: 15 16 while 1: 17 num=int(raw_input("请输入一个数字:")) 18 if num == ran : 19 print "you are clever" 20 break #跳出循环,exit()跳出整个python程序 21 22 elif num < ran : 23 print "your number is small!" 24 else num > ran : 25 print "your number is big!" 26 print "游戏结束!"~
--------------------------------------------------------------------------------------------------------
pydoc random //查看函数的模块文档 ,类似于shell中的man
python里的一切都是对象,一切对象都有属性和方法
dir(对象) //列出对象的属性和方法(方法即定义的函数)
id(对象) //查看对象在内存中的位置
help(对象) //查看对象怎样使用
>>> abcd = "www.baidu.com">>> a = abcd>>> dir(a)['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']>>> help(a.title) //可以查看如何使用a.title方法>>> a.lower().upper().title()'Www.Baidu.Com'>>> a.lower().upper().title()'Www.Baidu.Com'>>> a.replace("com","xxx")'www.baidu.xxx'>>> a.replace("com","xxx",1)'www.baidu.xxx'>>> a.replace("com","xxx",2)'www.baidu.xxx'>>> >>> a.startswith("www") //以什么开头True>>> a.startswith("www3")False>>> a.endswith("www3") //以什么结尾False>>> a.endswith("com")True>>> >>> a.split(".")['www', 'baidu', 'com']>>> >>> a.replace(".","-").split("-")['www', 'baidu', 'com']>>> >>> a[:2]'ww'>>> a[5:]'aidu.com'>>> a[:4]'www.'>>> a[:4]+"B"+a[5:]'www.Baidu.com'>>> >>> print "%sB%s" % (a[:4],a[5:])www.Baidu.com
python中的pass什么都不做,只是用来占位的
trip //去掉两端的空格
ltrip //去掉开头的空格
rtrip //去掉结尾的空格
count //统计字符数
字符串需要用引号引起来,单双都可以,但是不能嵌套,我们可以用三引号,三引号里可以自由使用单双引号,三引号里面可以包含多行字符。
python中的列表:list 把多个元素组合在一起形成列表
>>> l = [11,22,33,44]>>> l[0]11>>> l[2]33>>> l[-1]44>>> del l[-1]>>> l[11, 22, 33]>>> >>> print len(l)3>>> l.insert(2,"abc")>>> l[11, 22, 'abc', 33]>>> l.insert(len(l)-1,"kkk")>>> l[11, 22, 'xyz', 'abc', 'kkk', 33]>>> l.remove("abc")>>> l[11, 22, 'xyz', 'kkk', 33]>>> >>> l.pop() //删掉最后一个元素33>>> l[11, 22, 'xyz', 'kkk']>>> l.pop(2) //删掉索引为2的元素'xyz'>>> l[11, 22, 'kkk'] >>> bb = [77,88,99,55]>>> l.append(bb)>>> l[11, 22, 'kkk', [77, 88, 99, 55]]>>> l[-1][2]99>>> l>>> l.remove(bb)>>> l[11, 22, 'kkk']>>> >>> l.extend(bb)>>> l[11, 22, 'kkk', [77, 88, 99, 55], 77, 88, 99, 55]>>>
>>> dir(os)
>>> dir(os.path) //方法中还有方法
元组:tuple //只读
>>> t = (11,22,33,44)>>> dir(t)['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']>>> //元组只有两种方法
list(t) ---将元组改变为列表
tuple(l) ---将列表改变为元组 //这两个函数为内部函数
dir(__builtin__) //查看所有的内部
当元组中只有一个对象的时候,我们怎么来定义我们的元组
>>> cc = (11)>>> type(cc)<type 'int'>>>> cc = (11,)>>> type(cc)<type 'tuple'>>>>
fromkeys
引用拷贝 ---列表是引用拷贝
>>> a = [1,2,3,4,5,6,7]>>> b = a>>> a[1, 2, 3, 4, 5, 6, 7]>>> b[1, 2, 3, 4, 5, 6, 7]>>> a.pop(0)1>>> a[2, 3, 4, 5, 6, 7]>>> b[2, 3, 4, 5, 6, 7]>>>
值拷贝---元组和字符串都是硬链接
>>> c = "123">>> d = c>>> c'123'>>> d'123'>>> c = "abc">>> c'abc'>>> d'123'>>>
切片拷贝
>>> a[2, 3, 4, 5, 6, 7]>>> b[2, 3, 4, 5, 6, 7]>>> id(a)140381409723240>>> id(b)140381409723240>>> b = a[:] //用切片拷贝就会是值拷贝>>> id(a)140381409723240>>> id(b)140381409562208>>>
>>> l = [i for i in a if i>3]>>> l[4, 5, 6, 7]>>> l = [i*10 for i in a ]>>> l[20, 30, 40, 50, 60, 70]>>> a[2, 3, 4, 5, 6, 7]>>> l = [i*10 for i in a if i>2 and i<6]>>> l[30, 40, 50]>>>
generator 生成器,用来遍历元素
>>> l = (i*10 for i in a if i>2 and i<6)>>> l<generator object <genexpr> at 0x7fad180e2730>>>> for i in l:... print i... 304050
字典 键值对,成对出现,为无序的
键必须要是可hash 的(即不可变的),值可以为任意的类型
字典可以遍历
>>> tom = {"uid":500,"gid":600,"home":"/home/kate"}>>> tom{'home': '/home/kate', 'gid': 600, 'uid': 500}>>> for i in tom:... print i... homegiduid>>> >>> for i in tom:... print i ," => ", tom[i]... home => /home/kategid => 600uid => 500>>>
>>> e = tom.copy()>>> id(tom)32176912>>> id(e)32132272>>> e{'home': '/home/kate', 'gid': 600, 'uid': 500}>>>
>>> e.items()[('home', '/home/kate'), ('gid', 600), ('uid', 500)]>>> a,b = 11,22>>> a,b = (11,22)>>> a11>>> b22>>> for k,v in e.items():... print k,v... home /home/kategid 600uid 500
iteritms
iterkeys
itervalues //迭代的方式,性能会好些
keys //显示建
>>> e.popitem() //删除键值对('home', '/home/kate')>>> e{'gid': 600, 'uid': 500}>>>
e.update(tom) //合并两个字典
>>> e["shell"] = "/sbin/nologin"
>>> e
{'shell': '/sbin/nologin', 'gid': 600, 'uid': 500}
>>>
>>> "uid" in e
True
>>> tom.get("passwd","uplooking") //如果tom字典里没有passwd这个键,就给他赋值uplooking并且打印'uplooking'>>> tom //原来的字典不会变{'home': '/home/kate', 'gid': 600, 'uid': 500}>>> tom.setdefault("passwd","uplooking")'uplooking'>>> tom //字典的值会发生改变{'passwd': 'uplooking', 'home': '/home/kate', 'gid': 600, 'uid': 500}>>>
如何得到一个字典:
dict()内部函数
字典和列表的合并