Operators in Python
What are the operators?
In Python, operators are special symbols that perform specific operations on one or more operands (values or variables).
An operators is capable of manipulating a certain value or operand.
Operators are the backbone of any program and they used for everything from very simple functions like counting to complex algorithms like security encryption.
In Python, there are seven types of operators as follows;
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
1. Arithmetic operators
Arithmetic operators is a mathematical function that takes two operands and performs a calculation on numeric values.
Operator | Name |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
// | Exponential |
// | Floor Division |
Examples:
print("Value of the A is 10 and B is 5 ")
print("")
a = 10
b = 5
# Addition
c = a + b
print("Addition of 10 + 5 is : "+str(c))
# Subtraction
c = a - b
print("Subtraction of 10 - 5 is : "+str(c))
# Multiplication
c = a * b
print("Multiplication of 10 x 5 is : "+str(c))
# Division
c = a / b
print("Division of 10 / 5 is : "+str(c))
# Modulus
c = a % b
print("Modulus of 10 % 5 is : "+str(c))
# Exponential
c = a ** b
print("Exponential of 10 ** 5 is : "+str(c))
# Floor Division
c = a // b
print("Floor Division of 10 // 5 is : "+str(c))
2. Assignment operators
The assignment operator assigns the value of its right-hand operand to a variable, a property, or an indexer element given by its left-hand operand.
Operator | Name |
---|---|
= | Assignment |
+= | Add and Assign |
-= | Subtract and Assign |
*= | Multiply and Assign |
/= | Divide and Assign |
**= | Exponential and Assign |
//= | Floor Division and Assign |
Examples:
a = 10
print("Current Value of a is : "+str(a))
# Addition and Assign
a += 5
print("After a += 5 Value of A is : "+str(a))
print("Current Value of a is : "+str(a))
# Subtract and assign
a -= 5
print("After a -= 5 Value of A is : "+str(a))
print("Current Value of a is : "+str(a))
# Multiply and assign
a *= 5
print("After a *= 5 Value of A is : "+str(a))
print("Current Value of a is : "+str(a))
# Divide and assign
a /= 5
print("After a /= 5 Value of A is : "+str(a))
print("Current Value of a is : "+str(a))
# Exponentialand assign
a **= 5
print("After a **= 5 Value of A is : "+str(a))
print("Current Value of a is : "+str(a))
# Floor Division and assign
a //= 5
print("After a //= 5 Value of A is : "+str(a))
3. Comparison operators
Comparison operators are used to compare values and return a result that us True, False or Null.
Operator | Name |
---|---|
== | equal to |
!= | not equal to |
< | Less than |
> | Greater than |
>= | Greater than equal to |
<= | Less than equal to |
Examples:
a = 10
b = 5
# Equal to
print("Is 10 == 5 : "+str(a == b))
# Not equal to
print("Is 10 != 5 : "+str(a != b))
# Less than
print("Is 10 < 5 : "+str(a < b))
# Greater than
print("Is 10 > 5 : "+str(a > b))
# Less than equal to
print("Is 10 <= 5 : "+str(a <= b))
# Greater than equal to
print("Is 10 >= 5 : "+str(a >= b))
4. Logical operators
Logical operators are used to perform logical operations and include AND, OR or NOT.
Operator | Name |
---|---|
and | Logical AND |
or | Logical OR |
not | Logical NOT |
print("The Numbers are 10, 5, and 8")
print("")
a = 10
b = 5
c = 8
if a > b and a > c:
print("The Greater Number is : "+str(a))
elif b > a and b > c:
print("The Greater Number is : "+str(b))
else:
print("The Greater Number is : "+str(c))
5. Identity operators
We use identity operators to compare the memory location of two objects.
Operator | Description | Syntax |
---|---|---|
is | Returns True if both variables are same | x is y |
is not | Returns True if both variables are not same | x is not y |
Examples:
a = ["Apple","Coko"]
b = ["Apple", "Coko"]
c = a
print(" A is B are : "+str(a is b))
print("")
print(" A is C are : "+str(a is c))
print("")
print(" A == B are : "+str(a == b))
print("")
print(" A is not B are : "+str(a is not b))
print("")
6. Membership operators
We use membership operators to check whether a value or variable exists in a sequence
Operator | Description | Syntax |
---|---|---|
in | Returns True if the value is present in object | x in y |
not in | Returns True if value is not present in object | x not in y |
Examples:
print("")
a = ["Apple","Coko"]
print("")
print("Apple are present in A : "+str("Apple" in a))
print("")
print("Coko are not present in A : "+str("Coko" not in a))
print("")
7. Bitwise operators
Bitwise operators is an operator used to perform bitwise operations on bit patterns
Operator | Name | Syntax |
---|---|---|
& | AND | x & y |
| | OR | X | y |
^ | XOR | x ^ y |
~ | NOT | ~x |
>> | Bitwise right shift | X >> y |
<< | Bitwise left shift | X << y |
0 Comments
Please do not add Any Spam link in Comments !