{"id":3112,"date":"2026-11-09T06:30:03","date_gmt":"2026-11-09T06:30:03","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3112"},"modified":"2026-11-09T06:30:03","modified_gmt":"2026-11-09T06:30:03","slug":"python-operators","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-operators\/","title":{"rendered":"Python Operators: All 7 Types with Examples"},"content":{"rendered":"<p>Operators are the symbols that tell Python to do something with values: add them, compare them, check a condition, and so on. In <code>total = price + tax<\/code>, the <code>+<\/code> and <code>=<\/code> are operators, and <code>price<\/code>, <code>tax<\/code> and <code>total<\/code> are the <strong>operands<\/strong> they work on.<\/p>\n<p>Python has seven groups of operators. This page gives you a quick, working example of each group, then covers operator precedence and the mistakes beginners make most often. Each group also has its own detailed lesson, linked below.<\/p>\n<h2>Types of Operators in Python<\/h2>\n<table>\n<thead>\n<tr>\n<th>Group<\/th>\n<th>Operators<\/th>\n<th>Used for<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Arithmetic<\/td>\n<td><code>+ - * \/ \/\/ % **<\/code><\/td>\n<td>Maths on numbers<\/td>\n<\/tr>\n<tr>\n<td>Assignment<\/td>\n<td><code>= += -= *= \/= \/\/= %= **=<\/code><\/td>\n<td>Storing and updating values in variables<\/td>\n<\/tr>\n<tr>\n<td>Comparison<\/td>\n<td><code>== != &gt; &lt; &gt;= &lt;=<\/code><\/td>\n<td>Comparing two values (result is True or False)<\/td>\n<\/tr>\n<tr>\n<td>Logical<\/td>\n<td><code>and or not<\/code><\/td>\n<td>Combining conditions<\/td>\n<\/tr>\n<tr>\n<td>Bitwise<\/td>\n<td><code>&amp; | ^ ~ &lt;&lt; &gt;&gt;<\/code><\/td>\n<td>Working on the binary bits of integers<\/td>\n<\/tr>\n<tr>\n<td>Membership<\/td>\n<td><code>in, not in<\/code><\/td>\n<td>Checking if a value is inside a list, string, etc.<\/td>\n<\/tr>\n<tr>\n<td>Identity<\/td>\n<td><code>is, is not<\/code><\/td>\n<td>Checking if two names point to the same object<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>1. Arithmetic Operators<\/h2>\n<p>These work the way you expect from maths, with two extras: <code>\/\/<\/code> (floor division, which drops the decimal part) and <code>**<\/code> (power).<\/p>\n<pre class=\"wp-block-code\"><code>a = 17\nb = 5\nprint(a + b)    # addition\nprint(a - b)    # subtraction\nprint(a * b)    # multiplication\nprint(a \/ b)    # division, always gives a float\nprint(a \/\/ b)   # floor division\nprint(a % b)    # remainder (modulus)\nprint(a ** 2)   # power<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>22\n12\n85\n3.4\n3\n2\n289<\/code><\/pre>\n<p>Read more: <a href=\"https:\/\/gyanipandit.com\/programming\/python-arithmetic-operators\/\">Python arithmetic operators<\/a><\/p>\n<h2>2. Assignment Operators<\/h2>\n<p><code>=<\/code> stores a value in a variable. The shortcut forms such as <code>+=<\/code> update the variable using its current value, so <code>score += 5<\/code> means <code>score = score + 5<\/code>.<\/p>\n<pre class=\"wp-block-code\"><code>score = 10\nscore += 5\nprint(score)\nscore *= 2\nprint(score)\nscore \/\/= 4\nprint(score)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>15\n30\n7<\/code><\/pre>\n<p>Read more: <a href=\"https:\/\/gyanipandit.com\/programming\/python-assignment-operators\/\">Python assignment operators<\/a><\/p>\n<h2>3. Comparison Operators<\/h2>\n<p>A comparison always gives a Boolean: <code>True<\/code> or <code>False<\/code>. That is why comparisons are used inside <code>if<\/code> statements and loops.<\/p>\n<pre class=\"wp-block-code\"><code>x = 10\ny = 20\nprint(x == y)\nprint(x != y)\nprint(x &lt; y)\nprint(x &gt;= 10)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>False\nTrue\nTrue\nTrue<\/code><\/pre>\n<p>Read more: <a href=\"https:\/\/gyanipandit.com\/programming\/python-comparison-operators\/\">Python comparison operators<\/a><\/p>\n<h2>4. Logical Operators<\/h2>\n<p><code>and<\/code> is True only when both sides are True, <code>or<\/code> is True when at least one side is True, and <code>not<\/code> flips the result.<\/p>\n<pre class=\"wp-block-code\"><code>age = 19\nhas_id = True\nprint(age &gt;= 18 and has_id)\nprint(age &lt; 13 or age &gt; 60)\nprint(not has_id)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nFalse\nFalse<\/code><\/pre>\n<p>Read more: <a href=\"https:\/\/gyanipandit.com\/programming\/python-logical-operators\/\">Python logical operators<\/a><\/p>\n<h2>5. Bitwise Operators<\/h2>\n<p>Bitwise operators work on the binary form of whole numbers. Here 6 is <code>110<\/code> in binary and 3 is <code>011<\/code>.<\/p>\n<pre class=\"wp-block-code\"><code>p = 6\nq = 3\nprint(p &amp; q)    # AND\nprint(p | q)    # OR\nprint(p ^ q)    # XOR\nprint(~p)       # NOT\nprint(p &lt;&lt; 1)   # left shift\nprint(p &gt;&gt; 1)   # right shift<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>2\n7\n5\n-7\n12\n3<\/code><\/pre>\n<p>Read more: <a href=\"https:\/\/gyanipandit.com\/programming\/python-bitwise-operators\/\">Python bitwise operators<\/a><\/p>\n<h2>6. Membership Operators<\/h2>\n<p><code>in<\/code> checks whether a value is present in a sequence such as a list, tuple, string or dictionary (for a dictionary it checks the keys).<\/p>\n<pre class=\"wp-block-code\"><code>fruits = [\"apple\", \"mango\", \"banana\"]\nprint(\"mango\" in fruits)\nprint(\"grapes\" not in fruits)\nprint(\"py\" in \"python\")<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nTrue\nTrue<\/code><\/pre>\n<p>Read more: <a href=\"https:\/\/gyanipandit.com\/programming\/membership-operators-in-python\/\">Membership operators in Python<\/a><\/p>\n<h2>7. Identity Operators<\/h2>\n<p><code>is<\/code> checks whether two variables point to the <em>same object<\/em> in memory, which is different from having the same value. Use <code>==<\/code> to compare values and <code>is<\/code> mainly to check for <code>None<\/code>.<\/p>\n<pre class=\"wp-block-code\"><code>list1 = [1, 2, 3]\nlist2 = [1, 2, 3]\nlist3 = list1\nprint(list1 == list2)\nprint(list1 is list2)\nprint(list1 is list3)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nFalse\nTrue<\/code><\/pre>\n<p><code>list1<\/code> and <code>list2<\/code> hold equal values but are two separate lists, so <code>is<\/code> gives False. <code>list3<\/code> is just another name for <code>list1<\/code>, so <code>is<\/code> gives True.<\/p>\n<p>Read more: <a href=\"https:\/\/gyanipandit.com\/programming\/identity-operators-in-python\/\">Identity operators in Python<\/a><\/p>\n<h2>Operator Precedence in Python<\/h2>\n<p>When an expression has more than one operator, Python decides the order using precedence. Higher rows in this table run first:<\/p>\n<table>\n<thead>\n<tr>\n<th>Order<\/th>\n<th>Operators<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1 (highest)<\/td>\n<td><code>()<\/code> brackets<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td><code>**<\/code><\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td><code>+x -x ~x<\/code> (unary)<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td><code>* \/ \/\/ %<\/code><\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td><code>+ -<\/code><\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td><code>&lt;&lt; &gt;&gt;<\/code><\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td><code>&amp;<\/code><\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td><code>^<\/code><\/td>\n<\/tr>\n<tr>\n<td>9<\/td>\n<td><code>|<\/code><\/td>\n<\/tr>\n<tr>\n<td>10<\/td>\n<td>Comparisons, <code>in<\/code>, <code>not in<\/code>, <code>is<\/code>, <code>is not<\/code><\/td>\n<\/tr>\n<tr>\n<td>11<\/td>\n<td><code>not<\/code><\/td>\n<\/tr>\n<tr>\n<td>12<\/td>\n<td><code>and<\/code><\/td>\n<\/tr>\n<tr>\n<td>13 (lowest)<\/td>\n<td><code>or<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"wp-block-code\"><code>print(2 + 3 * 4)\nprint((2 + 3) * 4)\nprint(2 ** 3 ** 2)\nprint(-3 ** 2)\nprint(10 - 4 - 3)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>14\n20\n512\n-9\n3<\/code><\/pre>\n<p>Two of these surprise most students. <code>2 ** 3 ** 2<\/code> is worked out from the right, as <code>2 ** 9<\/code>, so the answer is 512, not 64. And <code>-3 ** 2<\/code> is -9 because the power is applied before the minus sign. Write <code>(-3) ** 2<\/code> if you want 9. When in doubt, add brackets; they cost nothing and make your code easier to read.<\/p>\n<h2>Common Mistakes with Python Operators<\/h2>\n<ul>\n<li><strong>Using <code>=<\/code> instead of <code>==<\/code>.<\/strong> <code>=<\/code> stores a value, <code>==<\/code> compares. Writing <code>if marks = 90:<\/code> is a syntax error.<\/li>\n<li><strong>Expecting <code>\/<\/code> to give a whole number.<\/strong> <code>7 \/ 2<\/code> is <code>3.5<\/code>. Use <code>7 \/\/ 2<\/code> for <code>3<\/code>. Careful with negatives: <code>-7 \/\/ 2<\/code> is <code>-4<\/code>, because floor division always rounds down.<\/li>\n<li><strong>Using <code>is<\/code> to compare values.<\/strong> <code>is<\/code> checks identity, not equality. Use <code>==<\/code> for numbers and strings, and keep <code>is<\/code> for <code>None<\/code>, as in <code>if value is None:<\/code>.<\/li>\n<li><strong>Forgetting that <code>and<\/code> \/ <code>or<\/code> return a value, not always True or False.<\/strong> <code>0 or \"default\"<\/code> gives <code>\"default\"<\/code>, and <code>3 and 5<\/code> gives <code>5<\/code>. This is useful for default values, but it can confuse you while debugging.<\/li>\n<\/ul>\n<h2>Practice Questions<\/h2>\n<p>Work these out on paper first, then run them to check.<\/p>\n<ol>\n<li><code>print(15 % 4 + 2 ** 2)<\/code><\/li>\n<li><code>print(not (5 &gt; 3 and 2 &gt; 8))<\/code><\/li>\n<li><code>print(10 \/\/ 3 * 3 + 10 % 3)<\/code><\/li>\n<\/ol>\n<p><strong>Answers:<\/strong> 1) <code>7<\/code>, because 15 % 4 is 3 and 2 ** 2 is 4. 2) <code>True<\/code>, because the bracket is False and <code>not<\/code> flips it. 3) <code>10<\/code>, because 3 * 3 + 1 is 10. This works for any two positive whole numbers <code>a<\/code> and <code>b<\/code>: <code>(a \/\/ b) * b + a % b == a<\/code>.<\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>How many types of operators are there in Python?<\/h3>\n<p>Seven: arithmetic, assignment, comparison, logical, bitwise, membership and identity.<\/p>\n<h3>What is the difference between \/ and \/\/ in Python?<\/h3>\n<p><code>\/<\/code> is true division and always returns a float (<code>7 \/ 2<\/code> is <code>3.5<\/code>). <code>\/\/<\/code> is floor division and rounds down to the nearest whole number (<code>7 \/\/ 2<\/code> is <code>3<\/code>).<\/p>\n<h3>What is the difference between == and is?<\/h3>\n<p><code>==<\/code> checks whether two values are equal. <code>is<\/code> checks whether two variables refer to the very same object in memory.<\/p>\n<h3>Does Python have ++ and &#8212; operators?<\/h3>\n<p>No. Python has no increment or decrement operator. Use <code>count += 1<\/code> or <code>count -= 1<\/code> instead.<\/p>\n<h3>Which operator has the highest precedence in Python?<\/h3>\n<p>Brackets come first, then the power operator <code>**<\/code>. Among the logical operators, <code>not<\/code> runs first, then <code>and<\/code>, then <code>or<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Operators are the symbols that tell Python to do something with values: add them, compare them, check a condition, and so on. In total = price + tax, the + and = are operators, and price, tax and total are the operands they work on. Python has seven groups of operators. This page gives you &#8230; <a title=\"Python Operators: All 7 Types with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-operators\/\" aria-label=\"Read more about Python Operators: All 7 Types with Examples\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":["post-3112","post","type-post","status-publish","format-standard","hentry","category-python-tutorial","tag-python","tag-python-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3112"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3112\/revisions"}],"predecessor-version":[{"id":6126,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3112\/revisions\/6126"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}