Python在“运算符”模块下为许多数学、逻辑、关系、位等操作提供了预定义函数。本文介绍了一些基本功能。
1.加入(a,b) :-此函数返回 附加 在给定的论点中。 操作—— a+b。
2.分包(a、b) :-此函数返回 差别 在给定的论点中。 操作—— a-b。
3.mul(a,b) :-此函数返回 产品 在给定的论点中。 操作—— a*b。
# Python code to demonstrate working of # add(), sub(), mul() # importing operator module import operator # Initializing variables a = 4 b = 3 # using add() to add two numbers print ( "The addition of numbers is :" ,end = ""); print (operator.add(a, b)) # using sub() to subtract two numbers print ( "The difference of numbers is :" ,end = ""); print (operator.sub(a, b)) # using mul() to multiply two numbers 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.特鲁迪夫(a、b) :-此函数返回 分开 在给定的论点中。 操作—— a/b。
5.楼面面积(a、b) :-此函数还返回给定参数的除法。但价值是最低价值,即。 返回最大的小整数 . 操作—— a//b。
6.战俘(a、b) :-此函数返回 指数化 在给定的论点中。 操作—— a**b。
7.国防部(a、b) :-此函数返回 模数 在给定的论点中。 操作—— a%b。
# Python code to demonstrate working of # truediv(), floordiv(), pow(), mod() # importing operator module import operator # Initializing variables a = 5 b = 2 # using truediv() to divide two numbers print ( "The true division of numbers is : " ,end = ""); print (operator.truediv(a,b)) # using floordiv() to divide two numbers print ( "The floor division of numbers is : " ,end = ""); print (operator.floordiv(a,b)) # using pow() to exponentiate two numbers print ( "The exponentiation of numbers is : " ,end = ""); print (operator. pow (a,b)) # using mod() to take modulus of two numbers 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 .
9.乐(a,b) :-此函数用于 检查a是否小于或等于b .如果a小于或等于b,则返回true,否则返回false。 操作—— a<=b .
10.等式(a,b) :-此函数用于 检查a是否等于b .如果a等于b,则返回true,否则返回false。 操作—— a==b .
# Python code to demonstrate working of # lt(), le() and eq() # importing operator module import operator # Initializing variables a = 3 b = 3 # using lt() to check if a is less than b if (operator.lt(a,b)): print ( "3 is less than 3" ) else : print ( "3 is not less than 3" ) # using le() to check if a is less than or equal to 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" ) # using eq() to check if a is equal to 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.通用电气(a、b) :-此函数用于 检查a是否大于或等于b .如果a大于或等于b,则返回true,否则返回false。 操作—— a>=b .
13.ne(a、b) :-此函数用于 检查a是否不等于b或是否等于 .如果a不等于b,则返回true,否则返回false。 操作—— a!=B .
# Python code to demonstrate working of # gt(), ge() and ne() # importing operator module import operator # Initializing variables a = 4 b = 3 # using gt() to check if a is greater than b if (operator.gt(a,b)): print ( "4 is greater than 3" ) else : print ( "4 is not greater than 3" ) # using ge() to check if a is greater than or equal to 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" ) # using ne() to check if a is not equal to 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
本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。