广告
返回顶部
首页 > 资讯 > 后端开发 > Python >深入了解Python中运算符函数的使用
  • 728
分享到

深入了解Python中运算符函数的使用

2024-04-02 19:04:59 728人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

python 在“运算符”模块下为许多数学、逻辑、关系、按位等操作预定义了函数。本文介绍了一些基本功能。 1. add(a, b)  :- 这个函数

python 在“运算符”模块下为许多数学、逻辑、关系、按位等操作预定义了函数。本文介绍了一些基本功能。

1. add(a, b)  :- 这个函数返回给定参数的加法

操作 - a + b。

2. sub(a, b)  :- 此函数返回给定参数的差异

操作 - a - b。

3. mul(a, b)  :- 这个函数返回给定参数的乘积

操作 - a * b。

# 演示 add()、sub()、mul() 工作的 Python 代码

# importing operator module
import operator

# 初始化变量
a = 4

b = 3

# 使用 add() 将两个数字相加
print ("The addition of numbers is :",end="");
print (operator.add(a, b))

# 使用 sub() 减去两个数字
print ("The difference of numbers is :",end="");
print (operator.sub(a, b))

# 使用 mul() 将两个数字相乘
print ("The product of numbers is :",end="");
print (operator.mul(a, b))

输出:

The addition of numbers is:7
The difference of numbers is :1
The product of numbers is:12

4. truediv(a,b)  :- 这个函数返回给定参数的除法

操作 - a / b。

5. floordiv(a,b)  :- 此函数还返回给定参数的除法。但该值是下限值,即返回最大的小整数

操作 – a // b。

6. pow(a,b)  :- 这个函数返回给定参数的

操作 – a ** b.

7. mod(a,b)  :- 这个函数返回给定参数的模数。操作 – a % b.

# 演示 truediv()、floordiv()、pow()、mod() 工作的 Python 代码

# importing operator module
import operator

# 初始化变量
a = 5

b = 2

# 使用 truediv() 将两个数字相除
print ("The true division of numbers is : ",end="");
print (operator.truediv(a,b))

# 使用 floordiv() 将两个数字相除
print ("The floor division of numbers is : ",end="");
print (operator.floordiv(a,b))

# 使用 pow() 对两个数字求幂
print ("The exponentiation of numbers is : ",end="");
print (operator.pow(a,b))

# 使用 mod() 取两个数的模
print ("The modulus of numbers is : ",end="");
print (operator.mod(a,b))

输出:

The true division of numbers is: 2.5
The floor division of numbers is: 2
The exponentiation of numbers is: 25
The modulus of numbers is: 1

8. lt(a, b)  :- 此函数用于检查 a 是否小于 b。如果 a 小于 b,则返回 true,否则返回 false。

操作 - a < b

9. le(a, b)  :- 此函数用于检查 a 是否小于或等于 b。如果 a 小于或等于 b,则返回 true,否则返回 false。

操作 - a <= b

10. eq(a, b)  :- 此函数用于检查 a 是否等于 b。如果 a 等于 b,则返回 true,否则返回 false。

操作 - a == b

# 演示 lt()、le() 和 eq() 工作的 Python 代码

# importing operator module
import operator

# 初始化变量
a = 3

b = 3

# 使用 lt() 检查 a 是否小于 b
if(operator.lt(a,b)):
	print ("3 is less than 3")
else : print ("3 is not less than 3")

# 使用 le() 检查 a 是否小于或等于 b
if(operator.le(a,b)):
	print ("3 is less than or equal to 3")
else : print ("3 is not less than or equal to 3")

# 使用 eq() 检查 a 是否等于 b
if (operator.eq(a,b)):
	print ("3 is equal to 3")
else : print ("3 is not equal to 3")

输出:

3 is not less than 3
3 is less than or equal to 3
3 is equal to 3

11. gt(a,b)  :- 此函数用于检查 a 是否大于 b。如果 a 大于 b,则返回 true,否则返回 false。

操作 - a > b

12. ge(a,b)  :- 此函数用于检查 a 是否大于或等于 b。如果 a 大于或等于 b,则返回 true,否则返回 false。

操作 - a >= b

13. ne(a,b)  :- 此函数用于检查 a 是否不等于 b 或是否相等。如果 a 不等于 b,则返回 true,否则返回 false。

操作 - a != b

# 演示 gt()、ge() 和 ne() 工作的 Python 代码

# importing operator module
import operator

# 初始化变量
a = 4

b = 3

# 使用 gt() 检查 a 是否大于 b
if (operator.gt(a,b)):
	print ("4 is greater than 3")
else : print ("4 is not greater than 3")

# 使用 ge() 检查 a 是否大于或等于 b
if (operator.ge(a,b)):
	print ("4 is greater than or equal to 3")
else : print ("4 is not greater than or equal to 3")

# 使用 ne() 检查 a 是否不等于 b
if (operator.ne(a,b)):
	print ("4 is not equal to 3")
else : print ("4 is equal to 3")

输出:

4 is greater than 3
4 is greater than or equal to 3
4 is not equal to 3

到此这篇关于深入了解Python中运算符函数的使用的文章就介绍到这了,更多相关Python运算符函数内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 深入了解Python中运算符函数的使用

本文链接: https://www.lsjlt.com/news/120480.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作