Blog header background

Python Operators: A Beginner’s Guide

Updated on September 27, 2024

7 min read

Copy link
Share on WhatsApp

Python operators are the symbols that represent the arithmetic or logical operations, and the value on which the operator is operating is called an operand. There are seven categories of operations in Python: arithmetic, assignment, comparison, logic, identity, membership, and boolean operators.

Introduction to Operators in Python

The operators in Python language are special symbols that enable the manipulation of variables and values through various operations. It can be categorised into several types, including arithmetic operators for basic mathematical calculations (such as addition and multiplication), comparison operators for evaluating relationships between values (like equal to or greater than), logical operators for combining conditional statements (such as AND and OR), and assignment.

brochure-banner-bg

POSTGRADUATE PROGRAM IN

Multi Cloud Architecture & DevOps

Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.

Types of Operators in Python

There are seven categories of Python operators:

Python Operators

Assignment Operators

In Python language, the assignment operator is used to perform some basic operations on values and variables. These are the special symbols that carry out arithmetic, logical and bitwise computation in Python. The value the operator operates on is known as the Operand.

Operator Description Syntax
= They assign a value to a variable x = 10
 += This adds and assigns x += (equiv to x =x +5)
-= It subtracts and assigns x -= 3 (equiv to x= x-3)
*= It  multiples and assigns x  *= 2 (equiv to x = x-3)
/= It divides and assigns x/=4 (equiv. To x =x /4)
//= Floor division and assigns x // =3 (equiv to x = x//3)
%= Its modules and assigns  x %=2 (equiv to x=x %2)
**= It exponentiation and assigns x **  =2 (eqiv to x = x **2)
&= It bitwise and and assigns x &= 1 (equiv to x = x  & 1)
^= It bitwise XOR and assigns x ^=4 (equiv to x = x^4)
>>= Right shift and assign  x >> 1 (eqiv. to  x= x>>1)
<<= Left Shift and assigns x << 1 (equiv to x = x<< 1)

Program

x = 10

print(f"Initial value of x: {x}")

x = 10

print(f"After assignment (x = 10): {x}")

x += 5

print(f"After addition (x += 5): {x}")

x -= 3

print(f"After subtraction (x -= 3): {x}")

x *= 2

print(f"After multiplication (x *= 2): {x}")

x /= 4

print(f"After division (x /= 4): {x}")

x //= 3

print(f"After floor division (x //= 3): {x}")

Output

Initial value of x: 10

After assignment (x = 10): 10

After addition (x += 5): 15

After subtraction (x -= 3): 12

After multiplication (x *= 2): 24

After division (x /= 4): 6.0

After floor division (x //= 3): 2.0

Arithmetic Operators

Python language arithmetic operators perform basic mathematical operations like addition, subtraction, multiplication, and division.

In Python, 3.x, the result of division is a floating-point, while in Python 2.x, the division of 2 integers was an integer. To obtain an integer result in Python, we can use 3.x floored (// integer).

Operator Description Syntax
+ This adds two operands  x + y
 It subtracts two operands x-y
* It multiplies two operands x * y
/ It divides the first operand by the second x / y
// It divides the first operand by the second operator x // y
% It returns the remainder when the first operand is divided by the second operator x  % y
 ** It returns first raised to power second x ** y
skill-test-section-bg

82.9%

of professionals don't believe their degree can help them get ahead at work.

Comparison Operators in Python

These operators compare the value of the left operand and the right operand and return either true or false. Let’s consider a equals 10 and b equals 20.

Operator Description Example
== These operators compare the values of two operands. If the two operands are equal, then it will return true or false.  (x == y) is not true
!= If the values of two operands are not equal, then the condition becomes true. (a!=b) is true
<>  If the values of two operands are not equal, then the condition becomes true. (a<>b) is true. This is similar to the!=operator
If the value of the left operand is greater than that of the right, the condition is true. (a>b) is not true.
>= If the value of the left operand is less than or equal to that of the right, the condition is true.  (a <=b) is true.
<= If the value of the left operand is less than or equal to that of the right, the condition is true. (a<=b) is true

Program


a = 10

b = 20

 

# Comparison Operators

print(f"Comparing a = {a} and b = {b}:")

# Equal to

print(f"a == b: {a == b}")  # False

# Not equal to

print(f"a != b: {a != b}")  # True

# Greater than

print(f"a > b: {a > b}")    # False

# Less than

print(f"a < b: {a < b}")     # True

# Greater than or equal to

print(f"a >= b: {a >= b}")  # False

# Less than or equal to

print(f"a <= b: {a <= b}")  # True

c = 10

print(f"\nComparing a = {a} and c = {c}:")

print(f"a == c: {a == c}")   # True

print(f"a != c: {a != c}")   # False

print(f"a < c: {a < c}")     # False

print(f"a > c: {a > c}")     # False

Output


Comparing a = 10 and b = 20:

a == b: False

a != b: True

a > b: False

a < b: True

a >= b: False

a <= b: True

 

Comparing a = 10 and c = 10:

a == c: True

a != c: False

a < c: False

a > c: False

Logical Operators in Python

There are three operators in Python: Logical AND, Logical OR, and Logical Not. It is used to combine conditional statements.

Operator Description Syntax
and It is true if both the operands are true x and y
or It is true if either of the operands is true x or y
not It is true if the  operand is false not x

Identity Operators in Python

In the C language, there are two identity operators: is and is not. Both are used to check if two values are located in the same part of the memory.

Operator Example Description
is x is y It returns true if both variables are the same object
is not x is not y It returns true if both variables are not the same object.

Membership Operators in Python

This operator searches for the value in the specified sequence. If the value is found, it returns true; otherwise, it returns false. The not-in operator returns true if the value specified is not found in the given sequence.

Operator Example Description
in 5 in {1,2,3,4,5} It returns true if it finds the corresponding value in a specified sequence. Otherwise, it returns false.
not in P5 not in {5,3,4,5} It returns true if it does not find the corresponding value in a given sequence and true otherwise.

Bitwise Operators in Python

These operators perform operations on binary numbers. If the number given is not binary, it is converted to binary internally, and an operation is performed. These operations are generally performed bit by bit.

Operator Name Description
& AND It sets each bit to 1 if both bits are 1
| OR  It sets each bit to 1 if one of the two bits is 1
^ XOR It sets each bit to 1 if one of the two bits is 1
~ NOT This covers all the bits.
<<  Zero-fill left shift This shift left by pushing zeroes in from the right and letting the leftmost bits fall off
>> Signed right shift It shifts right by pushing copies of the leftmost bit in from the left and letting the rightmost bits fall off.

Conclusion

In this article, we learned about the Python Operators. They are among the fundamental tools used for performing all ranges of operations-from the very basic, like arithmetic and comparisons, to complex logical evaluations and memory management. There are several types of categorised and membership operators, each of which has different purposes in programming. The operator allows for writing more efficient and readable source codes, which makes implementation of decision-making processes easier or just manipulation of the data structure easier.

FAQs
How do membership operators work?
The membership operators check if a value exists within a sequence (like a list or string). For example, 5 in [1,2,3,4,5,] returns True, while six not in [1,2,3,4,5] returns true.
How do identity operators work?
Identity operators (is and is not) check if two variables point to the same object in memory, for example. A is b and returns true if both variables are the same.
How do bitwise operators work?
The bitwise operators manipulate individual bits of integer values. For example, 5 and 3 (binary 0101 and 0011) result in 1 (binary 0001).
What is the purpose of the ** operator?
The ** operator is used for exponentiation in Python. For example, 2 ** 3 raises 2 to the power of 3, resulting in 8.

Updated on September 27, 2024

Link
Loading related articles...
Python Operators: A Guide for Absolute Beginners