{"id":74352,"date":"2019-12-20T09:44:01","date_gmt":"2019-12-20T04:14:01","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=74352"},"modified":"2024-08-22T20:11:53","modified_gmt":"2024-08-22T14:41:53","slug":"python-operators","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-operators\/","title":{"rendered":"7 Types of Python Operators that will ease your programming"},"content":{"rendered":"<p>Python has 7 types of operators. In this Python Operators article, we will discuss all of them in detail with examples.<\/p>\n<div class=\"tvdiduno\">Python, the fastest-growing major programming language, has risen in the ranks of programming languages, edging out Java this year and standing as the second most loved language (behind Rust) &#8211; <strong>Stack Overflow<\/strong><\/div>\n<p>First, let\u2019s discuss what are operators.<\/p>\n<h3>What are Operators?<\/h3>\n<p>An operator is a symbol that will perform mathematical operations on variables or on values. Operators operate on operands (values) and return a result. For instance, in the expression 3 + 5, + is the operator, and 3 and 5 are the operands, resulting in the value 8. Python supports various types of operators such as arithmetic, comparison, logical, bitwise, and more, each enabling specific types of operations on data. Understanding how to use these operators is fundamental to performing calculations and manipulating data in Python.<\/p>\n<p>Python has 7 types of operators that you can use:<\/p>\n<ul>\n<li>Arithmetic Operators<\/li>\n<li>Relational Operators<\/li>\n<li>Assignment Operators<\/li>\n<li>Logical Operators<\/li>\n<li>Membership Operators<\/li>\n<li>Identity Operators<\/li>\n<li>Bitwise Operators<\/li>\n<\/ul>\n<p>Let\u2019s take an example:<\/p>\n<p>2+3<\/p>\n<p>Here, <strong>+<\/strong> is an operator for <strong>addition<\/strong>. It adds 2 and 3 and prints 5 in the interpreter. This is an <strong>arithmetic<\/strong> operator.<\/p>\n<h3>Types of Python Operators<\/h3>\n<h4>1. Python Arithmetic Operators<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-arithmetic-operators-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74628 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-arithmetic-operators-1.jpg\" alt=\"arithmetic python operators\" width=\"690\" height=\"424\" \/><\/a><\/h4>\n<p>Let\u2019s discuss <strong>arithmetic operators<\/strong>&#8211; they are the most common. You have done this earlier.<\/p>\n<p>Python has seven arithmetic operators for different mathematical operations. They are:<\/p>\n<ul>\n<li>+ (Addition)<\/li>\n<li>&#8211; (Subtraction)<\/li>\n<li>* (Multiplication)<\/li>\n<li>\/ (Division)<\/li>\n<li>** (Exponentiation)<\/li>\n<li>\/\/ (Floor division)<\/li>\n<li>% (Modulus)<\/li>\n<\/ul>\n<h4>a. Addition Operator<\/h4>\n<p>The addition operator<strong> [+] adds<\/strong> two values and gives their sum.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1+num2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">11<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 7+4.1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">11.1<\/div>\n<p>In this example, we added integers to integers and integers to floats.<\/p>\n<h4>b. Subtraction Operator<\/h4>\n<p>The subtraction operator <strong>[-] subtracts<\/strong> second value from first and gives their <strong>difference<\/strong>.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1-num2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 7-4.1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">2.9000000000000004<\/div>\n<p>In this example, we subtracted integers from integers and floats from integers.<\/p>\n<h4>c. Multiplication Operator<\/h4>\n<p>The multiplication operator <strong>[*]<\/strong> <strong>multiplies<\/strong> two values and gives their <strong>product<\/strong>.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1*num2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">28<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 7*4.1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">28.699999999999996<\/div>\n<p>In this example, we multiplied integers by integers and floats by integers.<\/p>\n<h4>d. Division operator<\/h4>\n<p>The division operator<strong> [\/] divides<\/strong> one value by second and gives their <strong>quotient<\/strong>.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1\/num2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1.75<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 7\/4.1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1.707317073170732<\/div>\n<p>In this example, we divided integers by integers and integers by floats. Dividing in Python 3 always gives a <strong>float result<\/strong>.<\/p>\n<h4>e. Exponentiation<\/h4>\n<p>The exponentiation operator <strong>[**] raises<\/strong> one value to <strong>power<\/strong> of second.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1**num2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">2401<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 7**4.1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">2916.7685197377978<\/div>\n<p>In this example, we raise integers to the power of integers and integers to the power of floats.<\/p>\n<h4>f. Floor Division<\/h4>\n<p>The floor division operator <strong>[\/\/] divides<\/strong> one value by second and gives their <strong>quotient rounded<\/strong> to the next smallest whole number.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1\/\/num2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 7**4.1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1.0<\/div>\n<p>In this example, we floor-divided integers by integers and integers by floats.<\/p>\n<h4>g. Modulus<\/h4>\n<p>The modulus operator <strong>[%]<\/strong> <strong>divides<\/strong> one value by second and gives their <strong>remainder<\/strong>.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1%num2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 7%4.1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">2.9000000000000004<\/div>\n<p>In this example, we <strong>floor-divided<\/strong> integers by integers and integers by floats.<\/p>\n<h4>2. Python Relational Operators<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-relational-operators-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74629 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-relational-operators-1.jpg\" alt=\"relational python operators\" width=\"538\" height=\"506\" \/><\/a><\/h4>\n<p>Now, let\u2019s talk about <strong>relational<\/strong> operators. They are also called <strong>comparison operators<\/strong> and they compare values.<\/p>\n<p>Python has 6 relational operators:<\/p>\n<ul>\n<li>&gt; (Greater than)<\/li>\n<li>&lt; (Less than)<\/li>\n<li>== (Equal to)<\/li>\n<li>!= (Not equal to)<\/li>\n<li>&gt;= (Greater than or equal to)<\/li>\n<li>&lt;= (Less than or equal to)<\/li>\n<\/ul>\n<h5>a. Greater than<\/h5>\n<p>The greater than operator <strong>[&gt;] returns<\/strong> <strong>True<\/strong> if the first value is <strong>greater<\/strong> than the second.<\/p>\n<p style=\"padding-left: 40px;\"><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1&gt;num2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 7&gt;4.1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>In this example, we compared integers to integers and integers to floats.<\/p>\n<h5>b. Less than<\/h5>\n<p>The less than operator<strong> [&lt;]<\/strong> <strong>returns<\/strong> <strong>True<\/strong> if the first value is <strong>less<\/strong> than the second.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1&lt;num2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 7&lt;4.1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>In this example, we compared integers to integers and integers to floats.<\/p>\n<h5>c. Equal to<\/h5>\n<p>The equal to operator<strong> [==]<\/strong> <strong>returns True<\/strong> if the first value is <strong>equal<\/strong> to the second.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1==num2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 7==4.1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>In this example, we compared integers to integers and integers to floats.<\/p>\n<h5>d. Not equal to<\/h5>\n<p>The not equal to operator <strong>[!=] returns True<\/strong> if the first value is<strong> not equal<\/strong> to the second.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1!=num2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 7!=4.1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>In this example, we compared integers to integers and integers to floats.<\/p>\n<h5>e. Greater than or equal to<\/h5>\n<p>The greater than or equal to operator<strong> [&gt;=] returns True<\/strong> if the first value is<strong> greater<\/strong> than or <strong>equal<\/strong> to the second.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1&gt;=num2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 7&gt;=4.1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>In this Python operator example, we compared integers to integers and integers to floats.<\/p>\n<h5>f. Less than or equal to<\/h5>\n<p>The less than or equal to operator <strong>[&lt;=] returns True<\/strong> if the first value is <strong>smaller <\/strong>than or <strong>equal<\/strong> to the second.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1&lt;=num2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 7&lt;=4.1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>In this example, we compared integers to integers and integers to floats.<\/p>\n<h4>3. Python Assignment Operators<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/Python-Assignment-Operators.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74583 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/Python-Assignment-Operators.jpg\" alt=\"assignment python operators \" width=\"760\" height=\"405\" \/><\/a><\/h4>\n<p>Now, let\u2019s talk about <strong>assignment operators<\/strong>. They perform an <strong>operation<\/strong> and <strong>assign<\/strong> a value.<\/p>\n<p>Python has 8 assignment operators:<\/p>\n<ul>\n<li>= (Assign)<\/li>\n<li>+= (Add and assign)<\/li>\n<li>-= (Subtract and assign)<\/li>\n<li>*= (Multiply and assign)<\/li>\n<li>\/= (Divide and assign)<\/li>\n<li>%= (Modulus and assign)<\/li>\n<li>**= (Exponentiation and assign)<\/li>\n<li>\/\/= (Floor-divide and assign)<\/li>\n<\/ul>\n<h5>a. Assign<\/h5>\n<p>The assign operator <strong>[=]<\/strong> puts the value on the <strong>right<\/strong> in the variable on left.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; print(num1)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">7<\/div>\n<h5>b. Add and assign<\/h5>\n<p>The add and assign operator <strong>[+=]<\/strong> <strong>adds<\/strong> two values and assigns the result to the variable on left.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num+=num2\r\n&gt;&gt;&gt; print(num1)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">11<\/div>\n<p>num1 is num1+num2 and this is 11.<\/p>\n<h5>c. Subtract and assign<\/h5>\n<p>The subtract and assign operator<strong> [-=]<\/strong> <strong>subtracts<\/strong> second value from first and assigns to first.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1-=num2\r\n&gt;&gt;&gt; print(num1)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<p>num1-num2 is 3, and num1 is now 3.<\/p>\n<h5>d. Multiply and assign<\/h5>\n<p>The multiply and assign operator<strong> [*=]<\/strong> assigns the <strong>product<\/strong> to the variable on left.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1*=num2\r\n&gt;&gt;&gt; print(num1)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">28<\/div>\n<p>num1*num2 is 28, and num1 is 28 now. (7*4=28)<\/p>\n<h5>e. Divide and Assign<\/h5>\n<p>The divide and assign operator<strong> [\/=]<\/strong> assigns the <strong>division<\/strong> of two values to the first.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1\/=num2\r\n&gt;&gt;&gt; print(num1)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1.75<\/div>\n<p>num1\/num2 is 7\/4=1.75. So num1 is 1.75.<\/p>\n<h5>f. Modulus and Assign<\/h5>\n<p>The modulus and assign operator<strong> [%=]<\/strong> performs <strong>modulus<\/strong> on two values and assigns to first.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1%=num2\r\n&gt;&gt;&gt; print(num1)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<p>7%4 is 3, so num1 is 3.<\/p>\n<h5>g. Exponentiation and Assign<\/h5>\n<p>The exponentiation and assign operator<strong> [%=]<\/strong> performs <strong>exponentiation<\/strong> on two values and assigns to first.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1**=num2\r\n&gt;&gt;&gt; print(num1)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">2401<\/div>\n<p>7**4 is 2401, so num1 is 2401.<\/p>\n<h5>h. Floor divide and Assign<\/h5>\n<p>The floor divide and assign operator<strong> [\/\/=]<\/strong> performs <strong>floor division<\/strong> on two values and assigns to first.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; num1=7\r\n&gt;&gt;&gt; num2=4\r\n&gt;&gt;&gt; num1\/\/=num2\r\n&gt;&gt;&gt; print(num1)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<p>7\/\/4 is 1, so num1 is 1.<\/p>\n<h4>4. Python Logical Operators<\/h4>\n<p>They can <strong>combine conditions<\/strong>. Python has 3 logical operators:<\/p>\n<ul>\n<li>and (Logical and)<\/li>\n<li>or (Logical or)<\/li>\n<li>not (Logical not)<\/li>\n<\/ul>\n<h5>a. Logical and<\/h5>\n<p>The logical and operator returns True if <strong>both<\/strong> values are <strong>True<\/strong>. Otherwise, it returns False.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; True and False<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 3 and 4<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">4<\/div>\n<p>For 3 and 4, it doesn\u2019t give True, it gives the last value.<\/p>\n<h5>b. Logical or<\/h5>\n<p>The logical or operator returns True if even <strong>one<\/strong> value is <strong>True<\/strong>. It returns False if both values are False.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; True or False<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 3 or 4<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<p>3 or 4 gives the first value.<\/p>\n<h5>c. Logical not<\/h5>\n<p>The logical not operator returns <strong>True<\/strong> if an expression is True, otherwise returns False.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; not True<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; not 4<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>4 is True, so it prints False.<\/p>\n<h4>5. Python Membership Operators<\/h4>\n<p>Membership operators check whether a value is in <strong>another<\/strong>. Python has 2 membership operators:<\/p>\n<ul>\n<li>in<\/li>\n<li>not in<\/li>\n<\/ul>\n<h5>a. in operator<\/h5>\n<p>The in operator returns <strong>True<\/strong> if the first value is in second. Otherwise, it returns False.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 2 in [1, 2, 3]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; \u2018help\u2019 in \u2018stupidity\u2019<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>2 is in the list [1, 2, 3]. \u2018help\u2019 is not in the string \u2018stupidity\u2019.<\/p>\n<h5>b. not in operator<\/h5>\n<p>The not in operator returns <strong>True<\/strong> if the first value is not in second. It returns False otherwise.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 2 not in (1, 2, 3)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; \u2018help\u2019 not in \u2018stupidity\u2019<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h4>6. Python Identity Operators<\/h4>\n<p>Identity operators check whether two values are <strong>identical<\/strong>. Python has 2 identity operators as well:<\/p>\n<ul>\n<li>is<\/li>\n<li>is not<\/li>\n<\/ul>\n<h5>a. is operator<\/h5>\n<p>The is operator returns True if the first value is the <strong>same<\/strong> as the second. Otherwise, it returns False.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 2 is 2.0<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=8\r\n&gt;&gt;&gt; b=a\r\n&gt;&gt;&gt; a is b<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; c=8\r\n&gt;&gt;&gt; a is c<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>It returns True if two objects have the <strong>same identity<\/strong>.<\/p>\n<h5>b. is not operator<\/h5>\n<p>The is not operator returns True if the first value is <strong>not identical<\/strong> to the second. It returns False otherwise.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 0 is not False<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>0 is not identical to False.<\/p>\n<h4>7. Python Bitwise Operators<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/Python-Bitwise-Operators.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74590 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/Python-Bitwise-Operators.jpg\" alt=\"bitwise python operators\" width=\"688\" height=\"443\" \/><\/a><\/h4>\n<p>Finally, let\u2019s talk about bitwise operators. They operate on values <strong>bit by bit<\/strong>.<\/p>\n<p>Python has 6 bitwise operators:<\/p>\n<ul>\n<li>&amp; (Bitwise and)<\/li>\n<li>| (Bitwise or)<\/li>\n<li>^ (Bitwise xor)<\/li>\n<li>~ (Bitwise 1\u2019s complement)<\/li>\n<li>&lt;&lt; (Bitwise left-shift)<\/li>\n<li>&gt;&gt; (Bitwise right-shift)<\/li>\n<\/ul>\n<h5>a. Bitwise and<\/h5>\n<p>The bitwise and operator <strong>[&amp;]<\/strong> performs logical <strong>AND<\/strong> on corresponding bits in values.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 3&amp;4<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">0<\/div>\n<p>3&amp;4 is 011 &amp; 100. This is 000 (0).<\/p>\n<h5>b. Bitwise or<\/h5>\n<p>The bitwise or operator <strong>[|]<\/strong> performs logical<strong> OR<\/strong> on corresponding bits in values.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 3 | 4<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">7<\/div>\n<p>3|4 is 011|100. This is 111, which is 7.<\/p>\n<h5>c. Bitwise xor<\/h5>\n<p>The bitwise xor operator<strong> [^]<\/strong> performs logical <strong>XOR<\/strong> on corresponding bits in values.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 3^4<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">7<\/div>\n<p>3^4 is 011^100. This is 111, which is 7.<\/p>\n<h5>d. Bitwise 1\u2019s complement<\/h5>\n<p>The bitwise 1\u2019s complement operator <strong>[~]<\/strong> returns the bitwise <strong>negation<\/strong> of a value. Each bit is inverted.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; ~3<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">-4<\/div>\n<p>3 is 011- negation of this is 100, the result is -4. For x, its bitwise 1\u2019s complement is -(x+1).<\/p>\n<h5>e. Bitwise left-shift<\/h5>\n<p>The bitwise left-shift operator <strong>[&lt;&lt;]<\/strong> shifts bits for a value by a given number of places <strong>left<\/strong>. It adds 0s to new positions.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 4&lt;&lt;2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">16<\/div>\n<p>4&lt;&lt;2 is 100&lt;&lt;2. This is 10000. This is 16.<\/p>\n<h5>f. Bitwise right-shift<\/h5>\n<p>The bitwise right-shift operator <strong>[&gt;&gt;]<\/strong> shifts bits for a value by given number of places <strong>right<\/strong>. Some bits are <strong>lost<\/strong>.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 4&gt;&gt;2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<p>4&gt;&gt;2 is 100&gt;&gt;2. This is 1, which is 1 in decimal.<\/p>\n<h4>Python Operator Precedence<\/h4>\n<p>Which operator evaluates first can be confusing. So we have some rules for this too. This is the precedence table that denotes which operator <strong>evaluates<\/strong> first:<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-operator-precedence.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74630 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-operator-precedence.jpg\" alt=\"operator precedence in python\" width=\"663\" height=\"596\" \/><\/a><\/p>\n<p><strong>Higher priority<\/strong> operators evaluate first. We can use this table to get the result of this expression:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 4**2*4\/3+4-2%4<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">23.333333333333332<\/div>\n<h3>Summary<\/h3>\n<p>So, this was all about TechVidvan&#8217;s Python operators article.<\/p>\n<p>Today, we learned about <strong>7 types of operators<\/strong> in Python and their subtypes. These are <strong>arithmetic<\/strong>, <strong>relational<\/strong>, <strong>assignment<\/strong>, <strong>logical<\/strong>, <strong>membership<\/strong>, <strong>identity<\/strong> and <strong>bitwise<\/strong>. We also saw some examples of Python operators. And last, we studied operator precedence.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python has 7 types of operators. In this Python Operators article, we will discuss all of them in detail with examples. Python, the fastest-growing major programming language, has risen in the ranks of programming&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":74575,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[1110,1111,1112,1113,1114,1115,1116,1117],"class_list":["post-74352","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-identity-operators-in-python","tag-python-arithmetic-operators","tag-python-assignment-operators","tag-python-bitwise-operators","tag-python-logical-operators","tag-python-operator-precedence","tag-python-operators","tag-relational-operators-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>7 Types of Python Operators that will ease your programming - TechVidvan<\/title>\n<meta name=\"description\" content=\"Python Operators - Learn the concept of operators and explore seven different types of operators in Python such as arithmetic, bitwise, etc.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-operators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"7 Types of Python Operators that will ease your programming - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python Operators - Learn the concept of operators and explore seven different types of operators in Python such as arithmetic, bitwise, etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-operators\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-20T04:14:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-22T14:41:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-operators.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"7 Types of Python Operators that will ease your programming - TechVidvan","description":"Python Operators - Learn the concept of operators and explore seven different types of operators in Python such as arithmetic, bitwise, etc.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-operators\/","og_locale":"en_US","og_type":"article","og_title":"7 Types of Python Operators that will ease your programming - TechVidvan","og_description":"Python Operators - Learn the concept of operators and explore seven different types of operators in Python such as arithmetic, bitwise, etc.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-operators\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2019-12-20T04:14:01+00:00","article_modified_time":"2024-08-22T14:41:53+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-operators.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-operators\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-operators\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"7 Types of Python Operators that will ease your programming","datePublished":"2019-12-20T04:14:01+00:00","dateModified":"2024-08-22T14:41:53+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-operators\/"},"wordCount":1599,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-operators\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-operators.jpg","keywords":["identity operators in python","python arithmetic operators","python Assignment Operators","python bitwise operators","python logical operators","python operator precedence","python operators","relational operators in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-operators\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-operators\/","url":"https:\/\/techvidvan.com\/tutorials\/python-operators\/","name":"7 Types of Python Operators that will ease your programming - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-operators\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-operators\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-operators.jpg","datePublished":"2019-12-20T04:14:01+00:00","dateModified":"2024-08-22T14:41:53+00:00","description":"Python Operators - Learn the concept of operators and explore seven different types of operators in Python such as arithmetic, bitwise, etc.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-operators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-operators\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-operators\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-operators.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-operators.jpg","width":802,"height":420,"caption":"operators in python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-operators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"7 Types of Python Operators that will ease your programming"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/74352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=74352"}],"version-history":[{"count":2,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/74352\/revisions"}],"predecessor-version":[{"id":447693,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/74352\/revisions\/447693"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/74575"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=74352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=74352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=74352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}