Basic Operators in Python Programming

Python Operators Illustration
Explore Python's versatile operators: arithmetic, assignment, comparison, logical, bitwise, identity, and membership for efficient coding.

Python is a powerful and versatile programming language that supports multiple paradigms such as imperative, functional, object-oriented, and procedural. One of the fundamental features of Python is its rich set of operators that allow us to perform various operations on data types such as numbers, strings, lists, tuples, dictionaries, and sets.

In this blog, we will learn about the different types of operators in Python and how to use them with examples. We will cover the following topics:

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

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numeric values or variables. Python supports the following arithmetic operators:

OperatorNameExampleResult
+Addition5 + 27
Subtraction5 – 23
*Multiplication5 * 210
/Division5 / 22.5
//Floor division5 // 22
%Modulo5 % 21
**Exponentiation5 ** 225

Here are some examples of using arithmetic operators in Python:

# addition
a = 5 + 2
print(a) # output: 7

# subtraction
b = 5 - 2
print(b) # output: 3

# multiplication
c = 5 * 2
print(c) # output: 10

# division
d = 5 / 2
print(d) # output: 2.5

# floor division
e = 5 // 2
print(e) # output: 2

# modulo
f = 5 % 2
print(f) # output: 1

# exponentiation
g = 5 ** 2
print(g) # output: 25

Assignment Operators

Assignment operators are used to assign values to variables. The most common assignment operator is the equal sign (=), which assigns the value on the right to the variable on the left. For example:

x = 10 # assign 10 to x
y = x + 5 # assign x + 5 to y
print(x) # output: 10
print(y) # output: 15

Python also supports compound assignment operators, which combine an arithmetic operator with an assignment operator. For example:

x += 1 # equivalent to x = x + 1
y -= 2 # equivalent to y = y - 2
z *= 3 # equivalent to z = z * 3
w /= 4 # equivalent to w = w /4 

Here is a table of all the compound assignment operators in Python:

OperatorNameExampleEquivalent
+=Addition assignmentx += yx = x + y
-=Subtraction assignmentx -= yx = x – y
*=Multiplication assignmentx *= yx = x * y
/=Division assignmentx /= yx = x / y
//=Floor division assignmentx //= yx = x // y
%=Modulo assignmentx %= yx = x % y
**=Exponentiation assignmentx **= yx = x ** y

Comparison Operators

Comparison operators are used to compare two values or variables and return a boolean result (True or False). Python supports the following comparison operators:

OperatorNameExample
==Equal tox == y
!= or <> (deprecated)Not equal tox != y or x <> y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

Here are some examples of using comparison operators in Python:

x = 10
y = 20

# equal to operator
print(x == y) # output: False

# not equal to operator
print(x != y) # output: True

# greater than operator
print(x > y) # output: False

# less than operator
print(x < y) # output: True

# greater than or equal to operator
print(x >= y) # output: False

# less than or equal to operator
print(x <= y) # output: True

Logical Operators

Logical operators are used to combine two or more boolean expressions and return a boolean result. Python supports the following logical operators:

OperatorNameExample
andLogical ANDx and y
orLogical ORx or y
notLogical NOTnot x

The logical operators follow the rules of boolean algebra, which are summarized in the following truth tables:

xyx and y
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse
xyx or y
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse
xnot x
TrueFalse
FalseTrue

Here are some examples of using logical operators in Python:

x = 10
y = 20

# logical AND operator
print(x > 5 and y < 30) # output: True
print(x < 5 and y > 30) # output: False

# logical OR operator
print(x > 5 or y < 30) # output: True
print(x < 5 or y > 30) # output: False

# logical NOT operator
print(not x > 5) # output: False
print(not x < 5) # output: True

Bitwise Operators

Bitwise operators are used to perform operations on binary numbers (represented as bits). Python supports the following bitwise operators:

OperatorNameExample
&Bitwise ANDx & y
|Bitwise ORx | y
^Bitwise XORx ^ y
~Bitwise NOT~x
<<Bitwise left shiftx << n
>>Bitwise right shiftx >> n

The bitwise operators follow the rules of binary arithmetic, which are summarized in the following truth tables:

xyx & yx | yx ^ y
00000
01011
10011
11110

|x n x << n x >> n| |-|-|-|-| 0 0 0 0 0 1 0 0 1 0 1 1 1 1 10 0

Here are some examples of using bitwise operators in Python:

x = 10 # binary representation: 1010
y = 20 # binary representation: 10100

# bitwise AND operator
print(x & y) # output: 0 (binary representation: 00000)

# bitwise OR operator
print(x \| y) # output: 30 (binary representation: 11110)

# bitwise XOR operator
print(x ^ y) # output: 30 (binary representation: 11110)

# bitwise NOT operator
print(~x) # output: -11 (binary representation: -1011)

# bitwise left shift operator
print(x << 2) # output: 40 (binary representation: 101000)

# bitwise right shift operator
print(y >> 2) # output: 5 (binary representation: 101)

Identity Operators

Identity operators are used to compare the identity of two objects, not their values. Python supports the following identity operators:

  • is – returns True if both variables are the same object.
  • is not – returns True if both variables are not the same object.

In Python, every object has a unique identifier that can be obtained using the id() function. For example:

x = [1, 2, 3]
y = [1, 2, 3]
z = x

# print the id of each object
print(id(x)) # output: some number, e.g. 140714305581184
print(id(y)) # output: some other number, e.g.140714305581312 
print(id(z)) # output: same as x, e.g. 140714305581184

# use identity operators
print(x is y) # output: False
print(x is z) # output: True
print(x is not y) # output: True
print(x is not z) # output: False

Membership operators

Membership operators are used to test if a sequence is presented in an object. Python supports two membership operators:

  • in: Returns True if a sequence with the specified value is present in the object.
  • not in: Returns True if a sequence with the specified value is not present in the object.

You can use membership operators with different data types, such as strings, lists, tuples, sets, and dictionaries. For example:

x = "Hello"
y = ["a", "b", "c"]
z = {"name": "Alice", "age": 25}

# use membership operators
print("H" in x) # output: True
print("d" not in y) # output: True
print("age" in z) # output: True
print(25 in z) # output: False

Conclusion

In this blog, we have learned about the different types of operators in Python and how to use them with examples. We have covered:

  • Arithmetic operators for performing mathematical operations on numeric values or variables.
  • Assignment operators for assigning values to variables or updating them with compound operators.
  • Comparison operators for comparing two values or variables and returning a boolean result.
  • Logical operators for combining two or more boolean expressions and returning a boolean result.
  • Bitwise operators for performing operations on binary numbers (represented as bits).
  • Identity operators for comparing the identity of two objects, not their values.
  • Membership operators for testing if a sequence is presented in an object.

Operators are essential tools for manipulating data and performing various tasks in Python programming. By mastering the use of operators, you can write more concise, efficient, and elegant code.

I hope you enjoyed this blog and found it useful. If you have any questions or feedback, please leave a comment below. And if you liked this blog, please share it with your friends and colleagues who might be interested in learning Python.

Checkout previously covered Python topics.

Check out our latest blogs on WordPress, web development, AI, and more.

Explore our 16 free online tools for SEO optimization and more.

Thank you for reading!😊

Share:

Facebook
Twitter
Pinterest
LinkedIn

Leave a Comment

Hostinger

#1 Hosting Provider
Use code : 1FLUID35
For Extra 20% discount.

Onohosting

#1 India's Hosting Provider
Best services at Affordable price!
Starting ₹ 30 / month

Free Online SEO Tools

Explore versatile SEO tools: Meta Tag generator, PDF to JPG, QR Code Generator, IP Lookup, and much more.

Most Popular

Get The Latest Updates

Subscribe To Our Weekly Newsletter

No spam, notifications only about new products, updates.

Categories

Move Around On Site

Scroll to Top
small_c_popup.png

Learn how we helped 100 top brands gain success.

Let's have a chat