logo

Python の演算子関数 |セット1

Python には、モジュール「operator」の下に多くの数学的論理リレーショナルのビット単位などの演算用の関数が事前定義されています。この記事では、基本的な機能のいくつかについて説明します。 1.追加(a b) :- この関数は戻り値を返します 追加 与えられた引数の。手術 - a + b。 2. サブ(a b) :- この関数は戻り値を返します 違い 与えられた引数の。手術 - a~b。 3. マル(a b) :- この関数は戻り値を返します 製品 与えられた引数の。手術 - a * b。 Python
# 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)) 
Output:
The addition of numbers is:7 The difference of numbers is :1 The product of numbers is:12 

4. truediv(ab) :- この関数は戻り値を返します 分割 与えられた引数の。手術 - a / b。 5. フロアディビジョン(ab) :- この関数は、指定された引数の除算も返します。ただし、値は下限値です。つまり、 最大の小さい整数を返します 。手術 - a // b. 6.パウ(アブ) :- この関数は戻り値を返します べき乗 与えられた引数の。手術 - a ** b. 7.mod(ab) :- この関数は戻り値を返します 弾性率 与えられた引数の。手術 - a% b. Python
# 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(ab)) # using floordiv() to divide two numbers print ('The floor division of numbers is : 'end=''); print (operator.floordiv(ab)) # using pow() to exponentiate two numbers print ('The exponentiation of numbers is : 'end=''); print (operator.pow(ab)) # using mod() to take modulus of two numbers print ('The modulus of numbers is : 'end=''); print (operator.mod(ab)) 
Output:
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 を返します。手術 - ある< b9. ザ(a b) :- この関数は次の目的で使用されます。 a が b 以下かどうかをチェックします 。 a が b 以下の場合は true を返し、それ以外の場合は false を返します。手術 - ある<= b10. 等式(a b) :- この関数は次の目的で使用されます。 a が b に等しいかどうかをチェックする 。 a が b に等しい場合は true を返し、それ以外の場合は false を返します。手術 - a == b . Python
# 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(ab)): 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(ab)): 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(ab)): print ('3 is equal to 3') else : print ('3 is not equal to 3') 
Output:
3 is not less than 3 3 is less than or equal to 3 3 is equal to 3 

11.gt(ab) :- この関数は次の目的で使用されます。 a が b より大きいかどうかをチェックする 。 a が b より大きい場合は true を返し、それ以外の場合は false を返します。手術 - a > b12. ゲ(アブ) :- この関数は次の目的で使用されます。 a が b 以上であるかどうかをチェックする 。 a が b 以上の場合は true を返し、それ以外の場合は false を返します。手術 - a >= b13.ネ(アブ) :- この関数は次の目的で使用されます。 a が b と等しくない、または等しいかどうかをチェックする 。 a が b と等しくない場合は true を返し、それ以外の場合は false を返します。手術 - a != b . Python
# 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(ab)): 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(ab)): 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(ab)): print ('4 is not equal to 3') else : print ('4 is equal to 3') 
Output:
4 is greater than 3 4 is greater than or equal to 3 4 is not equal to 3