Python Operators
Tags: python
Published: 2020-03-21 17:44:04

<!-- wp:paragraph -->
<p>There are five groups of operators in the Python programming language: arithmetic, comparison, assignment, logical, identity, membership, and bitwise.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Arithmetic operators - are used to perform basic mathematical operations</li><li>Comparison operators - are used to compare values, they return a True/False based on the statement</li><li>Assignment operators - are used to perform some operation and assign a value to a variable</li><li>Logical operators - are used to combine conditional statements</li><li>Identity operators - are used to determine if objects are the same</li><li>Membership operators - are used to determine if a value is found in a sequence</li><li>Bitwise operators - are used to perform operations at the bit level</li></ul>
<!-- /wp:list -->

&nbsp;
<!-- wp:heading -->
<h2>Arithmetic Operators</h2>
<!-- /wp:heading -->

* `+`: [Addition](https://www.queworx.com/python/python-addition/)
* `-`: [Subtraction](https://www.queworx.com/python/python-subtraction/)
* `*`: [Multiplication](https://www.queworx.com/python/python-multiplication/)
* `/`: [Division](https://www.queworx.com/python/python-division/)
* `//`: [Floor division](https://www.queworx.com/python/python-floor-division/) - round down division
* `%`: [Modulo operation](https://www.queworx.com/python/python-modulo/) - remainder from division
* `**`: [Power operation](https://www.queworx.com/python/python-power-operator/)

```python
>>> 8 + 3
11
>>> 8 - 3
5
>>> 8 * 3
24
>>> 8 /3
2.6666666666666665
>>> 16//3
5
>>> 16%3
1
>>> 2**3
8
```

<!-- wp:heading {"level":3} -->
<h3>Order of operation</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In Python, just like in Math order of arithmetic operations is governed by <strong>PEMDAS </strong>- Parentheses, Exponent, Multiplication, Division, Addition, Subtraction.</p>
<!-- /wp:paragraph -->

```python
>>> 3 + 2 * 8
19
>>> (3 + 2) * 8
40
```

&nbsp;
<!-- wp:heading -->
<h2>Comparison Operators</h2>
<!-- /wp:heading -->

* `==`: [Equal](https://www.queworx.com/python/python-equal/)
* `!=`: [Not equal](https://www.queworx.com/python/python-not-equal/)
* `>`: [Greater than](https://www.queworx.com/python/python-greater-than/)
* `<`: [Less than](https://www.queworx.com/python/python-less-than/)
* `>=`: [Greater than or equal](https://www.queworx.com/python/python-greater-than-or-equal/)
* `<=`: [Less than or equal](https://www.queworx.com/python/python-less-than-or-equal/)

```python
>>> a = 3
>>> b = 8
>>> a == b
False
>>> a != b
True
>>> a > b 
False
>>> a < b
True
>>> a >= b
False
>>> a <= b
True
```

&nbsp;
<!-- wp:heading -->
<h2>Assignment Operators</h2>
<!-- /wp:heading -->

* `=`: [Assignment](https://www.queworx.com/python/python-assignment/)
* `+=`: [Addition assignment](https://www.queworx.com/python/python-addition-assignment/)
* `-=`: [Subtraction assignment](https://www.queworx.com/python/python-subtraction-assignment/)
* `*=`: [Multiplication assignment](https://www.queworx.com/python/python-multiplication-assignment/)
* `/=`: [Division assignment](https://www.queworx.com/python/python-division-assignment/)
* `%=`: [Modulus assignment](https://www.queworx.com/python/python-modulus-assignment/)
* `//=`: [Floor division assignment](https://www.queworx.com/python/python-floor-division-assignment/)
* `**=`: [Power assignment](https://www.queworx.com/python/python-power-assignment/)
* `&=`: [Bitwise AND assignment](https://www.queworx.com/python/python-bitwise-and-assignment/)
* `|=`: [Bitwise OR assignment](https://www.queworx.com/python/python-bitwise-or-assignment/)
* `^=`: [Bitwise XOR assignment](https://www.queworx.com/python/python-bitwise-xor-assignment/)
* `>>=`: [Bitwise shift right assignment](https://www.queworx.com/python/python-bitwise-shift-right-assignment/)
* `<<=`: [Bitwise shift left assignment](https://www.queworx.com/python/python-bitwise-shift-left-assignment/)

&nbsp;
<!-- wp:heading -->
<h2>Logical Operators</h2>
<!-- /wp:heading -->

* `and`: [And logical operation](https://www.queworx.com/python/python-and/)
* `or`: [Or logical operation](https://www.queworx.com/python/python-or/)
* `not`: [Not logical operation](https://www.queworx.com/python/python-not/)

```python
>>> 5 > 3 and 2 > 1
True
>>> 5 > 3 or 2 > 5
True
>>> not(5 > 3) 
False
```

&nbsp;
<!-- wp:heading -->
<h2>Identity Operators</h2>
<!-- /wp:heading -->

* `is`: [Is the same object](https://www.queworx.com/python/python-is/)
* `is not`: [Is not the same object](https://www.queworx.com/python/python-is-not/)

```python
>>> x = [1, 2]
>>> y = [1, 2]
>>> x is y
False
>>> x is not y
True
```

&nbsp;
<!-- wp:heading -->
<h2>Membership Operators</h2>
<!-- /wp:heading -->

* `in`: [And logical operation](https://www.queworx.com/python/python-in/)
* `not in`: [Or logical operation](https://www.queworx.com/python/python-not-in/)

```python
>>> 5 in [3, 5, 8]
True
>>> 5 not in [3, 5, 8]
False
```

&nbsp;
<!-- wp:heading -->
<h2>Bitwise Operators</h2>
<!-- /wp:heading -->

* `&`: [Bitwise AND](https://www.queworx.com/python/python-bitwise-and/)
* `|`: [Bitwise OR](https://www.queworx.com/python/python-bitwise-or/)
* `^`: [Bitwise XOR](https://www.queworx.com/python/python-bitwise-xor/)
* `>>`: [Bitwise shift right](https://www.queworx.com/python/python-bitwise-shift-right/)
* `<<`: [Bitwise shift left](https://www.queworx.com/python/python-bitwise-shift-left/)

```python
>>> 9 & 3
1
>>> 9 | 3
11
>>> 9 ^ 3 
10
>>> 9 << 2
36
>>> 9 >> 2
2
```

&nbsp;
<!-- wp:heading {"level":2} -->
<h2>Combining Operators</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You can combine the various operators above in various ways, such as:</p>
<!-- /wp:paragraph -->

```python
>>>  x = 3
>>> (x < 5 + 1) and (x > 1)
True
```