AIOps 一场颠覆传统运维的盛筵
798
2022-10-16
Python运维开发基础01-语法基础
标签(空格分隔): Mr.chen之Python3.0执教笔记(QQ:215379068)
--仅供北大青鸟内部学习交流使用
开发不是看出来的,开发一定是练出来的;想学好开发,没有捷径可走,只有不断的练练练;在运维的学习路线中,首看原理架构知识的掌握,次为实操部署;在开发的学习路线中,首看逻辑思想,再看架构设计,还看编码实操能力。是的,在开发里,没有次看的,都要会,都要会。
开篇导语
整个Python运维开发教学采用的是最新的3.5.2版,当遇到2.x和3.x版本的不同点时,会采取演示的方式,让同学们了解。教学预计分为四大部分,Python开发基础,Python开发进阶,Python网页编程,Python项目实战Python开发基础分为语法基础篇,文件基础篇,函数基础篇,模块基础篇语法基础篇中,我着重希望训练同学的是作为开发应该具备的一种逻辑思路。文件基础篇中,我们需要练习的是如何将数据永久性的存储在硬盘上,提供读,写。函数基础篇中,我重点是要引导同学们构建出整个代码架构的编写设计模式。只有逻辑和架构完全成型同学们才算是从思想上真正进入开发领域的大门。模块基础篇中,我们接触很多常用的模块,比如装饰器和生成器等。并通过这些模块来继续完善优化我们对整个代码架构的设计能力。至此,我们Python开发基础部分就结束了,大概60课时,15小节。
一,Python发展介绍
打开网页直接念百度百科介绍下Python3.X优于Python2.7X的地方企业Centos7.0将和Python3.0同步升级
二,Python的基础环境构建
在编译之前,我们先yum安装一些支持程序
yum -y install gcc gcc-c++ make autoconf readline readline-devel
2.1 Centos6.5升级Python2.7和Python3.5程序
升级Python2.7.13
[root@localhost ~]# tar xf Python-2.7.13.tgz -C /usr/src/[root@localhost ~]# tar xf Python-2.7.13.tgz -C /usr/src/[root@localhost Python-2.7.13]# ./configure --prefix=/usr/local/python27[root@localhost Python-2.7.13]# make && make install[root@localhost python27]# cd /usr/local/python27/[root@localhost python27]# python -VPython 2.6.6[root@localhost python27]# which python/usr/bin/python[root@localhost python27]# mv /usr/bin/python /usr/bin/python26[root@localhost bin]# ln -s /usr/local/python27/bin/python /usr/bin/python [root@localhost bin]# python -VPython 2.7.13
升级Python3.5.2
[root@localhost ~]# tar xf Python-3.5.2.tgz -C /usr/src/[root@localhost ~]# cd /usr/src/Python-3.5.2/[root@localhost Python-3.5.2]# ./configure --prefix=/usr/local/python35[root@localhost Python-3.5.2]# make && make install[root@localhost python35]# cd /usr/local/python35/bin/[root@localhost bin]# ln -s /usr/local/python35/bin/python3 /usr/bin/python3[root@localhost bin]# python3 -VPython 3.5.2
修复yum仓库的编译错误
[root@localhost bin]# vim `which yum`[root@localhost bin]# head -1 `which yum`#!/usr/bin/python26 #修改这里
2.2 创建Python的vim初始练习环境
在家目录创建如下文件和内容
[root@localhost ~]# cat ~/.vimrc set cursorlineset tabstop=4set shiftwidth=4set expandtabfunction PythonHeader() call setline(1,"#!/usr/bin/env python3") call setline(2,"# -*- coding:utf-8 -*-") call setline(3,"# author:你的名字") normal G normal o normal oendfuncautocmd BufNewfile *.py call PythonHeader()map
三,初识Python
3.1 解析器的写法
他们的区别?
#!/usr/bin/env python#!/usr/bin/python
env是一个查找器,它会自动帮我们去找python这个可执行文件位置
3.2 第一个Python程序
在Linux下创建一个文件叫做hello.py
print("hello world!")
然后执行命令:python hello.py
[root@localhost ~]# python3 hello.py hello world!
指定解释器:
如果想用类似于shell脚本一样的方式执行python脚本,例如:./hello.py,那么就需要在hello.py文件的头部指定解释器,如下:
#代码演示:#!/usr/bin/env python3print ("hello world!")#输出结果[root@localhost ~]# ./hello.py hello world!
在交互器中执行代码
除了把程序写在文件中,还可以直接调用python自带的交互器运行代码。
[root@localhost ~]# python3Python 3.5.2 (default, Dec 20 2017, 01:15:07) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> print("Hello world!")Hello world!
3.3 Python的变量
>>> name = 123 #直接赋值数字>>> type(name)
变量定义规则:
['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']
关键字是什么?类似Python程序默认的类型int,str还有系统环境变量等都是关键字
四,思考题与作业:利用Shell实现"模拟用户登录程序"
4.1 shell知识补充
(1)break,continue,return,exit循环控制语句作用演示(2)脚本库函数functions引用
[root@localhost yunjisuan]# cat test.sh #!/bin/bash. /etc/rc.d/init.d/functionsaction "服务运行成功!" /bin/trueaction "服务运行失败!" /bin/false[root@localhost yunjisuan]# sh test.sh 服务运行成功! [确定]服务运行失败! [失败]
4.2 shell实现用户登录验证程序
[root@localhost yunjisuan]# cat login.sh #!/bin/bash#user loginUser="yunjisuan"Passwd="666666"User2="yunjisuan2"Passwd2="123123"Lock=""function Title(){cat<
发表评论
暂时没有评论,来抢沙发吧~