1.1 为什么学习python
python的优点
- Python 是简约的语言,非常易于读写,遇到问题时,程序员可以把更多的注意力放在问题本身上,而不用花费太多精力在程序语言、语法上。
免费,Python 是免费开源的。这意味着程序员不用花钱,就可以共享、复制和交换它。这也帮助 Python 形成了强壮的社区,使它更加完善,技术发展更快。专业人士可以在社区和初学者中分享他们的知识和经验。那么找到你可以使用的开源库会得到什么好处?削减一半的项目支出!
兼容性,Python 兼容众多平台,所以开发者不会遇到使用其他语言时常会遇到的困扰。
面向对象,Python 既支持面向过程,也支持面向对象编程。在面向过程编程中,程序员可以复用代码;在面向对象编程中,使用基于数据和函数的对对象。尽管面向对象的程序语言通常十分复杂,Python 却设法保持简洁。
库,Python 社区创造了一大堆各种各样的 Python 库。在他们的帮助下,你可以管理文档,执行单元测试、数据库、Web 浏览器、电子邮件、密码学、图形用户界面和更多的东西。所有东西包括在标准库,然而,除了它,还有很多其他的库。
python 语言的用途
Python 适用于网站、桌面应用开发,自动化脚本,复杂计算系统,科学计算,生命支持管理系统,物联网,游戏,机器人,自然语言处理等很多方面。
1.2 python的安装
- python 是跨平台 linux Windows mac
- python 的环境搭建
一、 linux
默认用的是centos 默认自带的
可以检查一下 python 是否安装
rpm -q python
如果安装有,可以直接运行python 能直接看到版本
[root@jason-test deployer]# python
Python 2.7.5 (default, Jul 13 2018, 13:06:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello world"
hello world
>>> exit ()
二、 Windows
到官网进行下载,下在后默认安装即可,讲目录所在为C 下 安装以后需要到环境变量下配置一下
因为我的是win10 系统 所以 配置起来也比较方便
呃!3 的语法貌似有点差异,没运行成功
后期学习都是以linux为准
所以需要安装一个ipython 工具,在敲命令的时候,可以对命令进行补齐。会比较方便
ipython 需要基于 pip 进行安装 所以需要提前装 pip的包
[root@jason-test deployer]# rpm -q python-pip
package python-pip is not installed
没有安装,默认使用yum 也是安装不上的,因为在ios镜像里面是没有的,这就需要安装一个扩展源
yum install -y epel-release
然后就会在yum.repos.d 这个目录下
[root@jason-test deployer]# ls /etc/yum.repos.d/
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo epel-testing.repo
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo epel.repo
之后就能正常安装上去了
yum install -y python-pip
安装完成后就有了 pip 这个命令了 这个pip 安装的文件是根据 https://pypi.org 站进行查找的 ipython 当前最高的版本 为 7.1.1,支持为 python3 所以。。
pip 升级
pip install --upgrade pip
这个是相等于安装5.30版本的ipython
pip install ipython==5.3.0
使用ipython
[root@jason-test ~]# ipython
Python 2.7.5 (default, Jul 13 2018, 13:06:57)
Type "copyright", "credits" or "license" for more information.
IPython 5.3.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
输入 pr 使用tab键进行补齐,多了很多命令,这样就方便操作了
In [1]: print 1 + 2
3
In [2]: print "hello world"
hello world
In [3]:
Do you really want to exit ([y]/n)?y
输入exit 可以退出 也可以 直接ctrl+d 退出,会直接询问你是否退出,感觉会比较安全
1.3 python的文件类型
建立程序文件的三种方法
一、源代码
python 源代码文件以“py”为扩展名,由python程序进行解释,不需要编译
#!/usr/bin/python
对文件加上 自行权限以后,可以直接使用 ./1.py 进行执行
[root@jason-test day01]# vim 1.py
[root@jason-test day01]# cat 1.py
#!/usr/bin/python
print 'hello world'
[root@jason-test day01]# python 1.py
hello world
[root@jason-test day01]# chmod +x 1.py
[root@jason-test day01]# ./1.py
hello world
如果没有第一行 解释 说明 是什么类型的解释,系统将会默认以为是bash 的脚本,进行解释
[root@jason-test day01]# cat !$
cat 1.py
print 'hello world'
[root@jason-test day01]# ./1.py
./1.py: line 2: print: command not found
###二、字节代码 -python源码文件进行编译后生产的扩展名为“pyc”的文件 -编译方法: 需用通过这个模块进行编译
import py_compile //插入这个模块
py_compile.compile('hello.py')
#!/usr/bin/python
import py_compile
py_compile.compile('1.py') // ‘’里面如果不写路径,将认为默认执行当前路径
[root@jason-test day01]# python 2.py //执行以后发现并没有任何提示
[root@jason-test day01]# ls
1.py 1.pyc 2.py //ls 发现多了一个 pyc 的文件
[root@jason-test day01]# file 1.pyc
1.pyc: python 2.7 byte-compiled //查看文件类型,发现是一个被编译过以后的文件
[root@jason-test day01]# cat 1.pyc
ɂ@s dGHdS(s
hello worldN((((s1.py<module>s // 编译过之后就变成二进制文件了。查看就是乱码了
这类文件,可以直接通过python 来进行执行
[root@jason-test day01]# python 1.pyc
hello world
这个编译以后的文件,源文件移走以后,不会影响正常使用
[root@jason-test day01]# ls
1.py 1.pyc 2.py
[root@jason-test day01]# mv 1.py /tmp
[root@jason-test day01]# ls
1.pyc 2.py
[root@jason-test day01]# python 1.pyc
hello world
三、优化代码
- 经过优化的源码文件,扩展名为“pyo”
- python -O -m py_compile hellp.py
[root@jason-test day01]# python -O -m py_compile 1.py
[root@jason-test day01]# ls
1.py 1.pyc 1.pyo 2.py
[root@jason-test day01]# python 1.pyo
hello world
[root@jason-test day01]# cat 1.pyo
ɂ@s dGHdS(s
hello worldN((((s1.py<module>s
同样是不可以查看的类型
总结 py 为源码文件,pyo、pyc 都是为编译后以后的文件,不同的是pyo 是优化过的;同样的都是不需要源码文件支持就能使用的
1.4 python的变量
- 变量是计算机内存中的一块区域,变量可以储存规定范围内的值,而且值可以改变
- python 中 变量是对一个数据的引用,变量名相当于是一个标签,通过这个标签来读内存数值,变量是可以改变的,那么就可以对变量进行重新赋值,在python中的赋值,就是相当于将这个标签指向了内存中的另外一个区域;和C语言是有区别的,C 是直接对当前的内存数据进行改变
变量的命名
- 变量名由字母、数值、下划线组成。
- 变量不能以数字开头
- 不可以使用关键字 例:
a a1 _a
变量的赋值
-是变量的声明和定义过程 例:
a = 1
id(a) //查看变量的内存地址
In [1]: a = 123
In [2]: id(a)
Out[2]: 17091904
In [3]: a = 456
In [4]: id(a)
Out[4]: 24102432
python 运算符
- 赋值运算符
- 算术运算符
- 关系运算符
- 逻辑运算符
表达式是将不同的数据(包括变量、函数)用运算符号按一定的规则连接起来的一种式子
赋值运算符
- python下,自动的识别你赋值的是什么类型
- type(x) 可以查看这个变量是什么类型
- = : x = 3 , y = 'abcd' // 引号的通常表示字符串,没有引号的通常表示变量。数字除外
In [5]: x = 2
In [6]: x
Out[6]: 2
- +=: x += 2
In [6]: x
Out[6]: 2
In [7]: x +=2
In [8]: x
Out[8]: 4
- -=: x -= 2
In [8]: x
Out[8]: 4
In [9]: x -= 2
In [10]: x
Out[10]: 2
- *=: x *= 2
In [10]: x
Out[10]: 2
In [11]: x *= 2
In [12]: x
Out[12]: 4
- /=: x /= 2
In [12]: x
Out[12]: 4
In [13]: x /= 2
In [14]: x
Out[14]: 2
- %=: x %= 2 取余
In [14]: x
Out[14]: 2
In [15]: x %= 2
In [16]: x
Out[16]: 0
算术运算符
- +: 数字就是数学运算, 字符串就是将两个字符串合在一起
In [17]: 3 + 4
Out[17]: 7
In [18]: 'a' + 'b'
Out[18]: 'ab'
- -:
In [19]: 3 - 4
Out[19]: -1
- *:
In [20]: 3 * 4
Out[20]: 12
- /: 默认为整除,想精确到小数点,只需要将其中一个数值变为浮点,也就是 4变为 4.0 即可得出精确到小数的结果
In [21]: 4 / 3
Out[21]: 1
In [22]: 4.0 / 3
Out[22]: 1.3333333333333333
- //: 为整除,哪怕数值为浮点型,也只能是整数结果
In [25]: 4.0 / 3.5
Out[25]: 1.1428571428571428
In [23]: 4.0 // 3.5
Out[23]: 1.0
- %:
In [25]: 4.0 / 3.5
Out[25]: 1.1428571428571428
In [26]: 4 % 3.5
Out[26]: 0.5
- **:乘方
In [27]: 2**3
Out[27]: 8
In [28]: 2**10
Out[28]: 1024
关系运算符
返回结果,为布尔值
- 大于 : 1 > 2
In [29]: 1 > 2
Out[29]: False
- 小于 : 2 < 3
In [30]: 1 < 2
Out[30]: True
- 大于等于: 1 >= 1
In [32]: 1 >= 2
Out[32]: False
- <=: 2 <= 2
In [31]: 1 <= 2
Out[31]: True
- ==: 2 == 2
In [33]: 3 == 3
Out[33]: True
- !=: 1 != 2
In [34]: 4 != 5
Out[34]: True
逻辑运算符
- and 逻辑与:True and False
两个为True 才会返回 True
In [36]: 1 == 2 and 2 > 1
Out[36]: False
In [37]: 1 == 1 and 2 > 1
Out[37]: True
- or 逻辑或:False or True
两个条件,其中一个为True,那么结果将会返回 True;两个都为True,返回将为False,反之也一样
In [38]: 1 == 2 or 2 > 1
Out[38]: True
In [39]: 1 == 2 or 2 < 1
Out[39]: False
- not 逻辑非: not True 取反
In [40]: not 1 == 2
Out[40]: True
优先级
Lambda | |
逻辑运算: | or |
逻辑运算: | and |
逻辑运算: | not |
成员测试: | in,not in |
同一测试: | is ,is not |
比较 : | < , <= , > , >= , != , == |
按位或 : | 竖线 |
按位异 : | ^ |
按位与 : | & |
移位 : | << , >> |
加法与减法 : | + , - |
乘法 、除法与取余: | * , / , % |
正负号: | +x , -x |
按位翻转: | ~x |
指数: | ** |
###练习
写一个四则运算器
- 要求从键盘读取数值
- 参考: input()与raw_input()
可以使用
help(input) 查看帮助手册
input 更适合数字的运算,raw_input 会直接把输入的任何字符看做字符串
input()
In [41]: input("Please input: ")
Please input: 45
Out[41]: 45
In [42]: input("Please input: ")
Please input: 123
Out[42]: 123
In [43]: input("Please input: ")
Please input: abc //输入abc,的时候,就报错,因为input将它们当做变量了
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-43-7933837eeaf6> in <module>()
----> 1 input("Please input: ")
<string> in <module>()
NameError: name 'abc' is not defined
raw_input()
In [44]: raw_input("Please input: ")
Please input: 123
Out[44]: '123'
In [45]: raw_input("Please input: ")
Please input: 456
Out[45]: '456'
In [46]: raw_input("Please input: ")
Please input: abc
Out[46]: 'abc'
[root@jason-test day01]# vim 3.py
#!/usr/bin/python
num1 = input("Please a number : ")
num2 = input("Please a number : ")
print "%s + %s = %s" % (num1,num2,num1+num2)
print "%s - %s = %s" % (num1,num2,num1-num2)
print "%s * %s = %s" % (num1,num2,num1*num2)
print "%s / %s = %s" % (num1,num2,num1/num2)
[root@jason-test day01]# python 3.py
Please a number : 6
Please a number : 5
6 + 5 = 11
6 - 5 = 1
6 * 5 = 30
6 / 5 = 1
###知识点:
格式化字符串
对应的颜色,为对应颜色的变量(markdown的问题没办法显示颜色),反正就是一个元素对应一个元素就是了。
"%s + %s = %s" % (num1,num2,num1+num2)