Python Operators explained in the easiest way

Python Operators

Let's Decode
4 min readMay 17, 2023

Python is a versatile and powerful programming language, that offers a wide range of operators that enable developers to perform various operations on data. Python operators play a crucial role in manipulating values and controlling program flow from simple arithmetic calculations to complex logical evaluations. In this article, we will dive into the world of Python operators, exploring their types, functionalities, and best practices for their usage.

Python operator precedence
Fig: python operators with precedence
  1. Arithmetic Operators: Arithmetic operators in Python allow you to perform basic mathematical operations such as addition, subtraction, multiplication, division, and more. They include:
  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulo (%)
  • Floor Division (//)
  • Exponentiation (**)
>>print(2+3)
>>5
>>print(5-2)
>>3
>>print(2*3)
>>6 #asterisk(*) is used for multiplication
>>print(5/2)
>>2.5
#but if want only the integer part of 2.5 then we have to use //
>>print(5//2)
>>2
#modulo(%) is used to find the remainder
>>print(5%2)
>>1
#power operator is **
>>print(3**2) # 3 square is 9
>>9

2. Assignment Operators: Assignment operators are used to assign values to variables. They provide a concise way to update variable values based on computations. Examples of assignment operators in Python are:

  • Assignment (=)
  • Addition Assignment (+=)
  • Subtraction Assignment (-=)
  • Multiplication Assignment (*=)
  • Division Assignment (/=)
  • Modulo Assignment (%=)
  • Exponentiation Assignment (**=)
  • Floor Division Assignment (//=)
>>marks=20
>>marks+=10 #it is same as (marks= marks + 10)
>>marks-=10 #it is same as (marks= marks - 10)
>> marks/=2 #it is the same as (marks = marks/2)
#Actually when we want to store the values in the same variable
#and to reduce the statement we use assignment statements

3. Comparison Operators: Comparison operators are used to compare two values and determine their relationship. They return a Boolean value (True or False) based on the comparison. Some common comparison operators in Python include:

  • Equal to (==)
  • Not equal to (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)
>>print(2==3)
>>False
>>print(2!=3)
>>True
>>print(5>=6)
>>False

4. Logical Operators: Logical operators in Python are used to combine or manipulate Boolean values. They are particularly useful in controlling program flow and making decisions based on multiple conditions. The three logical operators in Python are:

  • Logical AND (and)
  • Logical OR (or)
  • Logical NOT (not)
#Logical AND will be true when all the values are True
>>x=True
>>y=True
>>x and y
>>True
#Logical OR will be False when all the values are False
>>y=False
>>x OR y
>>True #x is still True
>>x=False
>>x OR y
>>False #both are False
#Logical NOT is simply reversing the values
>>NOT x
>>True

5. Bitwise Operators: Bitwise operators manipulate the binary representation of values. They perform operations at the bit level, making them useful in scenarios such as data encryption or manipulation of binary data. Some commonly used bitwise operators in Python include:

  • Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise XOR (^)
  • Bitwise NOT (~)
  • Left Shift (<<)
  • Right Shift (>>)
#AND(&)Returns the result of bitwise AND of two integers
>>1&0
>>0
#OR(|) Returns the result of bitwise OR of two integers
>>1|0
>>1
#left shift shifts the bits of the first operand left by
#the specified number of bits
>> bin(0b1111 << 1)
>>'0b11110'
#right shift shifts the bits of the first operand right by
#the specified number of bits
>> bin(0b1111 >> 1)
>>'0b111'

6. Membership Operators: Membership operators are used to test whether a value belongs to a sequence or collection. They evaluate the presence of a value within a list, tuple, or string. Python provides two membership operators:

  • In
  • Not in
>>marks=[10,20,30,40]
>>print(20 in marks)
>>True
>>print(10 not in marks)
>>False

7. Identity Operators: Identity operators are used to compare the memory locations of two objects. They determine if two variables point to the same object in memory. Python has two identity operators:

  • is
  • is not
>> name1="Pritam"
>>name2="Rohan"
>>print(name1 is name2)
>>False

Conclusion: In conclusion, Python operators are a fundamental aspect of the language that enables developers to perform a wide range of operations on data. By gaining a deep understanding of arithmetic, assignment, comparison, logical, bitwise, membership, and identity operators, you can enhance your programming skills and efficiently manipulate data in Python. With this comprehensive guide to Python operators, you are equipped with the knowledge and techniques to write more expressive, efficient, and powerful code. Start utilizing Python operators today and unlock your programming potential with ease.

To stay updated with the latest insights on Python operators and expand your programming knowledge, be sure to follow me. Together, we will explore the intricacies of Python operators, delve into advanced techniques, and discover practical examples to sharpen your coding skills.

--

--

Let's Decode

From Complexity to Crystal Clear: Data Science and Engineering