【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]
相关推荐
欢乐马87911 小时前
优化|MPSolver 和 pywraplp 到底谁是谁?一次讲清 OR-Tools 的三层结构
python
mengge.cloud2 小时前
Python基础保姆级!从入门到入土
开发语言·python·云原生·pygame
qq_158895411116 小时前
Suno-API 是什么?官方 V6 模型与 Python / TypeScript 接入指南
python·ai音乐·suno api
学无止境小袁同学17 小时前
Google ADK 学习指南
python·llm·agent·多agent·google adk·llmagent·agent cli
郝学胜-神的一滴17 小时前
Effective Python 条款35:用类封装替代生成器异常注入
开发语言·人工智能·python·金融·pandas·scikit-learn·matplotlib
LK_0718 小时前
【力扣】142. 环形链表 II |Python
python·leetcode·链表
wuyk55518 小时前
Python办公自动化-第02讲-Excel文件认知与四大库选型
开发语言·python·excel
用户548178459095019 小时前
Java 与 Python 在大模型与智能体领域的选型
java·python
yiangL_19 小时前
1、Python 输入输出实验|print 与 input 人机交互实践
开发语言·python·人机交互