Python Operators

In Python, operators are essential tools used to perform operations on variables and values. They enable you to manipulate data and perform various tasks.

Types of Python Operators

Python categorizes operators into several groups:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Identity Operators
  • Membership Operators
  • Bitwise Operators

Arithmetic Operators

Arithmetic operators perform basic mathematical operations on numeric values:

OperatorNameExampleExpected Output
+Additionprint(12 + 8)# 20
Subtractionprint(18 - 7)# 11
*Multiplicationprint(6 * 3)# 18
/Divisionprint(20 / 4)# 5.0
%Modulusprint(17 % 5)# 2
**Exponentiationprint(3 ** 4)# 81
//Floor Divisionprint(19 // 4)# 4

Assignment Operators

Assignment operators are used to assign values to variables and can also combine assignments with arithmetic operations:

OperatorExampleExpected Value of x
=x = 7# x = 7
+=x += 4# x = 11
-=x -= 2# x = 9
*=x *= 3# x = 27
/=x /= 3# x = 9.0
%=x %= 4# x = 1.0
//=x //= 2# x = 0.0
**=x **= 3# x = 0.0

Comparison Operators

Comparison operators are used to compare two values and return a Boolean result:

OperatorNameExampleExpected Output
==Equalprint(9 == 9)# True
!=Not Equalprint(9 != 10)# True
>Greater Thanprint(8 > 6)# True
<Less Thanprint(5 < 9)# True
>=Greater Than or Equal Toprint(7 >= 7)# True
<=Less Than or Equal Toprint(4 <= 5)# True

Logical Operators

Logical operators are used to combine conditional statements:

OperatorDescriptionExampleExpected Output
andReturns True if both statements are trueprint(5 < 8 and 3 < 4)# True
orReturns True if one of the statements is trueprint(7 < 5 or 4 > 2)# True
notReverses the result, returns False if the result is trueprint(not(10 < 15 and 8 > 6))# False

Identity Operators

Identity operators compare the memory location of two objects:

OperatorDescriptionExampleExpected Output
isReturns True if both variables refer to the same objectx = ["book1", "book2"]
y = x
print(x is y)
# True
is notReturns True if both variables do not refer to the same objectx = ["book1", "book2"]
y = ["book1", "book2"]
print(x is not y)
# True

Membership Operators

Membership operators test if a value or sequence is present within an object:

OperatorDescriptionExampleExpected Output
inReturns True if the specified value is present in the objectbooks = ["book1", "book2", "book3"]
print("book2" in books)
# True
not inReturns True if the specified value is not present in the objectbooks = ["book1", "book2", "book3"]
print("book4" not in books)
# True

Bitwise Operators

Bitwise operators perform operations on binary numbers:

OperatorNameDescriptionExampleExpected Output
&ANDSets each bit to 1 if both bits are 1print(5 & 3)# 1
|ORSets each bit to 1 if one of the bits is 1print(5 | 3)# 7
^XORSets each bit to 1 if only one of the bits is 1print(5 ^ 3)# 6
~NOTInverts all the bitsprint(~5)# -6
<<Zero fill left shiftShift left by pushing zeros in from the rightprint(4 << 2)# 16
>>Signed right shiftShift right by pushing copies of the leftmost bit from the leftprint(8 >> 2)# 2

Conclusion

Python provides a wide range of operators to perform various operations on data. Whether you’re manipulating numbers, making comparisons, or working with conditions, these operators help you achieve your desired outcome efficiently. Understanding how to use them effectively is crucial to writing powerful and flexible Python code.

Leave a Comment

The technical storage or access that is used exclusively for statistical purposes. New led ultra thin lights motion sensor night light wireless under cabinet lights for kitchen closet cabinet lighting.