【Python】OJ 常用函数

这里写目录标题

  • [一. math](#一. math)
    • [1. 求阶乘 - factorial()](#1. 求阶乘 - factorial())
    • [2. 绝对值 - fabs()](#2. 绝对值 - fabs())
  • [二. 容器的方法](#二. 容器的方法)
    • [1. reverse()](#1. reverse())
  • [三. Python 内置函数](#三. Python 内置函数)
    • [1. sort()](#1. sort())

一. math

需要引入 math 包:import math

1. 求阶乘 - factorial()

cpp 复制代码
import math
print(math.factorial(5))

--------运行结果--------
120

2. 绝对值 - fabs()

cpp 复制代码
import math
print(math.fabs(-5))

--------运行结果--------
5

二. 容器的方法

1. reverse()

reverse 是列表的一种内置方法,用于列表中数据的反转:

cpp 复制代码
list = [1, 2, 3, 4, 5]
list.reverse()
print(list)

--------运行结果--------
[5, 4, 3, 2, 1]

三. Python 内置函数

1. sort()

可以对列表中数据进行排序:

cpp 复制代码
ls = [1, -1, 3, 88]
list.sort()
print(list)

--------运行结果--------
[-1, 1, 3, 88]
相关推荐
论文复现现场13 小时前
ComfyUI 怎么同时调用 4 张/8 张 RTX 4090?AI 视频批量生成的多实例队列与 Python 调度方案
人工智能·python·comfyui·rtx4090
泡海椒13 小时前
评分系统最佳实践:JQuick-Java实现权重、阈值动态配置评分
java·人工智能·python
weixin1997010801613 小时前
《二手ERP对接的对账机制:按日巡检 + 自动补偿,消灭“幽灵订单“》(附Python源码)
python
m0_5474866613 小时前
《Python数据分析与实践》全套PPT课件(杭州电子科技大学)
python·数据分析
OKkankan13 小时前
Python 基础进阶(三):从函数、类到 asyncio 与 FastAPI 后端开发实战
开发语言·python
智购科技自动售卖机厂家13 小时前
设备一到夏天就频繁跳闸,从启动电流追到压缩机电容~YH
数据结构·人工智能·python·eclipse
科技林总13 小时前
Poetry‌介绍
python
东木月14 小时前
PyCharm 集成 Claude Code 插件:安装、配置与使用指南
python·pycharm·编辑器
jerryinwuhan15 小时前
Python快速入门(纯净版)
python
liferecords15 小时前
第 12 讲 · Python 侧接入:火焰图定位热点与间接使用硬件加速
python·性能分析·鲲鹏·火焰图·ctypes