Table of Contents
Python Operators
In Python Programming, operators in general functions will perform operations on values and variables. These operators are considered as the standard symbol which functions for logical and arithmetic operations.
Let us check out this article to learn more about the different types of Python Operators.
What are Operators in Python?
Python and several programming languages have different types of operators which are the symbol and even referred to as keywords. They will do operations on one or more operands.
These operators will offer different operations such as addition, subtraction, multiplication, and division.
Types Of Operators in Python
a. Arithmetic Operator
b. Comparison Operator
c. Assignment Operator
d. Logical Operator
e. Bitwise Operators
f. Membership Operators
g. Identity Operators
Python Arithmetic Operators
Arithmetic Operators mainly function to do basic mathematical operations. Hence, the variable will have 10 and the variable b has 20.
Operator | Name | Example |
---|---|---|
+ | Addition | a + b = 30 |
– | Subtraction | a – b = -10 |
* | Multiplication | a * b = 200 |
/ | Division | b / a = 2 |
% | Modulus | b % a = 0 |
** | Exponent | a**b =10**20 |
// | Floor Division | 9//2 = 4 |
Python Comparison Operators
These operators have values on either side of them and can analyze the relation between them. These are also known as Relational Operators.
Suppose variable a has 10 and variable b has 20
Operator | Name | Example |
---|---|---|
== | Equal | (a == b) is not true. |
!= | Not equal | (a != b) is true. |
> | Greater than | (a > b) is not true. |
< | Less than | (a < b) is true. |
>= | Greater than or equal to | (a >= b) is not true. |
<= | Less than or equal to | (a <= b) is true. |
Python Assignment Operators
Assignment operators will provide values to several variables. Python assignment operators table is provided below
Operator | Example | Same As |
---|---|---|
= | a = 10 | a = 10 |
+= | a += 30 | a = a + 30 |
-= | a -= 15 | a = a – 15 |
*= | a *= 10 | a = a * 10 |
/= | a /= 5 | a = a / 5 |
%= | a %= 5 | a = a % 5 |
**= | a **= 4 | a = a ** 4 |
//= | a //= 5 | a = a // 5 |
&= | a &= 5 | a = a & 5 |
|= | a |= 5 | a = a | 5 |
^= | a ^= 5 | a = a ^ 5 |
>>= | a >>= 5 | a = a >> 5 |
<<= | a <<= 5 | a = a << 5 |
Python Bitwise Operators
Bitwise Operator will perform on bits and functions bit by bit operations. Moreover, these operations will compare binary numbers.
Operator | Name | Example |
---|---|---|
& | AND | a & b |
| | OR | a | b |
^ | XOR | a ^ b |
~ | NOT | ~a |
<< | Zero fill left shift | a << 3 |
>> | Signed right shift | a >> 3 |
Python Logical Operators
These operators will further compile more than 2 conditions and monitor the final result. The logical operators which are provided here will support the Python language.
Operator | Name | Example |
---|---|---|
and | AND | a and b |
or | OR | a or b |
not | NOT | not(a) |
Python Membership Operators
These operators will test for membership in a sequence like the strings, lists, or tuples.
Operator | Description | Example |
---|---|---|
in | Returns True if it finds a variable in the specified sequence, false otherwise. | a in b |
not in | an in b | a not in b |
Python Identity Operators
Identity operators will compare the memory locations of two objects. Those identity operators are given below
Operator | Description | Example |
---|---|---|
is | Returns True if both variables are the same object and false otherwise. | a is b |
is not | Returns True if both variables are not the same object and false otherwise. | a is not b |
Python Operators Precedence
The table below has operators from the highest precedence to the lowest.
Sr.No. | Operator & Description |
---|---|
1 | **Exponentiation (raise to the power) |
2 | ~ + –Complement, unary plus and minus (method names for the last two are +@ and -@) |
3 | * / % //Multiply, divide, modulo and floor division |
4 | + –Addition and subtraction |
5 | >> <<Right and left bitwise shift |
6 | &Bitwise ‘AND’ |
7 | ^ |Bitwise exclusive `OR’ and regular `OR’ |
8 | <= < > >=Comparison operators |
9 | <> == !=Equality operators |
10 | = %= /= //= -= += *= **=Assignment operators |
11 | is is notIdentity operators |
12 | in not inMembership operators |
13 | not or andLogical operators |
Conclusion
To conclude, this article will educate us on the different types of Python operators. Python Arithmetic Operators, Python Bitwise operators, Python Logical operators, and several other operators are included in this.
Python Operators- FAQs
Q1. What is the in operator in Python?
Ans. The in-operator will monitor if a value is in a collection of values
Q2. What is init in Python?
Ans. It is considered a special method for the initialization of the object.
Q3. What is str in Python?
Ans. Python consists of a built-in string class referred to as “str” with many handy features . String literals will be enclosed by either double or single quotes.
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together