In this article, you will learn about Python operators as well as you will learn different types of operators in Python using appropriate examples. In previous we have seen all about Python comments and its usages. Any programming languages, Operators play a major role using Operators we can perform different types of operations.
In Python, operators are used to performing a specific task using variables and values.
Before going through this tutorial, Let’s understand what is operators in Python?
What is Python operators ?
In Python, the operator is a symbol which performs a specific task using variable and values. The value that the operator operates is called an operand.
x = 12 y = 23 print(x + y)
In the above example + is the operator that performs addition. 12 and 23 is the operand in this example and + is the operator.
In Python, There are different types of operators available:
Types of Python Operators
- Arithmetic Operator
- Assignment Operator
- Comparison Operator
- Logical Operator
- Identity Operator
- Membership Operator
- Bitwise Operator
Python Arithmetic Operators:
In Python, Arithmetic Operators are used to performing Mathematical operations like addition, division, multiplication, and subtraction.
|+||Add two operands or unary plus|
|–||Subtract right operand from left operand or unary minus|
|*||Multiply two operands|
|/||Divide left operand by right one|
|%||Modulus – the remainder of the division.|
x = 18 y = 5 print('x + y:- ', x + y) print('x -y:- ', x - y) print('x * y:- ', x * y) print('x / y:- ', x / y) print('x % y:- ', x % y) print('x // y:- ', x // y) print('x ** y:- ', x ** y)
x + y:- 23 x -y:- 13 x * y:- 90 x / y:- 3.6 x % y:- 3 x // y:- 3 x ** y:- 1889568
Python Assignment Operator:
In Python, the assignment operator is used to assigning the value to variables.
|=||x = 10||x = 10|
|+=||x += 20||x = x + 20|
|-=||x -= 19||x = x – 10|
|*=||x *= 20||x = x * 20|
|/=||x /= 5||x = x / 5|
|//=||x //= 20||x = x // 20|
|**=||x /= 20||x = x / 20|
|%=||x %= 20||x = x % 20|
|&=||x &= 20||x = x & 20|
||=||x |= 20||x = x | 20|
|=||x >>= 20||x = x >> 20|
|<<=||x <<= 20||x = x << 20|
Python Comparison operators are used to compare one operand to another operand.
|<=||Less Than or Equal To|
|>=||Greater Than or Equal To|
|!=||Not Equal To|
x = 25 y = 19 print(x > y) print(x < y) print(x != y) print(x <= y) print(x >= y) print(x == y)
True False True False True False
Logical Operators in Python:
In Python, Logical operators are used to combining conditional statements.
|or||Return True if one of the statements is true|
|and||Return True if both the statement are true|
|not||Reverse the result, return False if the result is true.|
x = 12 print(x < 20 or x < 3) print(x < 30 and x > 4) print(not(x < 30 and x > 4))
True True False
Python Identity Operators:
is and is not are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal do not imply that they are identical.
|is||Return True if the object is identical|
|is not||Return true if the object is identical|
x1 = 23 y1 = 23 x2 = 'Python' y2 = 'Python' x3 = [1,2,3] y3 = [1,2,3] print(x1 is y1) print(x2 is not y2) print(x3 is y3)
True False False
Python Membership Operator:
is used to check if a sequence present in the object.
|not in||Return |
x = 'Programming Funda' print('P' in x) print('Funda' not in x)
Python Bitwise Operator
Bitwise operators are used to comparing binary numbers.
|>>||Bitwise ZERO FILL RIGHT SHILT|
|<<||Bitwise ZERO FILL LEFT SHILT|
So, In this article, you have learned what is Python operators and Python operators types and usages. In Python, Operators are used to performing any particular task for example addition, division, comparison, and so many more operations can perform by operators.
I hope this tutorial will have helped you. If you like this Python operators tutorial, if you have any doubt regarding Python operators, Then you can comments. if you like this Python operators tutorial, Please share it with your friends who want to learn Python from scratch to advanced.