【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]
相关推荐
cup111 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi004 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵5 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf6 小时前
Agent 流程编排
后端·python·agent
copyer_xyf7 小时前
Agent RAG
后端·python·agent
copyer_xyf7 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf7 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵1 天前
用 Pygame 实现 15 puzzle
python·数学·游戏