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
相关推荐
2501_930707782 小时前
使用C#代码在 PowerPoint 中组合或取消组合形状
开发语言·c#
晚烛3 小时前
CANN 调试工具与性能剖析:从日志分析到 NPU 行为追踪的完整调试体系
开发语言·windows·python·深度学习·缓存
惊鸿一博3 小时前
图标加载方式_zeroIcon_是否加前缀mdi
开发语言·前端·javascript
森G3 小时前
TypeScript 基础类型
开发语言·typescript
huipeng9264 小时前
企业级微服务开发实战(一):项目启动与工程化设计
java·开发语言·spring boot·spring cloud·微服务·云原生·架构
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ4 小时前
java实现excel导入、下载模板方法
java·开发语言·excel
眠りたいです5 小时前
现代C++:C++14中的新语言特性和库特性
c语言·开发语言·c++
隔壁大炮5 小时前
MNE-Python 第9天学习笔记:源定位基础
python·eeg·mne·脑电数据处理
独隅5 小时前
Android Studio 接入 CodeX 的全面指南
android·ide·android studio
叶小鸡5 小时前
Java 篇-项目实战-AI 天机学堂(从 0 到 1)-day1
java·开发语言