day14-Python运维开发基础(内置函数、pickle序列化模块、math数学模块)
1. 内置函数

# ### 内置函数
# abs 绝对值函数
res = abs(-10)
print(res) # round 四舍五入 (n.5 n为偶数则舍去 n.5 n为奇数,则进一!) 奇进偶不进
res = round(13.56)
res = round(4.5)
res = round(5.5)
res = round(4.53)
res = round(4.9)
print(res) # sum 计算一个序列得和
tup = (1,2,3,43,5,6,6)
res = sum(tup)
print(res) # max 获取一个序列里边的最大值
tup = (1,2,3,43,5,6,6)
res = max(tup)
print(res) # max的高阶函数使用
def func(n):
# print(n) # 参数是一个个的元组 ('林明辉', 58)
return n[1] # 33 58 99 -8 lst = [("常远",33),("林明辉",58),("李德亮",99),("李诗韵",-8)]
res = max(lst,key=func)
print(res) # min 获取一个序列里边的最小值
tup = (1,2,3,43,5,6,6)
res = min(tup)
print(res) # min的高阶函数使用
dic = {"任鹏伟":60,"宗永玲":59,"黄乐锡":90,"李诗韵":-7}
def func(n):
# print(n) # 参数是字典的键
return dic[n] # 通过键返回值 , 通过值排序,找出最小值对应的键 res = min(dic,key=func)
print(res) # pow 计算某个数值的x次方
res = pow(2,3)
"""前两个数运算的值 在和第三个数取余"""
res = pow(2,3,3)
# res = pow(2,3,4)
print(res) # range 产生指定范围数据的可迭代对象
for i in range(3):
print(i) for i in range(1,5):
print(i) for i in range(1,10,3):
print(i) # bin 将10进制数据转化为二进制
res = bin(255)
print(res) # oct 将10进制数据转化为八进制
res = oct(255)
print(res) # hex 将10进制数据转化为16进制
res = hex(255)
print(res) # chr 将ASCII编码转换为字符
res = chr(97)
print(res)
# ord 将字符转换为ASCII编码
res = ord("a")
print(res) # eval 将字符串当作python代码执行
strvar = "print('我是大帅锅')"
print(strvar)
eval(strvar) # eval 有局限性,不能创建变量的
# strvar = "a=5"
# eval(strvar)
# exec 将字符串当作python代码执行(功能更强大) 谨慎使用,存在安全隐患
strvar = "a='文哥真帅!'"
exec(strvar)
print(a) """
# 复习
dic = globals()
print(dic)
dic["wangwen"] = "宇宙第一男人"
print(wangwen)
"""
strvar = """
for i in range(5):
print(i)
"""
exec(strvar) # repr 不转义字符输出字符串
strvar = "D:\nabc"
res = repr(strvar)
print(res) # input 接受输入字符串
# name = input("先森,你妈贵姓?")
# print(name) # hash 生成哈希值
"""
(1) 可以加密密码
(2) 哈希值可以校验文件
"""
# 相同的字符串,无论哈希多少次,都是相同的哈希值
strvar1 = "abc"
strvar2 = "abc"
res1 = hash(strvar1)
res2 = hash(strvar2)
print(res1,res2) with open("ceshi1.txt",mode="r+",encoding="utf-8") as fp1 , open("ceshi2.txt",mode="r+",encoding="utf-8") as fp2:
res = fp1.read()
res2 = hash(res) res = fp2.read()
res3 = hash(res)
if res2 == res3 :
print("两者文件内容相同")
else:
print("两者文件不相同")
内置函数 示例代码
2. pickle序列化模块

# ### pickle 序列化模块
"""
序列化: 把不能够直接存储到文件的数据变得可存储,这个过程就是序列化
反序列化:把存储的数据拿出来,恢复成原来的数据类型,这个过程就是反序列化 php: (了解)
serialize 序列化
unserialize 反序列化 pickle 可以序列化所有的数据类型
"""
import pickle
# 文件当中,只能存储字符串 或 二进制字节流 ,其他不行
"""
lst = [1,2,34,5]
with open("ceshi.txt",mode="w",encoding="utf-8") as fp:
fp.write(lst)
"""
# (1)容器类型数据可以序列化
lst = [1,2,34,5]
#dumps 把任意对象序列化成一个bytes
res = pickle.dumps(lst)
print(res) #loads 把任意bytes反序列化成原来数据
lst = pickle.loads(res)
print(lst,type(lst)) # (2)函数可以序列化
def func():
print("我是函数func") #dumps 把任意对象序列化成一个bytes
res = pickle.dumps(func)
print(res)
#loads 把任意bytes反序列化成原来数据
func = pickle.loads(res)
print(func,type(func))
func() # (3)迭代器可以序列化
it = iter(range(5))
# 序列化迭代器
res = pickle.dumps(it)
# 反序列化恢复原来的数据类型
it2 = pickle.loads(res)
print(it2)
from collections import Iterator,Iterable
res = isinstance(it2,Iterator)
print(res) # 获取迭代器中的数据
for i in range(3):
res = next(it2)
print(res) for i in it2:
print(i) # 方法一
#dump 把对象序列化后写入到file-like Object(即文件对象)
lst = [1,2,3]
with open("ceshi.txt",mode="wb") as fp:
pickle.dump(lst,fp) #load 把file-like Object(即文件对象)中的内容拿出来,反序列化成原来数据
with open("ceshi.txt",mode="rb") as fp:
res = pickle.load(fp)
print(res,type(res)) # 方法二
# 用dumps 和 loads 对数据进行存储
lst = [1,2,3]
# 写入
with open("ceshi4.txt",mode="wb") as fp:
res = pickle.dumps(lst)
fp.write(res)
# 读取
with open("ceshi4.txt",mode="rb") as fp:
res = fp.read()
lst = pickle.loads(res)
print(lst)
pickle序列化模块 示例代码
3. math 数学模块

# ### math 数学模块
import math
#ceil() 向上取整操作 (对比内置round)
res = math.ceil(4.1111)
print(res) #floor() 向下取整操作 (对比内置round)
res = math.floor(6.9999)
print(res) #pow() 计算一个数值的N次方(结果为浮点数) (对比内置pow)
res = math.pow(2,3)
# res = math.pow(2,3,3) error math中pow方法只有2个参数;
print(res) #sqrt() 开平方运算(结果浮点数)
res = math.sqrt(9)
print(res) #fabs() 计算一个数值的绝对值 (结果浮点数) (对比内置abs)
res = math.fabs(-8)
print(res) #modf() 将一个数值拆分为整数和小数两部分组成元组
res = math.modf(7.81)
print(res) # (0.8099999999999996, 7.0) #copysign() 将参数第二个数值的正负号拷贝给第一个 (返回一个小数)
res = math.copysign(-18,-9)
print(res) #fsum() 将一个容器数据中的数据进行求和运算 (结果浮点数)(对比内置sum)
lst = [1,2,3,45]
res = math.fsum(lst)
print(res) #圆周率常数 pi
res = math.pi
print(res)
math 模块函数 示例代码
day14
day14-Python运维开发基础(内置函数、pickle序列化模块、math数学模块)的更多相关文章
- Python运维开发基础09-函数基础【转】
上节作业回顾 #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 实现简单的shell命令sed的替换功能 import ...
- Python运维开发基础03-语法基础 【转】
上节作业回顾(讲解+温习60分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen #只用变量和字符串+循环实现“用户登陆 ...
- Python运维开发基础02-语法基础【转】
上节作业回顾(讲解+温习60分钟) #!/bin/bash #user login User="yunjisuan" Passwd="666666" User2 ...
- Python运维开发基础10-函数基础【转】
一,函数的非固定参数 1.1 默认参数 在定义形参的时候,提前给形参赋一个固定的值. #代码演示: def test(x,y=2): #形参里有一个默认参数 print (x) print (y) t ...
- Python运维开发基础08-文件基础【转】
一,文件的其他打开模式 "+"表示可以同时读写某个文件: r+,可读写文件(可读:可写:可追加) w+,写读(不常用) a+,同a(不常用 "U"表示在读取时, ...
- Python运维开发基础07-文件基础【转】
一,文件的基础操作 对文件操作的流程 [x] :打开文件,得到文件句柄并赋值给一个变量 [x] :通过句柄对文件进行操作 [x] :关闭文件 创建初始操作模板文件 [root@localhost sc ...
- Python运维开发基础06-语法基础【转】
上节作业回顾 (讲解+温习120分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 添加商家入口和用户入口并实现物 ...
- Python运维开发基础05-语法基础【转】
上节作业回顾(讲解+温习90分钟) #!/usr/bin/env python # -*- coding:utf-8 -*- # author:Mr.chen import os,time Tag = ...
- Python运维开发基础04-语法基础【转】
上节作业回顾(讲解+温习90分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 仅用列表+循环实现“简单的购物车程 ...
随机推荐
- composer基本命令
安装:https://getcomposer.org/download/ { "require":{ // "厂商/类库":"版本号", & ...
- 201771010135杨蓉庆 《面对对象程序设计(java)》第八周学习总结
1.实验目的与要求 (1) 掌握接口定义方法: (2) 掌握实现接口类的定义要求: (3) 掌握实现了接口类的使用要求: (4) 掌握程序回调设计模式: (5) 掌握Comparator接口用法: ( ...
- 22 严格模式&this关键词&let&const
严格模式: ECMA5后的新指令:"use strict" 它不算一条语句,而是一段文字表达式,更早版本的JavaScript会忽略它. 严格模式无法使用未声明的变量. 严格模式的 ...
- vs2017 vs2019配置sqlite3连接引擎(驱动)指南(二)vs2019续集
在写完上一篇博客后,一觉醒来,又又又又不行了,介绍一个终极大招,如果你的fuck vs又提示无法打开sqlite3.h的问题 环境win10 vs2019 debug x86 实在没心情写文字了,直 ...
- docker环境下mysql数据库的备份
#! /bin/bash DATE=`date +%Y%m%d%H%M%S` BACK_DATA=erp-${DATE}.sql #导出表结构,不包括表数据 #docker exec -i xin-m ...
- HDU1875 畅通工程再续
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全 ...
- 标定设备自动化-ASAP3
欢迎关注<汽车软件技术>公众号,回复关键字获取资料. 1.ASAP3定义 下图选自INCA文档<INCA_IF_ASAM-ASAP3_EN.pdf>说明了ASAP3的用途:标定 ...
- Windows 安装python虚拟环境
windows 安装pytho虚拟环境 方法一:virtualenv (1)使用pip安装virtualenv工具 pip install virtualenv (2)使用virtualenv创建虚拟 ...
- Do You Know These Plastic Bottle Processing Terms?
The molding process of a plastic bottle refers to a process of making a final plastic article from a ...
- GO测试
测试 Go拥有一个轻量级的测试框架,它由 go test 命令和 testing 包构成. 你可以通过创建一个名字以 _test.go 结尾的,包含名为 TestXXX 且签名为 func (t *t ...