{"id":33465,"date":"2020-03-17T13:37:46","date_gmt":"2020-03-17T08:07:46","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=33465"},"modified":"2020-03-17T13:37:46","modified_gmt":"2020-03-17T08:07:46","slug":"operator-functions-in-python","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/operator-functions-in-python\/","title":{"rendered":"Operator Functions in Python"},"content":{"rendered":"<p style=\"text-align: justify;\">Python has predefined functions for several mathematical, logical, relational, bit wise etc operations under the module \u201coperator\u201d. a number of the essential functions are covered during this article.<\/p>\n<ol style=\"text-align: justify;\">\n<li><strong>add (a, b) : This<\/strong> function returns\u00a0<strong>addition<\/strong>of the given arguments.<br \/>\nOperation \u2013\u00a0<strong>a + b.<\/strong><\/li>\n<li><strong>sub (a, b)<\/strong><strong>:<\/strong>This function returns\u00a0<strong>difference<\/strong>\u00a0of the given arguments.<br \/>\nOperation \u2013\u00a0<strong>a \u2013 b.<\/strong><\/li>\n<li><strong>mul (a, b)<\/strong><strong>: <\/strong>This function returns\u00a0<strong>product<\/strong>of the given arguments.<br \/>\nOperation \u2013\u00a0<strong>a * b<\/strong><\/li>\n<\/ol>\n[pastacode lang=\u201dpython\u201d manual=\u201d%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))\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output\" style=\"text-align: justify;\">Output:<\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201dThe%20addition%20of%20numbers%20is%20%3A7%0AThe%20difference%20of%20numbers%20is%20%3A1%0AThe%20product%20of%20numbers%20is%20%3A12\u2033 message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p style=\"text-align: justify;\">\u00a0 \u00a0 4. <strong>true div(a,b)\u00a0<\/strong>:- This function returns division of the given arguments.<br \/>\nOperation \u2013 a \/ b.<\/p>\n<p style=\"text-align: justify;\">\u00a0 \u00a0 \u00a05. <strong>floor div(a,b)<\/strong> :- This function also returns division of the given arguments. But the value is floored value i.e. returns greatest small integer.<br \/>\nOperation \u2013 a \/\/ b.<\/p>\n<p style=\"text-align: justify;\">\u00a0 \u00a0 \u00a06. <strong>pow(a,b)<\/strong> :- This function returns exponentiation of the given arguments.<br \/>\nOperation \u2013 a ** b.<\/p>\n<p style=\"text-align: justify;\">\u00a0 \u00a0 \u00a0 7. <strong>mod(a,b)<\/strong> :- This function returns modulus of the given arguments.<br \/>\nOperation \u2013 a % b.<\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%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)\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output-2\" style=\"text-align: justify;\">Output:<\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201dThe%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\u2033 message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p style=\"text-align: justify;\"><strong>\u00a0 \u00a0 \u00a0 8. lt(a, b)<\/strong>\u00a0:- This function is used to\u00a0<strong>check if a is less than b or not<\/strong>. Returns true if a is less than b, else returns false.<br \/>\nOperation \u2013 <strong>a < b<\/strong>.<\/p>\n<p style=\"text-align: justify;\"><strong>\u00a0 \u00a0 \u00a0 9. le(a, b)<\/strong>\u00a0:- This function is used to\u00a0<strong>check if a is less than or equal to b or not<\/strong>. Returns true if a is less than or equal to b, else returns false.<br \/>\nOperation \u2013 <strong>a <= b<\/strong>.<\/p>\n<p style=\"text-align: justify;\"><strong>\u00a0 \u00a0 \u00a0 10. eq(a, b)<\/strong>\u00a0:- This function is used to\u00a0<strong>check if a is equal to b or not<\/strong>. Returns true if a is equal to b, else returns false.<br \/>\nOperation \u2013 <strong>a == b<\/strong>.<\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%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\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output-3\" style=\"text-align: justify;\">Output:<\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201d3%20is%20not%20less%20than%203%0A3%20is%20less%20than%20or%20equal%20to%203%0A3%20is%20equal%20to%203\u2033 message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p style=\"text-align: justify;\"><strong>\u00a0 \u00a0 \u00a0 11. gt(a,b)<\/strong>\u00a0:- This function is used to\u00a0<strong>check if a is greater than b or not<\/strong>. Returns true if a is greater than b, else returns false.<br \/>\nOperation \u2013 <strong>a > b<\/strong>.<\/p>\n<p style=\"text-align: justify;\"><strong>\u00a0 \u00a0 \u00a012. ge(a,b)<\/strong>\u00a0:- This function is used to\u00a0<strong>check if a is greater than or equal to b or not<\/strong>. Returns true if a is greater than or equal to b, else\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 returns false.<br \/>\nOperation \u2013 <strong>a >= b<\/strong>.<\/p>\n<p style=\"text-align: justify;\"><strong>\u00a0 \u00a0 \u00a013. ne(a,b)<\/strong>\u00a0:- This function is used to\u00a0<strong>check if a is not equal to b or is equal<\/strong>. Returns true if a is not equal to b, else returns false.<br \/>\nOperation \u2013 <strong>a != b<\/strong>.<\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%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\u2033 message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output-4\" style=\"text-align: justify;\">Output:<\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201d4%20is%20greater%20than%203%0A4%20is%20greater%20than%20or%20equal%20to%203%0A4%20is%20not%20equal%20to%203\u2033 message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n","protected":false},"excerpt":{"rendered":"<p>Python has predefined functions for several mathematical, logical, relational, bit wise etc operations under the module \u201coperator\u201d. a number of the essential functions are covered during this article. add (a, b) : This function returns\u00a0additionof the given arguments. Operation \u2013\u00a0a + b. sub (a, b):This function returns\u00a0difference\u00a0of the given arguments. Operation \u2013\u00a0a \u2013 b. mul [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":33474,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[86888,86887,86886],"class_list":["post-33465","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding","tag-different-methods-in-python","tag-operators-functions-in-python","tag-operators-in-python"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/33465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=33465"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/33465\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/33474"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=33465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=33465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=33465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}