Python Operators

In programming, an operator is usually a special symbol or combination of symbols that allows you to perform specific operations on variables and values. This operation can act on one or more operands.

  • OPERATORS: These are the special symbols. Eg- + , * , /, etc.
  • OPERAND: It is the value on which the operator is applied.

In this article, we will look into different types of Python operators.

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators

Arithmetic Operators:

Arithmetic operators are used to perform mathematical operations like addition, subtraction, and multiplication. The assignment operator is one of the most frequently used operators in Python. The operator consists of a single equal sign (=), and it operates on two operands. The left-hand operand is typically a variable, while the right-hand operand is an expression.

Operator Operation Examples
+ Addition 5 + 5 = 10

3 + 2 = 5

Subtraction 20 – 5 = 15

6 – 10 = -4

* Multiplication 2 * 6 = 12

8 * 2 = 16

/ Division (float) 8 / 4 = 2.0

15 / 3 = 5.0

// Floor Division 10 // 3 = 3

20 // 5 = 4

% Modulus 10 % 3 = 1

9 % 3 = 0

** Power 3 ** 3 = 27

2 ** 4 = 16

Precedence of Arithmetic Operators in Python:

The precedence of Arithmetic Operators in Python is as follows:

  1. P – Parentheses
  2. E – Exponentiation
  3. M – Multiplication (Multiplication and division have the same precedence)
  4. D – Division
  5. A – Addition (Addition and subtraction have the same precedence)
  6. S – Subtraction

Examples:

  • print((6 + 3) – (6 + 3))

Parenthesis have the highest precedence and need to be evaluated first.

The calculation above reads 9 – 9 = 0

  • print(100 + 5 * 3)

Multiplication has higher precedence than addition and needs to be evaluated first.

The calculation above reads 100 + 15 = 115

Assignment Operators:

Assignment operators are used to assign values to variables. Following is a table that shows all Python assignment operators.

Operator Name Examples
= Assignment Operator a = 10

b = 5

+= Addition Assignment a += 6 (a = a + 6)

b += 9 (b = b + 9)

-= Subtraction Assignment a -= 2 (a = a – 2)

b -= 0 (b = b – 0)

*= Multiplication Assignment x *= 5 (x = x * 5)

y *= 7 (y = y * 7)

/= Division Assignment c /= 12 (c = c / 12)

d /= 6 (d = d / 12)

%= Remainder Assignment y %= 2 (y = y % 2)

z %= 1 (z = z % 1)

**= Exponent Assignment k **= 15 (k = k ** 15)

p **= 8 (p = p ** 8)

//= Floor Division Assignment a //= 6 (a = a // 6)

c //= 2 (c = c // 2)

Comparison Operators:

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

Operator Name Examples
== Equal 4 == 6 gives us False

4 == 4 gives us True

!= Not equal 3 != 5 gives True

3 != 3 gives False

> Greater than 5 > 2 gives True

2 > 5 gives False

< Less than 2 < 5 gives True

5 < 2 gives False

>= Greater than or Equal to 3 >= 5 gives False

5 >= 3 gives True

<= Less than or Equal to 5 <= 5 gives True

1 <= 0 gives False

Logical Operators:

Logical operators are used to combine conditional statements. They are used in design-making.

Operator Description Examples
AND If both operands are true, then the condition becomes true. a AND b

x AND z

OR If at least one operand is true, then the condition becomes true.  a OR b

x OR y

NOT True, if the operand is false. vice-versa NOT a

NOT b

Example:

p = True 

q = False 

and_result = p and q # False 

or_result = p or q # True 

not_result_p = not p # False 

not_result_q = not q # True

Identity operators:

Identity operators are used to compare objects to determine whether they are actually the same object in the same memory location rather than whether they are equal.

Operator Description Example
is True if both variables are the same object a is b
Is not True if both variables are not the same object a is not b

Example:

a = 10

b = 20

c = a

 print(a is not b)

print(a is c)

Output:

True

True

Membership operators:

Membership operators are used to test if a sequence is presented in an object.

Operator Description Example
in True if the value is found in the sequence x in y
Not in True if the value is not found in the sequence x not in y
  • x = [“apple”, “banana”]

print(“banana” in x)

Result:

True

# returns True because a sequence with the value “banana” is in the list

  • numbers = [1, 2, 3, 4, 5]

in_result = 3 in numbers

not_in_result = 6 not in numbers

Result:

True

True

Conclusion:

Python operators are necessary tools for performing various data operations. You will be better prepared to handle a variety of programming challenges if you can master these operators. To write clean and efficient code, use them sparingly and in accordance with best practices.

Visited 9 times, 1 visit(s) today