Python Operators (With Examples) (2024)

Operators are special symbols that perform operations on variables and values. For example,

print(5 + 6) # 11

Here, + is an operator that adds two numbers: 5 and 6.

Types of Python Operators

Here's a list of different types of Python operators that we will learn in this tutorial.

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Special Operators

1. Python Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc. For example,

sub = 10 - 5 # 5

Here, - is an arithmetic operator that subtracts two values or variables.

OperatorOperationExample
+Addition5 + 2 = 7
-Subtraction4 - 2 = 2
*Multiplication2 * 3 = 6
/Division4 / 2 = 2
//Floor Division10 // 3 = 3
%Modulo5 % 2 = 1
**Power4 ** 2 = 16

Example 1: Arithmetic Operators in Python

a = 7b = 2# additionprint ('Sum: ', a + b) # subtractionprint ('Subtraction: ', a - b) # multiplicationprint ('Multiplication: ', a * b) # divisionprint ('Division: ', a / b) # floor divisionprint ('Floor Division: ', a // b)# moduloprint ('Modulo: ', a % b) # a to the power bprint ('Power: ', a ** b) 

Output

Sum: 9Subtraction: 5Multiplication: 14Division: 3.5Floor Division: 3Modulo: 1Power: 49

In the above example, we have used multiple arithmetic operators,

  • + to add a and b
  • - to subtract b from a
  • * to multiply a and b
  • / to divide a by b
  • // to floor divide a by b
  • % to get the remainder
  • ** to get a to the power b

2. Python Assignment Operators

Assignment operators are used to assign values to variables. For example,

# assign 5 to x x = 5

Here, = is an assignment operator that assigns 5 to x.

Here's a list of different assignment operators available in Python.

OperatorNameExample
=Assignment Operatora = 7
+=Addition Assignmenta += 1 # a = a + 1
-=Subtraction Assignmenta -= 3 # a = a - 3
*=Multiplication Assignmenta *= 4 # a = a * 4
/=Division Assignmenta /= 3 # a = a / 3
%=Remainder Assignmenta %= 10 # a = a % 10
**=Exponent Assignmenta **= 10 # a = a ** 10

Example 2: Assignment Operators

# assign 10 to aa = 10# assign 5 to bb = 5 # assign the sum of a and b to aa += b # a = a + bprint(a)# Output: 15

Here, we have used the += operator to assign the sum of a and b to a.

Similarly, we can use any other assignment operators as per our needs.

3. Python Comparison Operators

Comparison operators compare two values/variables and return a boolean result: True or False. For example,

a = 5b = 2print (a > b) # True

Here, the > comparison operator is used to compare whether a is greater than b or not.

OperatorMeaningExample
==Is Equal To3 == 5 gives us False
!=Not Equal To3 != 5 gives us True
>Greater Than3 > 5 gives us False
<Less Than3 < 5 gives us True
>=Greater Than or Equal To3 >= 5 give us False
<=Less Than or Equal To3 <= 5 gives us True

Example 3: Comparison Operators

a = 5b = 2# equal to operatorprint('a == b =', a == b)# not equal to operatorprint('a != b =', a != b)# greater than operatorprint('a > b =', a > b)# less than operatorprint('a < b =', a < b)# greater than or equal to operatorprint('a >= b =', a >= b)# less than or equal to operatorprint('a <= b =', a <= b)

Output

a == b = Falsea != b = Truea > b = Truea < b = Falsea >= b = Truea <= b = False

Note: Comparison operators are used in decision-making and loops. We'll discuss more of the comparison operator and decision-making in later tutorials.

4. Python Logical Operators

Logical operators are used to check whether an expression is True or False. They are used in decision-making. For example,

a = 5b = 6print((a > 2) and (b >= 6)) # True

Here, and is the logical operator AND. Since both a > 2 and b >= 6 are True, the result is True.

OperatorExampleMeaning
anda and bLogical AND:
True only if both the operands are True
ora or bLogical OR:
True if at least one of the operands is True
notnot aLogical NOT:
True if the operand is False and vice-versa.

Example 4: Logical Operators

# logical ANDprint(True and True) # Trueprint(True and False) # False# logical ORprint(True or False) # True# logical NOTprint(not True) # False

Note: Here is the truth table for these logical operators.

5. Python Bitwise operators

Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit, hence the name.

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

In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

OperatorMeaningExample
&Bitwise ANDx & y = 0 (0000 0000)
|Bitwise ORx | y = 14 (0000 1110)
~Bitwise NOT~x = -11 (1111 0101)
^Bitwise XORx ^ y = 14 (0000 1110)
>>Bitwise right shiftx >> 2 = 2 (0000 0010)
<<Bitwise left shiftx 0010 1000)

6. Python Special operators

Python language offers some special types of operators like the identity operator and the membership operator. They are described below with examples.

Identity operators

In Python, is and is not are used to check if two values are located at the same memory location.

It's important to note that having two variables with equal values doesn't necessarily mean they are identical.

OperatorMeaningExample
isTrue if the operands are identical (refer to the same object)x is True
is notTrue if the operands are not identical (do not refer to the same object)x is not True

Example 4: Identity operators in Python

x1 = 5y1 = 5x2 = 'Hello'y2 = 'Hello'x3 = [1,2,3]y3 = [1,2,3]print(x1 is not y1) # prints Falseprint(x2 is y2) # prints Trueprint(x3 is y3) # prints False

Here, we see that x1 and y1 are integers of the same values, so they are equal as well as identical. The same is the case with x2 and y2 (strings).

But x3 and y3 are lists. They are equal but not identical. It is because the interpreter locates them separately in memory, although they are equal.

Membership operators

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

In a dictionary, we can only test for the presence of a key, not the value.

OperatorMeaningExample
inTrue if value/variable is found in the sequence5 in x
not inTrue if value/variable is not found in the sequence5 not in x

Example 5: Membership operators in Python

message = 'Hello world'dict1 = {1:'a', 2:'b'}# check if 'H' is present in message stringprint('H' in message) # prints True# check if 'hello' is present in message stringprint('hello' not in message) # prints True# check if '1' key is present in dict1print(1 in dict1) # prints True# check if 'a' key is present in dict1print('a' in dict1) # prints False

Output

TrueTrueTrueFalse

Here, 'H' is in message, but 'hello' is not present in message (remember, Python is case-sensitive).

Similarly, 1 is key, and 'a' is the value in dictionary dict1. Hence, 'a' in y returns False.

Also Read:

  • Precedence and Associativity of operators in Python
  • Python Operator Overloading
Python Operators (With Examples) (2024)

References

Top Articles
Latest Posts
Article information

Author: The Hon. Margery Christiansen

Last Updated:

Views: 6059

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: The Hon. Margery Christiansen

Birthday: 2000-07-07

Address: 5050 Breitenberg Knoll, New Robert, MI 45409

Phone: +2556892639372

Job: Investor Mining Engineer

Hobby: Sketching, Cosplaying, Glassblowing, Genealogy, Crocheting, Archery, Skateboarding

Introduction: My name is The Hon. Margery Christiansen, I am a bright, adorable, precious, inexpensive, gorgeous, comfortable, happy person who loves writing and wants to share my knowledge and understanding with you.