10-14 Python官网:www.python.org 虚拟机怎么进入python sysadm@template:$ python3 编译Python的文件在哪 sysadm@template:$ vim hello.py

图片

变颜色就是自动矫正 执行: sysadm@template:$ python3 hello.py 或者 sysadm@template:$ chmod +x hello.py sysadm@template:$ ./hello.py sysadm@template:$ sudo mv hello.py /bin/ sysadm@template:$ hello.py Python3的解释器在哪里 sysadm@template:$ which python3 四、用ubuntu的系统: Pip install mysql #pip不存在,pip是安装python包的 Apt install pip #下载pip Pip installmysql 五、进入python界面:

root@ubuntu:~# python3

import os import sys os.system(“hostname”) Ubuntu #我的主机名是这个 0 #0表示执行成功 os.popen(“hostname”) #调用系统中的命令,返回的结果是系统对象 <os._wrap_close object at 0x7efc90cf6f80> os.popen(“hostname”).read() ‘ubuntu\n’ #\n代表换行符号 os.system(“ip a”) #看网卡 os.system(“ipwqdd”) sh: 1: ipwqdd: not found 32512 #代表IO错误,磁盘没有这个命令,这里返回的是非0表示命令失败 ——————————————————————————————以上是简单介绍 六、字符串 import this The Zen of Python, by Tim Peters #python的阐综,之下是赞美python的 当字符中有‘可以通过”包括,反之,但是既有’也有“那就用三引号包含 str1=“hello python” #用单引号和双引号去定义字符类型都是可以的 print(str1) hello python type(str1) #查看字符类型 <class ‘str’> he said,“Aren't can't shouldn't wouldn't #\表示转义字符 变量 定义变量最简单就是=。变量名字由字母、数字、下划线组成 数字不能开头 a=1 type(a) <class ‘int’> #整型 b=‘abc’ type(b) <class ‘str’> #字符串类型 序列 不是一个单独的类型,字符串、元组、列表都是序列,两个特点:索引和切片。 整型、字符串、元组、列表、字典。列表由[]表示平行和并列的关系 字典比喻描述什么的特点。 如何来做索引: str1=“hello python” str1[1] #取值里面的第二个值,第一个值是0 ’e’ str1[-1 ] #取从后面数最后一个 ’n’ str1[1:3] #表示从第二个到第四个字符 ’el’ str1[:-1] #表示除了最后一个意外全都要 ‘hello pytho’ str1[0::2] #表示从第一个开始,每隔两个 ‘hlopto’ len(str1) #求序列的长度 12 str1=“hello " str2=“world” print(str1+str2) #括号里面的+左右有没有空格无所谓,shell里需要 hello world #还可以做拼接 print(’#’*10) #一个字符想要重复10次 print(“hello”*10) hellohellohellohellohellohellohellohellohellohello ‘c’ in str1 #简单的包含条件判断,看看c有没有在str1所在的字符 False “w” in str2 True max(str2) #求ask11码谁最大,可以理解在26字母最靠后 ‘w’ min(str2) #求ask11码谁最小,可以理解在26字母最靠前 ’d’ hostinfo=‘www 192.168.0.11 00:54:52:68:22’ hostinfo[0:3] ‘www’ 怎么限定对方只能访问到我的80端口?元组:主要解决字符串定义格式的弊端:截取对方发过来的某一个固定选取哪个数据。 hostinfo=‘www 192.168.0.11 00:54:52:68:22’ #字符串类型 hostinfo1=(“www”,“192.168.0.11”,“00:54:52:68:22”) #元组 hostinfo[0:3] ‘www’ hostinfo1[0] ‘www’ 元组的id是用一个映射表存数据,不会改变id t1=() type(t1) <class ’tuple’> #元组类型 t2=(23) type(t2) <class ‘int’> #整型 t3=(23,) type(t3) <class ’tuple’> #我就一组数据,在数据的后面加逗号的方式定义成元组 a,b,c=(1,2,3) q a 1 b 2 c 3 t1=(“1”,“2”) hostname,ip=t1 hostname ‘1’ ip ‘2’ 元组中的数据不可变,元组创建成功后,不可以追加或者删除数据。 如果希望可变: 列表:弥补元组的不足(元组定义好后不可以改变),用中括号 l1=[“1”,“2”] #列表 t1=(“1”,“2”) #元组 l1.append(“00:54:52:68”) #列表追加数据 l1 [‘1’, ‘2’, ‘00:54:52:68’] l1.remove(“00:54:52:68”) #删除列表内的数据 l1 [‘1’, ‘2’] l1 [‘1’, ‘2’] l1[1] ‘2’ l1[1]=‘172.24.16.18’ l1[1] ‘172.24.16.18’ #修改列表内的数据 l1 [‘1’, ‘172.24.16.18’] l1.remove(l1[1]) l1 [‘1’] #删除列表内的数据 列表的不足:平时编写程序的时候或者引用数据的时候,程序不知道你写的数据意味着什么 字典:为了解决列表的不足,继承了列表所有的有点,同时在字典里用冒号进行分割一组的数据类型 dic1={“age”:23,“tel”:133333,“name”:“frank”} type(dic1) <class ‘dict’> #字典类型 dic1[“age”] 23 #用这个方式索引值 for x in dic1: … print(x) … age tel name #取里面的key for x in dic1: … print(dic1[x]) … 23 133333 frank #取里面的值 dic1[“home”]=“changchun” dic1 {‘age’: 23, ’tel’: 133333, ’name’: ‘frank’, ‘home’: ‘changchun’} #追加key和值 dic1[“home”]=“chengdu” dic1 {‘age’: 23, ’tel’: 133333, ’name’: ‘frank’, ‘home’: ‘chengdu’} #修改值 del(dic1[“home”]) dic1 {‘age’: 23, ’tel’: 133333, ’name’: ‘frank’} #删除字典里的数据 dic1.clear() dic1 {} #清空里面的数据结构,但是字典还在 del(dic1) #彻底删除字典 for z in “abcde”: #z的位置随便写,叫做迭代变量 … print(“hello world”) for x in range(10): #range是整型,10是从0-9, … print(“hello world,"+str(x)) for x in range(1,10): #从1开始到9,不包含10 … print(“hello world,"+str(x)) for x in range(1,10,2): #从1到9,2步长也就是每隔两个输出 … print(“hello world,"+str(x)) for x in [0,1,2,3,4,5,6,7,8,9]: #和range10同理 … print(“hello world,"+str(x))

root@ubuntu:~# for x in {1..10};do echo "hello world,$x";done #shell中这么写for循环

10-15 函数:一段代码段,目的:反复重复使用的代码段

def fun1(): #定义函数 … print(“hello world”) … fun1() #调用函数 hello world 函数的传参: def fun2(x): #x表示形式参数 … print(x) … fun2(“abc”) #括号里写的什么出来什么,也就是实际参数。 abc 函数内部定义的变量,只能在函数内部使用,出了函数就没用了 def fun1(): … c=200 … print(“hello”) … print(c) #c没被定义过,其实在函数内被定义过,但是是临时的,出了函数就被清除掉 错误 可以把c升级为全局变量 def fun1(): … global c #先声明把c设置为全局变量 … c=200 #再定义c … print(“hello”) … fun1() hello print(c) 200 函数的返回值: Print打印到屏幕上给人看的。return给下面的命令程序看,理解为一个标签也可以。 #coding:utf8 print("\n 填数字游戏,请填写两个数字并且,第一个要大于第二个\n”) print(”#"*50) s1 = int(input(‘input the first num: ‘)) s2 = int(input(‘input the second num: ‘)) def fun(x,y): if x > y: print(f”{x} 大于 {y}”) #前面加一个f表示{}不是字典,是变量。 return 1 if x < y: print(f”{x} 小于 {y}”) return -1 if x == y: print(f"{x} 等于 {y}") return 0 if fun(s1,s2) == 1: print(" 对的,是我要的结果!") else: print(“不是我要的结果!”) ————————————————————————保存退出

root@ubuntu:~# python3 fun_test.py #执行python文件。

图片

函数的值:多传值,少传值。 Lambda提供一套快速的算法,两套形参的乘积

from functools import reduce #导入reduce函数 reduce(lambda x,y:x*y,range(1,11)) #reduce的用法 3628800 reduce(lambda x,y:x+y,range(1,101)) 5050 基础内置函数 abs() min() max() len() divmod() pow() round() callable() cmp() range() xrange() str() list() tuple() int() 用法课参照 help() str1=“hello world!” str1.capitalize() #行首字母大写 ‘Hello world!’ replace:替换字符串 str1.replace(‘hello’,‘good’) ‘good world!’ str1=“hello world! hello hello” str1.replace(‘hello’,‘good’) ‘good world! good good’ #贪婪替换,全都替换了 str1.replace(‘hello’,‘good’,1) ‘good world! hello hello’ #单独把匹配的第一个hello替换成good str1.replace(‘hello’,‘good’,2) ‘good world! good hello’ #单独把匹配的前两个hello替换成good Split分割 str1.split() [‘hello’, ‘world!’, ‘hello’, ‘hello’] #默认以空格分割单位的,没有空格不分 ip.split(’.’) [‘192’, ‘168’, ‘122’, ‘43’] #以.进行分割 ip.split(’.’,1) [‘192’, ‘168.122.43’] #分割前一位 ip.split(’.’)[-1] ‘43’ #取最后一位 Strip去除行首行尾的空格 line1=" hello test good " line1.strip() ‘hello test good’ line1.lstrip() ‘hello test good ’ #去除行首的空格 line1.rstrip() ’ hello test good’ #去除行尾的空格 获取当前以太网网卡的MAC地址 os.system(‘ip a s’) #调用系统的命令 os.system(‘ip a s ens33’) 怎么提取信息中想要的: import os ens33=os.popen(“ip a s ens33”).read() #提取数据中的MAC地址,00:0c:29:51:63:d3 type(ens33) print(ens33) ens33.split() ens33.split()[14] ‘00:0c:29:51:63:d3’ 截获: mac=ens33.split()[14] print(mac) 00:0c:29:51:63:d3 #此时mac就提取出来了。 实验:

root@ubuntu:~# vim os-split-ip.py

图片

查询网卡的ip地址。

root@ubuntu:~# python3 os-split-ip.py

您要查询的网卡:ens33,您要查询的ip地址:172.24.126.134/24 对于文件的处理: String模块:

import string Python对于内存的使用:深拷贝,浅拷贝。 浅拷贝:共用内存地址,前一个变了,第二个跟着变。 文件的读取和写入: ipaddr=open(’/tmp/ipaddr.txt’,‘r’) print(ipaddr) type(ipaddr) ipaddr.read() #读取文件内容 ipaddr.read() ’’ #为什么为空,因为有指针概念,第一次读取到尾部,读取会慢慢读到最后。 ipaddr.readline() #以行为单位读取 ipaddr.readlines() #多行读取 fo #句柄,

root@ubuntu:~# ifconfig #安装包

Python 源文件经编译后生成的扩展名为“pyc”的文件;

root@ubuntu:~/py# python3 -m py_compile read_write-6.py

�root@ubuntu:~/py# python3 pycache/read_write-6.cpython-310.pyc 经过优化的源文件,扩展名为“.pyo”

root@ubuntu:~/py# python3 -O -m py_compile read_write-6.py

fun_add.py 定义个相加算法

root@ubuntu:~/py# cd /python/
root@ubuntu:/python# vim fun_add.py

#!/usr/bin/python def add(x,y): return x+y print(add(3,5))

root@ubuntu:/python# python3

sys.path.append("/python") #导入python中 import fun_add 模块: 写好的代码封装成一个.py文件,如果希望在不通的场景中反复使用这段代码,可以通过模块导入的方式反复调用该段代码。 步骤: 添加到模块搜索路径中: import sys sys.path.append(模块存放路径) 导入该模块(直接指定文件的名字) Import 文件的名字 包: 功能相近或有特殊意义一组模块,存放在一个文件夹中,并包含__init__.py文件的文件夹。 代码组织的方式: 函数(同应用中反复使用) < 模块(一个功能一个模块,包含多个函数) < 包(文件夹,一组相近或关联的模块的集合) #就想自己去调用函数的值

图片

import sys sys.path.append("/python") import fun_add #交互式python就找不到定义的3,5了 fun_add.add(10,20) 30 #可以这样调用函数写别的