Python环境
windows:
1、下载安装包 https://www.python.org/downloads/ 2、安装 默认安装路径:C:python27 3、配置环境变量 【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【Python安装目录追加到变值值中,用 ; 分割】 如:原来的值;C:python27,切记前面有分号
linux安装:
无需安装,原装Python环境
ps:如果自带2.6,请更新至2.7
更新Python
windows:
卸载重装即可
linux:
Linux的yum依赖自带Python,为防止错误,此处更新其实就是再安装一个Python
查看默认Python版本 python -V 1、安装gcc,用于编译Python源码 yum install gcc 2、下载源码包,https://www.python.org/ftp/python/ 3、解压并进入源码文件 4、编译安装 ./configure make all make install 5、查看版本 /usr/local/bin/python2.7 -V 6、修改默认Python版本 mv /usr/bin/python /usr/bin/python2.6 ln -s /usr/local/bin/python2.7 /usr/bin/python 7、防止yum执行异常,修改yum使用的Python版本 vi /usr/bin/yum 将头部 #!/usr/bin/python 修改为 #!/usr/bin/python2.6
Python 入门
在 /home/dev/ 目录下创建 hello.py 文件,内容如下:
print "hello,world"
执行 hello.py 文件,即:
python /home/dev/hello.py
python内部执行过程如下:
二、解释器
上一步中执行 python /home/dev/hello.py 时,明确的指出 hello.py 脚本由 python 解释器来执行。
如果想要类似于执行shell脚本一样执行python脚本,例: ./hello.py
,那么就需要在 hello.py 文件的头部指定解释器,如下:
#!/usr/bin/env python print "hello,world"
如此一来,执行: ./hello.py
即可.
三、内容编码
python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill)
ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,其最多只能用 8 位来表示(一个字节),即:2**8 = 256,所以,ASCII码最多只能表示 256 个符号。
显然ASCII码无法将世界上的各种文字和符号全部表示,所以,就需要新出一种可以代表所有字符和符号的编码,即:Unicode
Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,规定虽有的字符和符号最少由 16 位来表示(2个字节),即:2 **16 = 65536,
注:此处说的的是最少2个字节,可能更多
UTF-8,是对Unicode编码的压缩和优化,他不再使用最少使用2个字节,而是将所有的字符和符号进行分类:ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存...
所以,python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill),如果是如下代码的话:
报错:ascii码无法表示中文
#!/usr/bin/env python print "你好..."
改正:应该显示的告诉python解释器,用什么编码来执行源代码,即
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 print "你好..."
四、注释
当行注视:# 被注释内容
多行注释:""" 被注释内容 """
五、执行脚本传入参数
Python有大量的模块,从而使得开发Python程序非常简洁。类库有包括三中:
- Python内部提供的模块
- 业内开源的模块
- 程序员自己开发的模块
Python内部提供一个 sys 的模块,其中的 sys.argv 用来捕获执行执行python脚本时传入的参数
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 import sys 5 6 print sys.argv
六、 pyc 文件
执行Python代码时,如果导入了其他的 .py 文件,那么,执行过程中会自动生成一个与其同名的 .pyc 文件,该文件就是Python解释器编译之后产生的字节码。
ps:代码经过编译可以产生字节码;字节码通过反编译也可以得到代码。
七、变量
1、声明变量
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 name = "erxiao"
上述代码声明了一个变量,变量名为: name,变量name的值为:"erxiao"
变量的作用:昵称,其代指内存里某个地址中保存的内容
变量定义的规则:
- 变量名只能是 字母、数字或下划线的任意组合
- 变量名的第一个字符不能是数字
- 以下关键字不能声明为变量名
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
2、变量的赋值
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 name1 = "erxiao" 5 name2 = "sanxiao"
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 name1 = "sixiao" 5 name2 = name1
八、输入
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 # 将用户输入的内容赋值给 name 变量 5 name = raw_input("请输入用户名:") 6 7 # 打印输入的内容 8 print name
输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法,即:
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 import getpass 5 6 # 将用户输入的内容赋值给 name 变量 7 pwd = getpass.getpass("请输入密码:") 8 9 # 打印输入的内容 10 print pwd
九、流程控制和缩进
需求一、用户登陆验证
1 # 提示输入用户名和密码 2 3 # 验证用户名和密码 4 # 如果错误,则输出用户名或密码错误 5 # 如果成功,则输出 欢迎,XXX!
1 #!/usr/bin/env python 2 # -*- coding: encoding -*- 3 4 import getpass 5 6 7 name = raw_input('请输入用户名:') 8 pwd = getpass.getpass('请输入密码:') 9 10 if name == "alex" and pwd == "cmd": 11 print "欢迎,erxiao!" 12 else: 13 print "用户名和密码错误"
需求二、根据用户输入内容输出其权限
1 # 根据用户输入内容打印其权限 2 3 # alex --> 超级管理员 4 # eric --> 普通管理员 5 # tony --> 业务主管 6 # 其他 --> 普通用户
1 #!/usr/bin/env python 2 # -*- coding: encoding -*- 3 4 name = raw_input('请输入用户名:') 5 6 7 if name == "alex": 8 print "超级管理员" 9 elif name == "eric": 10 print "普通管理员" 11 elif name == "tony": 12 print "业务主管" 13 else: 14 print "普通用户"
2 是一个整数的例子。 长整数 不过是大一些的整数。 3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。 (-5+4j)和(2.3-4.6j)是复数的例子。
int(整型)
在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
跟C语言不同,Python的长整数没有指定位宽,即:Python没有限制长整数数值的大小,但实际上由于机器内存有限,我们使用的长整数数值不可能无限大。
注意,自从Python2.2起,如果整数发生溢出,Python会自动将整数数据转换为长整数,所以如今在长整数数据后面不加字母L也不会导致严重后果了。
float(浮点型)
浮点数用来处理实数,即带有小数的数字。类似于C语言中的double类型,占8个字节(64位),其中52位表示底,11位表示指数,剩下的一位表示符号。
complex(复数)
复数由实数部分和虚数部分组成,一般形式为x+yj,其中的x是复数的实数部分,y是复数的虚数部分,这里的x和y都是实数。
"hello world"
1 name = "erxiao" 2 print "i am %s " % name 3 4 #输出: i am erxiao
PS: 字符串是 %s;整数 %d;浮点数%f
- 移除空白
- 分割
- 长度
- 索引
- 切片
1 name_list = ['alex', 'seven', 'eric'] 2 或 3 name_list = list(['alex', 'seven', 'eric'])
基本操作:
- 索引
- 切片
- 追加
- 删除
- 长度
- 切片
- 循环
- 包含
ages = (11, 22, 33, 44, 55) 或 ages = tuple((11, 22, 33, 44, 55))
- 索引
- 切片
- 循环
- 长度
- 包含
person = {"name": "mr.wu", 'age': 18} 或 person = dict({"name": "mr.wu", 'age': 18})
常用操作:
- 索引
- 新增
- 删除
- 键、值、键值对
- 循环
- 长度
![](https://images2015.cnblogs.com/blog/503388/201601/503388-20160118120830029-679075340.png)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/python 2 3 a = 21 4 b = 10 5 c = 0 6 7 c = a + b 8 print "Line 1 - Value of c is ", c 9 10 c = a - b 11 print "Line 2 - Value of c is ", c 12 13 c = a * b 14 print "Line 3 - Value of c is ", c 15 16 c = a / b 17 print "Line 4 - Value of c is ", c 18 19 c = a % b 20 print "Line 5 - Value of c is ", c 21 22 a = 2 23 b = 3 24 c = a**b 25 print "Line 6 - Value of c is ", c 26 27 a = 10 28 b = 5 29 c = a//b 30 print "Line 7 - Value of c is ", c
比较运算:
以下假设变量a为10,变量b为20:
以下实例演示了Python所有比较运算符的操作:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/python 2 3 a = 21 4 b = 10 5 c = 0 6 7 if ( a == b ): 8 print "Line 1 - a is equal to b" 9 else: 10 print "Line 1 - a is not equal to b" 11 12 if ( a != b ): 13 print "Line 2 - a is not equal to b" 14 else: 15 print "Line 2 - a is equal to b" 16 17 if ( a <> b ): 18 print "Line 3 - a is not equal to b" 19 else: 20 print "Line 3 - a is equal to b" 21 22 if ( a < b ): 23 print "Line 4 - a is less than b" 24 else: 25 print "Line 4 - a is not less than b" 26 27 if ( a > b ): 28 print "Line 5 - a is greater than b" 29 else: 30 print "Line 5 - a is not greater than b" 31 32 a = 5; 33 b = 20; 34 if ( a <= b ): 35 print "Line 6 - a is either less than or equal to b" 36 else: 37 print "Line 6 - a is neither less than nor equal to b" 38 39 if ( b >= a ): 40 print "Line 7 - b is either greater than or equal to b" 41 else: 42 print "Line 7 - b is neither greater than nor equal to b"
赋值运算:
以下假设变量a为10,变量b为20:
以下实例演示了Python所有赋值运算符的操作:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/python 2 3 a = 21 4 b = 10 5 c = 0 6 7 c = a + b 8 print "Line 1 - Value of c is ", c 9 10 c += a 11 print "Line 2 - Value of c is ", c 12 13 c *= a 14 print "Line 3 - Value of c is ", c 15 16 c /= a 17 print "Line 4 - Value of c is ", c 18 19 c = 2 20 c %= a 21 print "Line 5 - Value of c is ", c 22 23 c **= a 24 print "Line 6 - Value of c is ", c 25 26 c //= a 27 print "Line 7 - Value of c is ", c
逻辑运算:
成员运算:
以下实例演示了Python所有成员运算符的操作:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/python 2 3 a = 10 4 b = 20 5 list = [1, 2, 3, 4, 5 ]; 6 7 if ( a in list ): 8 print "Line 1 - a is available in the given list" 9 else: 10 print "Line 1 - a is not available in the given list" 11 12 if ( b not in list ): 13 print "Line 2 - b is not available in the given list" 14 else: 15 print "Line 2 - b is available in the given list" 16 17 a = 2 18 if ( a in list ): 19 print "Line 3 - a is available in the given list" 20 else: 21 print "Line 3 - a is not available in the given list"
身份运算:
身份运算符用于比较两个对象的存储单元
以下实例演示了Python所有身份运算符的操作:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/python 2 3 a = 20 4 b = 20 5 6 if ( a is b ): 7 print "Line 1 - a and b have same identity" 8 else: 9 print "Line 1 - a and b do not have same identity" 10 11 if ( id(a) == id(b) ): 12 print "Line 2 - a and b have same identity" 13 else: 14 print "Line 2 - a and b do not have same identity" 15 16 b = 30 17 if ( a is b ): 18 print "Line 3 - a and b have same identity" 19 else: 20 print "Line 3 - a and b do not have same identity" 21 22 if ( a is not b ): 23 print "Line 4 - a and b do not have same identity" 24 else: 25 print "Line 4 - a and b have same identity"
位运算:
按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下:
下表中变量 a 为 60,b 为 13。
以下实例演示了Python所有位运算符的操作:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/python 2 3 a = 60 # 60 = 0011 1100 4 b = 13 # 13 = 0000 1101 5 c = 0 6 7 c = a & b; # 12 = 0000 1100 8 print "Line 1 - Value of c is ", c 9 10 c = a | b; # 61 = 0011 1101 11 print "Line 2 - Value of c is ", c 12 13 c = a ^ b; # 49 = 0011 0001 14 print "Line 3 - Value of c is ", c 15 16 c = ~a; # -61 = 1100 0011 17 print "Line 4 - Value of c is ", c 18 19 c = a << 2; # 240 = 1111 0000 20 print "Line 5 - Value of c is ", c 21 22 c = a >> 2; # 15 = 0000 1111 23 print "Line 6 - Value of c is ", c
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/python 2 3 a = 60 # 60 = 0011 1100 4 b = 13 # 13 = 0000 1101 5 c = 0 6 7 c = a & b; # 12 = 0000 1100 8 print "Line 1 - Value of c is ", c 9 10 c = a | b; # 61 = 0011 1101 11 print "Line 2 - Value of c is ", c 12 13 c = a ^ b; # 49 = 0011 0001 14 print "Line 3 - Value of c is ", c 15 16 c = ~a; # -61 = 1100 0011 17 print "Line 4 - Value of c is ", c 18 19 c = a << 2; # 240 = 1111 0000 20 print "Line 5 - Value of c is ", c 21 22 c = a >> 2; # 15 = 0000 1111 23 print "Line 6 - Value of c is ", c
Python逻辑运算符
Python语言支持逻辑运算符,以下假设变量a为10,变量b为20:
以下实例演示了Python所有逻辑运算符的操作:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/python 2 3 a = 10 4 b = 20 5 c = 0 6 7 if ( a and b ): 8 print "Line 1 - a and b are true" 9 else: 10 print "Line 1 - Either a is not true or b is not true" 11 12 if ( a or b ): 13 print "Line 2 - Either a is true or b is true or both are true" 14 else: 15 print "Line 2 - Neither a is true nor b is true" 16 17 18 a = 0 19 if ( a and b ): 20 print "Line 3 - a and b are true" 21 else: 22 print "Line 3 - Either a is not true or b is not true" 23 24 if ( a or b ): 25 print "Line 4 - Either a is true or b is true or both are true" 26 else: 27 print "Line 4 - Neither a is true nor b is true" 28 29 if not( a and b ): 30 print "Line 5 - Either a is not true or b is not true or both are not true" 31 else: 32 print "Line 5 - a and b are true"
运算符优先级:
以下表格列出了从最高到最低优先级的所有运算符:
以下实例演示了Python所有运算符优先级的操作:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/python 2 3 a = 20 4 b = 10 5 c = 15 6 d = 5 7 e = 0 8 9 e = (a + b) * c / d #( 30 * 15 ) / 5 10 print "Value of (a + b) * c / d is ", e 11 12 e = ((a + b) * c) / d # (30 * 15 ) / 5 13 print "Value of ((a + b) * c) / d is ", e 14 15 e = (a + b) * (c / d); # (30) * (15/5) 16 print "Value of (a + b) * (c / d) is ", e 17 18 e = a + (b * c) / d; # 20 + (150/5) 19 print "Value of a + (b * c) / d is ", e