All About Operators in Python
Python has many operators that you can use in your program. This article will teach you more about them.
The Python programming language is one of the most widely used computer languages. While learning Python appears simple, you must grasp several fundamental ideas before moving on to further Python applications. Python is known for its plethora of operators, such as +, =,-, %, and *, which perform computations swiftly.
What is an Operator in Python?
An operator in Python is a symbol that performs a task on one or more operands. A variable or a value on which the operation is performed is referred to as an operand.
Depending on the type of operator utilized in the operation, the result changes. Operators are special symbols or constructs that are used to manipulate the values of operands.
For example, if you wish to add two variables or values together, you can do it with the addition operator. The operand values might be variables or any data type available in Python.
Best-suited Python for data science courses for you
Learn Python for data science with these high-rated online courses
Types of Operators in Python
We can categorize the operators into seven types, as listed below.
Arithmetic Operators
Python operators for basic mathematical operations are included in this list of Python arithmetic operators
Operators | Symbol |
Addition | + |
Subtraction | – |
Multiplication | * |
Division | / |
Modulus | % |
Exponential | ** |
Floor Division | // |
Assignment Operators
In Python, assignment operators are used to assigning values to variables and other objects.
Operators | Example |
= | X=2 |
+= | X+=2 |
-= | X-=2 |
*= | X*=2 |
%= | X%=2 |
**= | X**=2 |
//== | X//==2 |
|= | X|=2 |
^= | X^=2 |
&= | X&=2 |
Comparison Operators
To compare two values, comparison operators are employed. The comparison operators available in Python are listed below.
Operators | Example |
Equal | = |
Not Equal | != |
Greater than | > |
Less than | < |
Less than Equal to | <= |
Greater than Equal to | >= |
Logical Operators
To compare two conditional statements, logical operators are employed. The logical operators available in Python are listed below.
x>2 and x>3 |
x>2 or x>3 |
not(x>2 and x>3) |
Membership Operators
Membership operators are used to determining whether a sequence exists in an object. The membership operators in Python are listed below.
Operators | Example |
In-Returns true if sequence present in the object | X in Y will return true if X is present in Y |
Not in- Returns true if sequence is not present in the object | X not in Y will return true if X is not present in Y |
Identity Operators
Two items are compared using identity operators. The identity operators available in Python are listed below.
Operators | Example |
Is-Returns true if both variables are same objects | X is Y will return true is both has the same objects |
Is not-Returns true if both variables are not same objects | X is not Y will return true is both does not have same objects |
Bitwise Operators
The binary values are compared using bitwise operations. The bitwise operators available in Python are listed below.
Operators | Symbol |
Bitwise AND – sets all bits to 1, if all bits are 1 | & |
Bitwise OR – sets all bits to 1, if one of the bits is 1 | | |
Bitwise XOR – sets all bits to 1, if only one of the bits is 1 | ^ |
Bitwise NOT – Inverts all the bits | ~ |
Left Shift – Shift left by pushing zeros from the right and let the leftmost bits fell off | << |
Right Shift – Shift right by pushing zeros from the left and let the rightmost bits fell off | >> |
Conclusion
These are the seven different Python operators that you should know about.
Recently completed any professional course/certification from the market? Tell us what you liked or disliked in the course for more curated content.
Click here to submit its review with Shiksha Online.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio