【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]
相关推荐
qq_589568102 分钟前
现代 Linux 系统(如 Ubuntu 24.04、Debian 12+) pip 安装第三方包报错解决
python·ubuntu
大数据三康26 分钟前
在spyder进行的遗传算法练习
开发语言·python·算法
njsgcs1 小时前
我仓库内cad python 有哪些应用到聚类的方法
开发语言·python·聚类
web3.08889992 小时前
电商数据化运营:速卖通API+Python打造竞品监控与选品利器
开发语言·python
good good study"3 小时前
PyCharm 添加 Conda 环境报错 “lateinit property envs_dirs has not been initialized“ 的完美解决
python·conda
我的xiaodoujiao3 小时前
API 接口自动化测试详细图文教程学习系列21--结合Pytest框架使用2--断言和插件
python·学习·测试工具·pytest
码智社3 小时前
Python安装配置超详细教程(Windows+macOS,新手零踩坑)
windows·python·macos
wuxinyan1233 小时前
工业级大模型学习之路026:LangGraph 入门与基础 Agent 开发
人工智能·python·学习·langsmith
AIFQuant4 小时前
量化交易系统:历史行情 API 批量拉取与回测数据清洗
开发语言·python·金融·restful·量化交易