blog/docs/code/python/num-func.md

130 lines
1.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 数学函数
date: 2020-07-20 17:09:41
tags: [Python]
categories: [Python]
author: Anges黎梦
---
## abs()绝对值
> 功能:返回数字的绝对值
示例:
```Python
# 取列表中每一个元素的绝对值
test_list = [12.45,0,-19.69]
for i in test_list:
print(abs(i))
```
执行结果:
```Python
12.45
0
19.69
```
## divmod()获取商和余数的元组
> 功能把除数和余数运算结果结合起来返回一个包含商和余数的元组a//ba%b
示例:
```Python
# a:被除数 b:除数, 9除以2
a = divmod(9, 2)
print(a)
```
执行结果:
4为整数1为余数
```Python
(4, 1)
```
## sum()求和计算
> 功能:函数对列表元组和集合等序列进行求和计算
示例:
```Python
a = sum([1, 2])
print(a)
```
执行结果:
```Python
3
```
`注参数iterable为一个可迭代对象。`
## round()四舍五入
> 功能:返回浮点数四舍五入的值
示例:
```Python
a = round(4.19)
b = round(4.99)
print(a)
print(b)
```
执行结果:
```Python
4
5
```
## pow()计算任意数n次方的值
> 功能返回x的y次方的值
示例:
```Python
a = pow(2, 3)
print(a)
```
执行结果:
```Python
8
```
## min()取出给定参数的最小值
> 功能:获取指定数值或者指定序列中最小值
示例:
```Python
a = min([47, 83, 45, 69, 32, 71, 74, 92, 58, 68])
print(a)
```
执行结果:
```Python
32
```
## max()取出给定参数的最大值
> 功能:获取指定数值或者指定序列中最大值
示例:
```Python
a = min([47, 83, 45, 69, 32, 71, 74, 92, 58, 68])
print(a)
```
执行结果:
```Python
92
```