Python has predefined functions for several mathematical, logical, relational, bit wise etc operations under the module “operator”. a number of the essential functions are covered during this article.

  1. add (a, b) : This function returns additionof the given arguments.
    Operation – a + b.
  2. sub (a, b):This function returns difference of the given arguments.
    Operation – a – b.
  3. mul (a, b): This function returns productof the given arguments.
    Operation – a * b
[pastacode lang=”python” manual=”%23%20Python%20code%20to%20demonstrate%20working%20of%20%20%0A%23%20add()%2C%20sub()%2C%20mul()%20%0A%20%20%0A%23%20importing%20operator%20module%20%20%0Aimport%20operator%20%0A%20%20%0A%23%20Initializing%20variables%20%0Aa%20%3D%204%0A%20%20%0Ab%20%3D%203%0A%20%20%0A%23%20using%20add()%20to%20add%20two%20numbers%20%0Aprint%20(%22The%20addition%20of%20numbers%20is%20%3A%22%2Cend%3D%22%22)%3B%20%0Aprint%20(operator.add(a%2C%20b))%20%0A%20%20%0A%23%20using%20sub()%20to%20subtract%20two%20numbers%20%0Aprint%20(%22The%20difference%20of%20numbers%20is%20%3A%22%2Cend%3D%22%22)%3B%20%0Aprint%20(operator.sub(a%2C%20b))%20%0A%20%20%0A%23%20using%20mul()%20to%20multiply%20two%20numbers%20%0Aprint%20(%22The%20product%20of%20numbers%20is%20%3A%22%2Cend%3D%22%22)%3B%20%0Aprint%20(operator.mul(a%2C%20b))” message=”” highlight=”” provider=”manual”/]

Output:

[pastacode lang=”python” manual=”The%20addition%20of%20numbers%20is%20%3A7%0AThe%20difference%20of%20numbers%20is%20%3A1%0AThe%20product%20of%20numbers%20is%20%3A12″ message=”” highlight=”” provider=”manual”/]

    4. true div(a,b) :- This function returns division of the given arguments.
Operation – a / b.

     5. floor div(a,b) :- This function also returns division of the given arguments. But the value is floored value i.e. returns greatest small integer.
Operation – a // b.

     6. pow(a,b) :- This function returns exponentiation of the given arguments.
Operation – a ** b.

      7. mod(a,b) :- This function returns modulus of the given arguments.
Operation – a % b.

[pastacode lang=”python” manual=”%23%20Python%20code%20to%20demonstrate%20working%20of%20%20%0A%23%20truediv()%2C%20floordiv()%2C%20pow()%2C%20mod()%20%0A%20%20%0A%23%20importing%20operator%20module%20%20%0Aimport%20operator%20%0A%20%20%0A%23%20Initializing%20variables%20%0Aa%20%3D%205%0A%20%20%0Ab%20%3D%202%0A%20%20%0A%23%20using%20truediv()%20to%20divide%20two%20numbers%20%0Aprint%20(%22The%20true%20division%20of%20numbers%20is%20%3A%20%22%2Cend%3D%22%22)%3B%20%0Aprint%20(operator.truediv(a%2Cb))%20%0A%20%20%0A%23%20using%20floordiv()%20to%20divide%20two%20numbers%20%0Aprint%20(%22The%20floor%20division%20of%20numbers%20is%20%3A%20%22%2Cend%3D%22%22)%3B%20%0Aprint%20(operator.floordiv(a%2Cb))%20%0A%20%20%0A%23%20using%20pow()%20to%20exponentiate%20two%20numbers%20%0Aprint%20(%22The%20exponentiation%20of%20numbers%20is%20%3A%20%22%2Cend%3D%22%22)%3B%20%0Aprint%20(operator.pow(a%2Cb))%20%0A%20%20%0A%23%20using%20mod()%20to%20take%20modulus%20of%20two%20numbers%20%0Aprint%20(%22The%20modulus%20of%20numbers%20is%20%3A%20%22%2Cend%3D%22%22)%3B%20%0Aprint%20(operator.mod(a%2Cb)” message=”” highlight=”” provider=”manual”/]

Output:

[pastacode lang=”python” manual=”The%20true%20division%20of%20numbers%20is%20%3A%202.5%0AThe%20floor%20division%20of%20numbers%20is%20%3A%202%0AThe%20exponentiation%20of%20numbers%20is%20%3A%2025%0AThe%20modulus%20of%20numbers%20is%20%3A%201″ message=”” highlight=”” provider=”manual”/]

      8. lt(a, b) :- This function is used to check if a is less than b or not. Returns true if a is less than b, else returns false.
Operation – a < b.

      9. le(a, b) :- This function is used to check if a is less than or equal to b or not. Returns true if a is less than or equal to b, else returns false.
Operation – a <= b.

      10. eq(a, b) :- This function is used to check if a is equal to b or not. Returns true if a is equal to b, else returns false.
Operation – a == b.

[pastacode lang=”python” manual=”%23%20Python%20code%20to%20demonstrate%20working%20of%20%20%0A%23%20lt()%2C%20le()%20and%20eq()%20%0A%20%20%0A%23%20importing%20operator%20module%20%20%0Aimport%20operator%20%0A%20%20%0A%23%20Initializing%20variables%20%0Aa%20%3D%203%0A%20%20%0Ab%20%3D%203%0A%20%20%0A%23%20using%20lt()%20to%20check%20if%20a%20is%20less%20than%20b%20%0Aif(operator.lt(a%2Cb))%3A%20%0A%20%20%20%20%20%20%20print%20(%223%20is%20less%20than%203%22)%20%0Aelse%20%3A%20print%20(%223%20is%20not%20less%20than%203%22)%20%0A%20%20%0A%23%20using%20le()%20to%20check%20if%20a%20is%20less%20than%20or%20equal%20to%20b%20%0Aif(operator.le(a%2Cb))%3A%20%0A%20%20%20%20%20%20%20print%20(%223%20is%20less%20than%20or%20equal%20to%203%22)%20%0Aelse%20%3A%20print%20(%223%20is%20not%20less%20than%20or%20equal%20to%203%22)%20%0A%20%20%0A%23%20using%20eq()%20to%20check%20if%20a%20is%20equal%20to%20b%20%0Aif%20(operator.eq(a%2Cb))%3A%20%0A%20%20%20%20%20%20%20print%20(%223%20is%20equal%20to%203%22)%20%0Aelse%20%3A%20print%20(%223%20is%20not%20equal%20to%203%22)%20%0A” message=”” highlight=”” provider=”manual”/]

Output:

[pastacode lang=”python” manual=”3%20is%20not%20less%20than%203%0A3%20is%20less%20than%20or%20equal%20to%203%0A3%20is%20equal%20to%203″ message=”” highlight=”” provider=”manual”/]

      11. gt(a,b) :- This function is used to check if a is greater than b or not. Returns true if a is greater than b, else returns false.
Operation – a > b.

     12. ge(a,b) :- This function is used to check if a is greater than or equal to b or not. Returns true if a is greater than or equal to b, else                        returns false.
Operation – a >= b.

     13. ne(a,b) :- This function is used to check if a is not equal to b or is equal. Returns true if a is not equal to b, else returns false.
Operation – a != b.

[pastacode lang=”python” manual=”%23%20Python%20code%20to%20demonstrate%20working%20of%20%20%0A%23%20gt()%2C%20ge()%20and%20ne()%20%0A%20%20%0A%23%20importing%20operator%20module%20%20%0Aimport%20operator%20%0A%20%20%0A%23%20Initializing%20variables%20%0Aa%20%3D%204%0A%20%20%0Ab%20%3D%203%0A%20%20%0A%23%20using%20gt()%20to%20check%20if%20a%20is%20greater%20than%20b%20%0Aif%20(operator.gt(a%2Cb))%3A%20%0A%20%20%20%20%20%20%20print%20(%224%20is%20greater%20than%203%22)%20%0Aelse%20%3A%20print%20(%224%20is%20not%20greater%20than%203%22)%20%0A%20%20%0A%23%20using%20ge()%20to%20check%20if%20a%20is%20greater%20than%20or%20equal%20to%20b%20%0Aif%20(operator.ge(a%2Cb))%3A%20%0A%20%20%20%20%20%20%20print%20(%224%20is%20greater%20than%20or%20equal%20to%203%22)%20%0Aelse%20%3A%20print%20(%224%20is%20not%20greater%20than%20or%20equal%20to%203%22)%20%0A%20%20%0A%23%20using%20ne()%20to%20check%20if%20a%20is%20not%20equal%20to%20b%20%0Aif%20(operator.ne(a%2Cb))%3A%20%0A%20%20%20%20%20%20%20print%20(%224%20is%20not%20equal%20to%203%22)%20%0Aelse%20%3A%20print%20(%224%20is%20equal%20to%203%22)%20″ message=”” highlight=”” provider=”manual”/]

Output:

[pastacode lang=”python” manual=”4%20is%20greater%20than%203%0A4%20is%20greater%20than%20or%20equal%20to%203%0A4%20is%20not%20equal%20to%203″ message=”” highlight=”” provider=”manual”/]

Categorized in: