Python基础(九)

文章目录

Python中的加密

hashlib

  • 难破解
  • 不可逆
常用加密方法

base64

python 复制代码
# coding:utf-8

import base64

def encode(data):
    if isinstance(data, str):
        data = data.encode('utf-8')
    elif isinstance(data, bytes):
        data = data
    else:
        raise TypeError('data need to be str or bytes')

    return base64.b64encode(data).decode('utf-8')


def decode(data):
    if not isinstance(data, bytes):
        raise TypeError('data need to be bytes')
    return base64.b64decode(data).decode('utf-8')

if __name__ == '__main__':
    result = encode('zhagnsan')
    print(result)                           # emhhZ25zYW4=
    print(decode(result.encode('utf-8')))   # zhagnsan

日志模块

日志的等级

  • debug
  • info
  • warnning
  • error
  • critical

loggin模块的使用

logging.basicConfig
format具体的格式

常用格式:

format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'

使用logging
python 复制代码
# coding:utf-8
import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')

logging.info('hello world') # 2025-12-30 16:04:35,657 logging_test.py[line:7] INFO hello world

Python内置函数总结



Python的随机模块---random

random.random

random.uniform

random.randint

random.choice

random.sample

random.randrange

python 复制代码
# coding:utf-8

import random

print(random.random()) # 0.31219675861853746

print(random.uniform(10,20)) # 17.623909031688694

print(random.randint(10,20)) # 15

print(random.choice([1,2,3,4,5,6])) # 5
print(random.choice('shdfkl')) # k

print(random.sample([1,2,3,4,5,6],2)) # [4, 2]

print(random.randrange(1,10, 2)) # 5

Python的迭代器

Python的迭代器是一个重要的概念,特别是在处理序列数据和流数据时。迭代器是一种可以逐一遍历集合中所有元素的对象

如何生成迭代器 -- iter

迭代器使用-- next

python 复制代码
# coding:utf-8

list_test = [1,2,3]

iter_test = iter(list_test)

print(next(iter_test)) # 1

迭代器的常用方法☞ 生成迭代器

for循环生成 -- yield
python 复制代码
def yield_test():
    for item in range(10):
        yield item

res = yield_test()
print(res, type(res)) # <generator object yield_test at 0x00000290513E15F0> <class 'generator'>
for循环一行生成迭代器
python 复制代码
res = (i for i in range(10))

迭代器常用方法 ☞ 佛如循环获取

python 复制代码
def yield_test():
    for item in range(10):
        yield item

res = yield_test()
print(res, type(res)) # <generator object yield_test at 0x00000290513E15F0> <class 'generator'>

for item in res:
    print(item, end=' ') # 0 1 2 3 4 5 6 7 8 9

Python中的高级函数

filter
功能

对循环根据过滤条件进行过滤

用法
python 复制代码
res = filter(lambda x: x > 1, [0,1,2,3,4,5])
print(list(res))  # [2, 3, 4, 5]
map
python 复制代码
res = map(lambda x: x > 1, [0,1,2,3,4,5])
print(list(res)) # [False, False, True, True, True, True]
reduce

对循环前后两个数据进行累加或者累乘等操作

python 复制代码
from functools import reduce
res = reduce(lambda x, y: x + y, [0,1,2,3,4,5])
print(res)  # 15
相关推荐
电摇小人2 小时前
我的“C++之旅”(博客之星主题作文)
java·开发语言
资生算法程序员_畅想家_剑魔2 小时前
Java常见技术分享-23-多线程安全-总结
java·开发语言
ytttr8732 小时前
MATLAB中CVX凸优化工具箱的使用指南
开发语言·matlab
大学生毕业题目2 小时前
毕业项目推荐:91-基于yolov8/yolov5/yolo11的井盖破损检测识别(Python+卷积神经网络)
python·yolo·目标检测·cnn·pyqt·井盖破损
萧曵 丶2 小时前
ArrayList 和 HashMap 自动扩容机制详解
java·开发语言·面试
gjc5922 小时前
MySQL隐蔽 BUG:组合条件查询无故返回空集?深度排查与规避方案
android·数据库·mysql·bug
这是程序猿2 小时前
基于java的ssm框架学生作业管理系统
java·开发语言·spring boot·spring·学生作业管理系统
XLYcmy3 小时前
TarGuessIRefined密码生成器详细分析
开发语言·数据结构·python·网络安全·数据安全·源代码·口令安全
weixin_433417673 小时前
Canny边缘检测算法原理与实现
python·opencv·算法