{"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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python code to demonstrate working of  <br\/># add(), sub(), mul() <br\/>  <br\/># importing operator module  <br\/>import operator <br\/>  <br\/># Initializing variables <br\/>a = 4<br\/>  <br\/>b = 3<br\/>  <br\/># using add() to add two numbers <br\/>print (&quot;The addition of numbers is :&quot;,end=&quot;&quot;); <br\/>print (operator.add(a, b)) <br\/>  <br\/># using sub() to subtract two numbers <br\/>print (&quot;The difference of numbers is :&quot;,end=&quot;&quot;); <br\/>print (operator.sub(a, b)) <br\/>  <br\/># using mul() to multiply two numbers <br\/>print (&quot;The product of numbers is :&quot;,end=&quot;&quot;); <br\/>print (operator.mul(a, b))<\/code><\/pre> <\/div>\n<h3 id=\"output\" style=\"text-align: justify;\">Output:<\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">The addition of numbers is :7<br\/>The difference of numbers is :1<br\/>The product of numbers is :12<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python code to demonstrate working of  <br\/># truediv(), floordiv(), pow(), mod() <br\/>  <br\/># importing operator module  <br\/>import operator <br\/>  <br\/># Initializing variables <br\/>a = 5<br\/>  <br\/>b = 2<br\/>  <br\/># using truediv() to divide two numbers <br\/>print (&quot;The true division of numbers is : &quot;,end=&quot;&quot;); <br\/>print (operator.truediv(a,b)) <br\/>  <br\/># using floordiv() to divide two numbers <br\/>print (&quot;The floor division of numbers is : &quot;,end=&quot;&quot;); <br\/>print (operator.floordiv(a,b)) <br\/>  <br\/># using pow() to exponentiate two numbers <br\/>print (&quot;The exponentiation of numbers is : &quot;,end=&quot;&quot;); <br\/>print (operator.pow(a,b)) <br\/>  <br\/># using mod() to take modulus of two numbers <br\/>print (&quot;The modulus of numbers is : &quot;,end=&quot;&quot;); <br\/>print (operator.mod(a,b)<\/code><\/pre> <\/div>\n<h3 id=\"output-2\" style=\"text-align: justify;\">Output:<\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">The true division of numbers is : 2.5<br\/>The floor division of numbers is : 2<br\/>The exponentiation of numbers is : 25<br\/>The modulus of numbers is : 1<\/code><\/pre> <\/div>\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 &lt; 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 &lt;= 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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python code to demonstrate working of  <br\/># lt(), le() and eq() <br\/>  <br\/># importing operator module  <br\/>import operator <br\/>  <br\/># Initializing variables <br\/>a = 3<br\/>  <br\/>b = 3<br\/>  <br\/># using lt() to check if a is less than b <br\/>if(operator.lt(a,b)): <br\/>       print (&quot;3 is less than 3&quot;) <br\/>else : print (&quot;3 is not less than 3&quot;) <br\/>  <br\/># using le() to check if a is less than or equal to b <br\/>if(operator.le(a,b)): <br\/>       print (&quot;3 is less than or equal to 3&quot;) <br\/>else : print (&quot;3 is not less than or equal to 3&quot;) <br\/>  <br\/># using eq() to check if a is equal to b <br\/>if (operator.eq(a,b)): <br\/>       print (&quot;3 is equal to 3&quot;) <br\/>else : print (&quot;3 is not equal to 3&quot;) <\/code><\/pre> <\/div>\n<h3 id=\"output-3\" style=\"text-align: justify;\">Output:<\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">3 is not less than 3<br\/>3 is less than or equal to 3<br\/>3 is equal to 3<\/code><\/pre> <\/div>\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 &gt; 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 &gt;= 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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python code to demonstrate working of  <br\/># gt(), ge() and ne() <br\/>  <br\/># importing operator module  <br\/>import operator <br\/>  <br\/># Initializing variables <br\/>a = 4<br\/>  <br\/>b = 3<br\/>  <br\/># using gt() to check if a is greater than b <br\/>if (operator.gt(a,b)): <br\/>       print (&quot;4 is greater than 3&quot;) <br\/>else : print (&quot;4 is not greater than 3&quot;) <br\/>  <br\/># using ge() to check if a is greater than or equal to b <br\/>if (operator.ge(a,b)): <br\/>       print (&quot;4 is greater than or equal to 3&quot;) <br\/>else : print (&quot;4 is not greater than or equal to 3&quot;) <br\/>  <br\/># using ne() to check if a is not equal to b <br\/>if (operator.ne(a,b)): <br\/>       print (&quot;4 is not equal to 3&quot;) <br\/>else : print (&quot;4 is equal to 3&quot;) <\/code><\/pre> <\/div>\n<h3 id=\"output-4\" style=\"text-align: justify;\">Output:<\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">4 is greater than 3<br\/>4 is greater than or equal to 3<br\/>4 is not equal to 3<\/code><\/pre> <\/div>\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}]}}