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

 更新时间:2022年09月15日 17:02:33   作者:海拥  
Python 在“运算符”模块下为许多数学、逻辑、关系、按位等操作预定义了函数。本文介绍了一些基本功能,感兴趣的小伙伴可以跟随小编一起学习一下

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运算符函数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python创建或生成列表的操作方法

    Python创建或生成列表的操作方法

    在本文中我们给大家分享了关于Python创建或生成列表的操作方法以及步骤图文流程,需要的朋友们学习下。
    2019-06-06
  • Python实用技巧之如何获取后缀名(扩展名)或文件名

    Python实用技巧之如何获取后缀名(扩展名)或文件名

    这篇文章主要介绍了在Python中获取文件名和扩展名的几种方法,包括使用os.path.basename()函数获取文件名,以及使用os.path.splitext()函数获取文件名和扩展名,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-12-12
  • 用python实现面向对像的ASP程序实例

    用python实现面向对像的ASP程序实例

    这篇文章主要介绍了用python实现面向对像的ASP程序,实例讲述了使用Python实现ASP程序的方法,具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-11-11
  • Python利用matplotlib绘制折线图的新手教程

    Python利用matplotlib绘制折线图的新手教程

    这篇文章主要给大家介绍了关于Python利用matplotlib绘制折线图的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • tensorflow使用CNN分析mnist手写体数字数据集

    tensorflow使用CNN分析mnist手写体数字数据集

    这篇文章主要介绍了tensorflow使用CNN分析mnist手写体数字数据集,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06
  • 删除PyCharm解释器的方法步骤

    删除PyCharm解释器的方法步骤

    这篇文章主要给大家介绍了关于删除PyCharm解释器的方法步骤,PyCharm解释器是指在PyCharm集成开发环境中用于运行和调试Python代码的解释器,需要的朋友可以参考下
    2023-09-09
  • Python操作PDF实现制作数据报告

    Python操作PDF实现制作数据报告

    Python操作PDF的库有很多,比如PyPDF2、pdfplumber、PyMuPDF等等。本文将利用FPDF模块操作PDF实现制作数据报告,感兴趣的小伙伴可以尝试一下
    2022-12-12
  • python如何实现单向链表及单向链表的反转

    python如何实现单向链表及单向链表的反转

    这篇文章主要介绍了python如何实现单向链表及单向链表的反转,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下
    2021-03-03
  • Python基础知识之推导式详解

    Python基础知识之推导式详解

    这篇文章主要介绍了Python基础知识之推导式详解,Python推导式是一种简洁高效的代码编写方式,可以用一行代码来创建列表、集合、字典等复杂数据结构,需要的朋友可以参考下
    2023-07-07
  • Python中bisect的使用方法

    Python中bisect的使用方法

    这篇文章主要介绍了Python中bisect的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12

最新评论