# Python Operators Example | Arithmetic, Comparison, Logical, Assignment

Python Operators Example | Arithmetic, Comparison, Logical, Assignment Operator is today’s topic. In this post, you will learn everything about different kinds of operators in Python, their syntax, and how to use them with brief examples. Python Operator is used to performing operations on variables and values. Operators are used to perform the operations on values and variables.

Operators can manipulate an individual item and returns the result. The data items are referred to as operands or arguments. Keywords or special characters either represent operators.

For example, for identity operators, we use the keyword “is” and “is not”.

**What is Operator in Python**

Content Overview

- 1 What is Operator in Python
- 2 Python Operators Example
- 3 Python Arithmetic Operator
- 4 Arithmetic Operators Example In Python
- 5 Python Assignment Operator
- 6 Python Comparison Operator
- 7 Comparison Operators Example In Python
- 8 Python Logical Operator
- 9 Logical Operators Example In Python
- 10 Python Identity Operator
- 11 Identity Operators Example In Python
- 12 Python Membership Operator
- 13 Membership Operators Example In Python
- 14 Python Bitwise Operator
- 15 Summary of Python Operators

Operators are unique symbols in Python that carry out arithmetic or logical calculation. A value that the Python operator runs on is called the operand.

Let’s see the following code example.

print(11 + 21)

See the output.

➜ pyt python3 app.py 32 ➜ pyt

Here, **+** is the operator that performs addition. **11** and **21** are the operands, and **32** is the output of the operation.

**Python Operators Example**

Python divides the operators into the following groups:

- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators

**Python Arithmetic Operator**

Arithmetic operators are used with the numeric values to perform everyday mathematical operations.

Arithmetic Operators perform different arithmetic computations like addition, subtraction, multiplication, division, %modulus, exponent, etc.

There are several methods for arithmetic computation in Python like you can use the eval function, declare variable & calculate, or call functions.

Operator | Meaning | Example |
---|---|---|

+ | Add two operands or unary plus. | a + b +2 |

– | Subtract the right operand from the left or unary minus. | a – b -2 |

* | Multiply two operands. | a * b |

/ | Divide left operand by a right one (always results into float). | a / b |

% | Modulus: the remainder of the division of left operand by the right. | a % b (remainder of a/b) |

// | Floor division: division that results in the whole number adjusted to the left in the number line. | a // b |

** | Exponent: left operand raised to the power of right. | a**b (a to the power b) |

**Arithmetic Operators Example In Python**

See the following code.

# app.py a = 11 b = 21 print('a + b =', a+b) print('a - b =', a-b) print('a * b =', a*b) print('a / b =', a/b) print('a // b =', a//b) print('a ** b =', a**b)

See the output.

➜ pyt python3 app.py a + b = 32 a - b = -10 a * b = 231 a / b = 0.5238095238095238 a // b = 0 a ** b = 7400249944258160101211 ➜ pyt

**Python Assignment Operator**

Python assignment operators are used to assigning the value of the right operand to a left operand. Multiple assignment operators used in Python are (+=, – = , *=, /= , etc.).

**Example**: Python assignment operators is to assign the value, for example.

a = 11 is a simple assignment operator that assigns the value 5 on the right to the variable **a** on the left.

There are various compound operators like a += 11 that adds to the variable and later assigns the same. It is equivalent to a = a + 11.

Operator | Example | Equivatent to |
---|---|---|

= | a = 11 | a = 11 |

+= | a += 11 | a = a + 11 |

-= | a -= 11 | a = a – 11 |

*= | a *= 11 | a = a * 11 |

/= | a /= 11 | a = a / 11 |

%= | a %= 11 | a = a % 11 |

//= | a //= 11 | a = a // 11 |

**= | a **= 11 | a = a ** 11 |

&= | a &= 11 | a = a & 11 |

|= | a |= 11 | a = a | 11 |

^= | a ^= 11 | a = a ^ 11 |

>>= | a >>= 11 | a = a >> 11 |

<<= | a <<= 11 | a = a << 11 |

**Python Comparison Operator**

Comparison operators are used to compare two values.

It either returns **True** or **False** based to the condition.

Operator | Meaning | Example |
---|---|---|

> | Greater than: True if the left operand is greater than the right | a > b |

< | Less than: True if the left operand is less than the right | a < b |

== | Equal to: True if both the operands are equal | a == b |

!= | Not equal to: True if the operands are not equal | a != b |

>= | Greater than or equal to: True if the left operand is greater than or equal to the right | a >= b |

<= | Less than or equal to: True if the left operand is less than or equal to the right | a <= b |

**Comparison Operators Example In Python**

See the following code.

# app.py a = 11 b = 21 print('b > a is', b > a) print('b < a is', b < a) print('b == a is', b == a) print('b != a is', b != a) print('b >= a is', b >= a) print('b <= a is', b <= a)

See the output.

➜ pyt python3 app.py b > a is True b < a is False b == a is False b != a is True b >= a is True b <= a is False ➜ pyt

**Python Logical Operator**

Logical operators are used to combine the conditional statements.

Logical operators are used for conditional statements are True or False.

Logical operators are AND, OR and NOT. For logical operators, the following condition is applied.

- For AND operator: It returns TRUE if both the operands (right side and left side) are True.
- For OR operator: It returns TRUE if either of the operand (right side or left side) is True.
- For NOT operator: returns TRUE if an operand is False.

Operator | Meaning | Example |
---|---|---|

and | True if both the operands are true. | a and b |

or | True if either of the operands is true. | a or b |

not | True if the operand is false (complements the operand). | not a |

**Logical Operators Example In Python**

See the following code.

# app.py a = True b = False print('a and b is', a and b) print('a or b is', a or b) print('not a is', not a)

See the output.

➜ pyt python3 app.py a and b is False a or b is True not a is False ➜ pyt

**Python Identity Operator**

The **is** and **is not** are the identity operators in Python.

They are used to check if the two values (or variables) are located on the identical part of the memory.

Two equal variables do not imply that they are same.

Identity operators are used to comparing the objects, not if they are equal, but if they are the same object, with the same memory location.

Operator | Meaning | Example |
---|---|---|

is | True if the operands are same (refer to the same object) | a is True |

is not | True if the operands are not similar (do not refer to the same object) | a is not True |

**Identity ****Operators Example In Python**

See the following code.

# app.py a1 = 11 b1 = 21 a2 = 'Eleven' b2 = 'Eleven' a3 = [11, 21, 10] b3 = [11, 21, 10] print(a1 is not b1) print(a2 is b2) print(a3 is b3)

See the output.

➜ pyt python3 app.py True True False ➜ pyt

**Python Membership Operator**

Membership operators are used for testing if the sequence is presented in an object.

The **in** and **not in** are the membership operators in Python. They are used to test whether the value or variable is found in a sequence (string, list, tuple, set, and dictionary).

Operator | Meaning | Example |
---|---|---|

in | True if value/variable is found in a sequence | 5 in a |

not in | True if the value/variable is not found in a sequence | 5 not in a |

**Membership ****Operators Example In Python**

See the following code.

# app.py a = 'Millie Bobby Brown' b = {1:'x',2:'y'} print('B' in a) print('Millie' not in a) print(1 in b) print('a' in b)

See the output.

➜ pyt python3 app.py True False True False ➜ pyt

**Python Bitwise Operator**

Bitwise operators act on the operands as if they were a string of binary digits.

It operates a bit by bit, hence the name.

Operator | Meaning | Example |
---|---|---|

& | Bitwise AND | a & b = 0 (`0000 0000` ) |

| | Bitwise OR | a | b = 14 (`0000 1110` ) |

~ | Bitwise NOT | ~a = -11 (`1111 0101` ) |

^ | Bitwise XOR | a ^ b = 14 (`0000 1110` ) |

>> | Bitwise right shift | a >> b = 2 (`0000 0010` ) |

<< | Bitwise left shift | a << b = 40 (`0010 1000` ) |

For example, **2** is **10** in the binary, and 7 is **111 **in binary.

**Summary of Python Operators**

Operators in the programming language are used to perform various operations on values and variables. In Python, you can use operators like the following.

- There are several methods for arithmetic calculation in Python as you can use the eval function, declare variable & calculate, or call functions.
- Comparison operators often referred to as relational operators are used to comparing the values on either side of them and determine a relation between them.
- Python assignment operators are to assign a value to a variable.
- Python also allows you to use the compound assignment operator, in the complicated arithmetic calculation, where you can assign a result of one operand to the other.
- For AND operator: It returns TRUE if both the operands (right side and left side) are True.
- For OR operato: It returns TRUE if either of the operand (right side or left side) is True.
- For NOT operator- returns TRUE if an operand is False.
- Two membership operators are used in Python. (in, not in).
- It gives a result based on the variable present in the particular sequence or string.
- The two identify operators used in the Python are is, is not.
- It returns true if the two variables point the same object and false otherwise.

Finally, Python Operators Example | Arithmetic, Comparison, Logical, Assignment is over.