Python Operators
Operators are special symbols in python that carries the arithmetic or logical operation. Operators are basically constructs which can manipulate the value of operands.
Lets consider the expression 6 + 2 = 8. Here, + is the operator that performs addition.
Types of Operator
In Python language there are operators listed below :
- 1. Arithmetic Operators
- 2. Comparison (Relational) Operators
- 3. Logical Operators
- 4. Assignment Operators
- 5. Bitwise Operators
- 6. Membership Operators
- 7. Identity Operators
Lets start with one by one with examples
1. Arithmetic Operators
Arithmetic Operator performs the mathematical operations like addition, subtraction, division and multiplication.
Lets see an example
# Examples of Arithmetic Operator
a = 4
b = 3
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
# Division(float) of number
div = a / b
# Modulo of both number
mod = a % b
# Power
exp = a **b
# Division(floor) of number
floor = a // b
print(add)
print(sub)
print(mul)
print(div)
print(mod)
print(exp)
print(floor)
Output:
7
1
12
1.3333333333333333
1
64
1
2. Comparison Operators
The Comparison operators actually compare the values on either sides of them and decide the relation among them.It either returns True or False according to the condition.
Note :
Even though == and is might look same but they aren't , which is a big mistake often done by beginners.
a is b checks whether the A and B point to the same object in memory or not whereas a==b checks for if the value stored in a and b are same.
# Examples of Relational Operators
a = 15
b = 20
# a == b is False
print(a == b)
# a > b is False
print(a > b)
# a < b is True
print(a < b)
# a != b is True
print(a != b)
# a >= b is False
print(a >= b)
# a <= b is True
print(a <= b)
Output:
False
False
True
True
False
True
3. Logical Operators
Logical operators perform Logical AND, Logical OR and Logical NOT operations.
There are following logical operators supported by Python language.
#Examples of Logical Operator
in this article i have explained brief details about Operators available in python
Recommended Post :
#operators in python
#python not equal
#python ternary operator
#python xor
#python power
#python and operator
#python division
#python logical operators
#python modulus
0 Comments
If you have any doubt, Please let me know.