常用运算+-*/、max()、pow()、ord()、divmod()

这篇博客介绍了Python中的一些基本运算,包括加减乘除以及整除和四舍五入。此外,还讨论了max()函数用于找到列表中的最大值,pow()函数用于求幂并支持取余运算,以及divmod()函数用于同时获取除法的商和余数。对于四舍五入,特别提到了0.5时不会向上进位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常用运算:加、减、乘、除(取小数)、整除(地板除//);

>>> 1+1
2
>>> 1-1
0
>>> 1*2
2
>>> 1/2
0.5
>>> 1//2
0
>>> 9//10	        #底板除,向下取
0

整除(int());round(内置函数)四舍五入0.5的时候要注意并没有入一位

>>> int(1/3)
0
>>> int(9/10)
0
>>> round(0.9,0)
1.0
>>> round(0.5,0)
0.0
>>> round(0.51,0)
1.0

除法保留指定小数位数

>>> round(1/3,2)
0.33
取余%
>>> 4%3
1
同时取商和余数
>>> divmod(4,3)
(1, 1)

添加元组,取第0位的值,或者第1位的值

>>> divmod(9,4)[1]
1
>>> divmod(9,4)[0]
2
>>>

max() 求一个最大值的方法

>>> max([1,2,3,43,45])
45

pow() 两个参数下是a的b次方;三个参数下是a的b次方除以c取余数

>>> import math					#15.导入库,库名叫math
>>> math.pow(2,3)
8.0
>>>
pow(x, y, z=None, /)
    Equivalent to x**y (with two arguments) or x**y % z (with three arguments)

    Some types, such as ints, are able to use a more efficient algorithm when
    invoked using the three argument form.

>>> pow(2,3)		#2的3次方
8
>>> pow(2,3,2)	#2的3次方除以2的余数
0
>>> pow(2,3,3)	#2的3次方除以3的余数
2

>>> math.sqrt(4)			#开平方
2.0
>>> math.sqrt(8)
2.8284271247461903
>>>
>>> math.pi			#π
3.141592653589793
# math.pow和pow不是一个东西,内置的pow有三个参数,math.pow只有两个参数。

>>> ord("a")           #ord把一个字母或者字符串算出一个unicode数字,可逆
97
>>> chr(97)
'a'
# ascii码256个字符128个常见字符,A-Z 65~90  a-z 97~122;a>A,比的是Ascii,不记得可以用ord("  ")算一下。

divmod()

>>> divmod(11,2)	#divmod(11除以2)返回(商,余数)
(5, 1)
>>> divmod(11,2)[1]	#取索引1位置的值--余数
1
>>> divmod(11,2)[0]	#取索引2位置的值--商
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值