Like other programming languages Python also uses various Python Operators. Here, we will discuss each descriptively. What is Operator? Operators are special symbols(like +,*,-,, etc.), which are used to performing operations on the variables and values.
Table of Content:
What’s inside: Types of Python Operators Arithmetic Operators Assignment operators Comparison operators Logical operators Identity operators Membership operators Bitwise operators FAQs |
Types of Python Operators
Python language divides the operators into the following types:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Let us have a look at all the operators one by one.
Arithmetic Operators
Arithmetic Operators in Python perform various arithmetic operations like addition, subtraction, multiplication, division, %modulus, and so on.
Suppose : a=10 and b=5
Operator | Description | Example |
---|---|---|
(+)Addition | Adds values. | a + b = 15 |
(-)Subtraction | Subtracts right-hand value from left-hand value. | a – b = 10 |
(*)Multiplication | Multiplies the values | a * b = 50 |
(/) Division | Divides left-hand Value by right-hand value. | b / a = 0.2 |
(%) Modulus | Divides left-hand operand or value by right-hand operand and return the remainder | b % a =2 |
(**) Exponent | Exponent exponential ( or power) calculation on operators | a**b =10 to the power 5 |
// | Floor Division – The division of values where the result is the quotient or remainder. But if one of the operands is (-) negative, the result will be floored, that is rounded away from zero (towards the negative infinity) − | 9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0 |
Assignment Operators
Like other programming languages, Python also has various assignment operators.
Let a=5, b=6, and c=7.
Operator | Description | Example |
---|---|---|
= | this sign assigns values from right side to left side operand | c = a + b assigns value of a + b into c |
+= Add AND | It adds right to the left operand and assigns the result to the left side operand | c += a is equivalent to c = c + a |
-= Subtract AND | this subtracts right from the left side operand and assigns the result to the left side operand | c -= a is equivalent to c = c – a |
*= Multiply AND | It multiplies the right with the left operand and assigns the result to the left side operand | c *= a is equivalent to c = c * a |
/= Divide AND | It divides left with the right operand and assign the result to the left side operand | c /= a is equivalent to c = c / a |
%= Modulus AND | It takes modulus using two operands and assigns the result to the left side | c %= a is equivalent to c = c % a |
**= Exponent AND | Performs the exponential ( or power) calculation on operators and assign value to the left side operand. | c **= a is equivalent to c = c ** a |
//= Floor Division | It performs floor division on both side operators and assigns the value to the left operand | c //= a is equivalent to c = c // a |
Python Comparison Operators
Operator | Meaning | Example |
---|---|---|
> | Greater than sign – True if the left operand is greater than the right. | x > y |
< | Less than sign – True if left operand is less than the right. | x < y |
== | Equal to sign – True if both operands are equal. | x == y |
!= | Not equal to sign – True if operands are not equal. | x != y |
>= | Greater than or equal to sign – True if the left operand is greater than or equal to the right. | x >= y |
<= | Less than or equal to sign – True if the left operand is less than or equal to the right. | x <= y |
Logical operators in Python
The Logical operators in PythonPython uses for conditional statements are true or false. Logical operators are AND, OR, and NOT.
For Python logical operators below, mentioned following condition are applied:
- AND operator – This operator is returned TRUE if both the operands (right side and left side) are true.
- OR operator– OR returns TRUE if either of the operand (right side or left side) is true.
- NOT operator– this operator returns TRUE if the operand is false.
For Example: Here, we get a true or false value based on the value of X and Y.
X = True Y = False print(('X and Y is',X and Y)) print(('X or Y is',X or Y)) print(('not a is',not Y))
Identity operators
Identity python operators use to compare the objects, not only if they are equal, but if they are the same object, with the same memory location, we can say that Identity Operators use to compare the memory location of the two objects.
There are two types of identity operators used in PythonPython.
- Is Operator: It returns true if two variables point to the same object.
- Is not Operator: This returns false if two variables or operands the same object, or it returns false if two variables are not the same.
a1 = 5
b1 = 5
a2 = 'Hello'
b2 = 'Hello'
a3 = [1,2,3]
b3 = [1,2,3]
# Output will be: False
.
print(a1 is not b1)
# Output will be: True
.
print(a2 is b2)
# Output will be: False
.
print(a3 is b3)
Output:
![Python Operators - Python3 Tutorials 1 python operators](https://technicalblog.in/wp-content/uploads/2020/11/python-operators.jpg)
Membership operators
Membership Python operators are types: in and not in.
In membership operators: True if the variable is found in the sequence. For instance:
not in membership operators: True if the variable is not found in the sequence.
x = 'Hello Guys'
y = {1:'a',2:'b'}
# Output will be: True
print('H' in x)
# Output will be: True
print('hello' not in x)
# Output will be: True
print(1 in y)
# Output will be: False
print('a' in y)
Output:
![Python Operators - Python3 Tutorials 2 python operators](https://technicalblog.in/wp-content/uploads/2020/11/python-operators-1.jpg)
Bitwise operators
Bitwise Python operators act on operands or values as if they were strings of binary digits. It operates on the bits and performs bit by bit operation.
For example: 2 = 10
in binary and 7 = 111
.
Operator | Name | Description |
---|---|---|
& | AND | AND is used to sets each bit to 1 if it exists in both operands |
| | OR | It copies 1 bit if it exists in either operand. |
^ | XOR | The resulting bit will be 0 if both the bits are the same, or it will be 1 if both bits are different; |
~ | NOT | It calculates the negation of each bit of the operand or values; that is, if the bit is 1, the resulting bit will be 0 and vice versa. |
<< | Zero fill left shift | The left operand value is moved left side by the number of bits present in the right side operand. |
>> | Signed right shift | The left operand is moved right side by the number of bits present in the right side operand. |
FAQs in Python Operators:
While using Python IDLE, by how many spaces are the code suites indented?
Ans: 4 Spaces
What is the output of code: print(2%6)
Ans: 2
What is the output of code: print(2 ** 3 ** 2)
Ans: 512
What is the output of the below-mentioned code of assignment operator?
y = 10
x = y += 2
print(x)
Ans: Syntax error
Must Read:
In the above, we have completed the discussion about Python operators, their type, uses, and descriptions with suitable examples. In the coming one, we will discuss the conditions statements and loops in PythonPython.
For now Thank you for being here 🙂