【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]
相关推荐
Rose 使者9 分钟前
今日实时汇率查询接口如何用Python实现调用?
python·api·汇率查询
惜鸟13 分钟前
Pydantic 的 BaseSettings 配置解析
后端·python
MYH51622 分钟前
拉力测试cuda pytorch 把 4070显卡拉满
人工智能·pytorch·python
某人辛木27 分钟前
基于tensorflow实现的猫狗识别
人工智能·python·tensorflow
大白爱琴29 分钟前
使用python进行图像处理—图像变换(6)
图像处理·人工智能·python
月忆36442 分钟前
等待组(waitgroup)
前端·爬虫·python
一个何包蛋!!1 小时前
相关类相关的可视化图像总结
开发语言·python·数据可视化
技术便签1 小时前
第一篇:Agent2Agent (A2A) 协议——协作式人工智能的黎明
人工智能·python·ai编程·agi·多智能体·智能体·adk
love530love1 小时前
教程:PyCharm 中搭建多级隔离的 Poetry 环境(从 Anaconda 到项目专属.venv)
开发语言·ide·人工智能·windows·python·pycharm
Ian在掘金1 小时前
bat+python实现easy connect自动连接
前端·python