What are operators in Python - bold black text on a white background

Operators in Python are symbols or special keywords that perform operations on operands (values or variables). Think of them as the verbs in the Python language that tell the interpreter what kind of action to take.

Here’s a breakdown of the main categories of operators in Python:

1. Arithmetic Operators:

These operators perform mathematical calculations.

OperatorNameExampleResult
+Addition3 + 58
-Subtraction10 - 46
*Multiplication6 * 742
/Division15 / 35.0
//Floor Division15 // 35
%Modulus16 % 51
**Exponentiation2 ** 38
  • / (Division): Always returns a float, even if the result is a whole number.
  • // (Floor Division): Divides and rounds the result down to the nearest whole number (integer).
  • % (Modulus): Returns the remainder of the division.
  • ** (Exponentiation): Raises the left operand to the power of the right operand.

2. Comparison Operators:

These operators compare two operands and return a boolean value (True or False).

OperatorNameExampleResult
==Equal to5 == 5True
!=Not equal to5 != 6True
>Greater than8 > 3True
<Less than2 < 7True
>=Greater than or equal to5 >= 5True
<=Less than or equal to4 <= 6True

3. Logical Operators:

These operators are used to combine or modify boolean values.

OperatorNameExampleResult
andANDTrue and FalseFalse (True only if both operands are True)
orORTrue or FalseTrue (True if at least one operand is True)
notNOTnot TrueFalse (Reverses the boolean value of the operand)

4. Assignment Operators:

These operators are used to assign values to variables.

OperatorNameExampleEquivalent to
=Assignmentx = 5x = 5
+=Add and assignx += 3x = x + 3
-=Subtract and assignx -= 2x = x - 2
*=Multiply and assignx *= 4x = x * 4
/=Divide and assignx /= 2x = x / 23
//=Floor divide and assignx //= 3x = x // 3
%=Modulus and assignx %= 5x = x % 5
**=Exponentiate and assignx **= 2x = x ** 2

5. Identity Operators:

These operators check if two operands refer to the same object in memory.

OperatorNameExampleResult
isIsx is yTrue if x and y refer to the same object, False otherwise.
is notIs notx is not yTrue if x and y do not refer to the same object, False otherwise.

6. Membership Operators:

These operators check if a value is a member of a sequence (like a string, list, or tuple).

OperatorNameExampleResult
inIn'a' in 'apple'True if 'a' is found in the string 'apple', False otherwise.
not inNot in'b' not in 'apple'True if 'b' is not found in the string 'apple', False otherwise.

7. Bitwise Operators (Less Common for Beginners):

These operators perform operations at the level of individual bits in integers.

OperatorName
&Bitwise AND
``
^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Right shift

You’ll encounter and use these operators frequently as you write Python code. Understanding how they work is fundamental to building more complex logic and performing various operations on data.


Discover more from Shafaat Ali Education

Subscribe to get the latest posts sent to your email.

Leave a comment

apple books

Buy my eBooks on Apple Books. Thanks! Shafaat Ali, Apple Books

Discover more from Shafaat Ali Education

Subscribe now to keep reading and get access to the full archive.

Continue reading