In this tutorial, we will learn about Operators in Python and we will also cover different examples related to Python operators. And, we will cover these topics.
- Python operators
- Arithmetic operators
- Comparison operators
- Logical operators
- Bitwise operators
- Assignment operators
- Identity operators
- Membership operators
- Precedence operators
- Associativity operators
Python operators
Python operators are generally used to perform arithmetical and logical operations. The value that the operator operates is called an operand.
Example1:
In the following code, we will use an arithmetic operator. In the arithmetic operator, we use add function and print the output of this function on the screen.
# Examples of Arithmetic Operator
a = 12
b = 8
# Addition of numbers
add = a + b
print(add)
Output:
After running the above code we get the following output in which we can see that the + operator performs addition and the result is shown on the command prompt.
Example2:
# Examples of Logical Operator
a = True
b = False
# Print a and b is False
print(a and b)
Output:
After running the above code we get the following output in which we can see that the logical operator is printed on the command prompt.
Arithmetic operators
The arithmetic operators are defined as the operator which performs a mathematical operation such as addition, subtraction, multiplication, and addition.
Operators | Use of operators | Syntax |
+ | + is addition operator: Add two operands | a+b |
– | – is subtraction operator: Subtract two operands | a-b |
* | * is multiplication operator: Multiply two operands | a*b |
/ | / is division operator: Divides that first operand by the second | a/b |
% | % is modulus operator: Modulus return the operator when the first operand is divided by the second | a%b |
** | ** is Power operator: Power operator return first raised to power second | a ** b |
Example:
a = 15
b = 4
# Output: a + b = 19
print('a + b =',a+b)
# Output: a - b = 11
print('a - b =',a-b)
# Output: a * b = 60
print('a * b =',a*b)
# Output: a / b = 3.75
print('a / b =',a/b)
# Output: a // b = 3
print('a // b =',a//b)
# Output: a ** b = 50625
print('a ** b =',a**b)
Output:
After running the above code we get the following output in which we can see that arithmetic operations are performed on the screen.
Comparison operators
The comparison operator is defined as it compares the values. It returns the true value or false it depends upon the condition we use or apply.
Operators | Use of operators | Syntax |
> | > is Greater than operator: True if the left operand a is greater than the right operand b | a>b |
< | < is Less than operator: True if the right operand b is grated than the left operand a | a<b |
== | == Equal to an operator: True if both operands are equal to each other | a==b |
!= | != Not Equal to an operator: True if two operands are not equal | a!=b |
>= | >= Greater than equal to an operator: True if the left operand a is greater than equal to right operand b | a>=b |
<= | <= Less than or equal to an operator: True if the left operand a is less than equal to right operand b | a<=b |
Code:
# Examples of Relational Operators
a = 15
b = 45
print(a > b)
print(a < b)
print(a == b)
print(a != b)
print(a >= b)
print(a <= b)
Output:
After running the above code we get the following output in which we can see that the comparison operations are performed on the screen.
Logical operators
Logical operators perform Logical AND, Logical OR, and Logical NOT operation. It also combines conditional statements.
Operators | Use of operators | Syntax |
and | Logical AND: True is both the operand a and b is true | a and b |
or | Logical OR: True if either a is true or b is true | a or b |
not | Logical NOT: True if both the operand is false | a not b |
Example:
In the following code, we will see how the logical operator works.
# Examples of Logical Operator
a = False
b = True
# Print a and b is False
print(a and b)
# Print a or b is True
print(a or b)
# Print not a is False
print(not a)
Output:
After running the above code we get the following output in which we can see those logical operations are performed on the screen.
Bitwise operators
Bitwise operator is defined as the operator which acts as a bit and performs bit by bit operations.
Operators | Use of operators | Syntax |
& | Bitwise AND | a&b |
| | Bitwise OR | a|b |
~ | Bitwise NOT | a~b |
^ | Bitwise XOR | a^b |
>> | Bitwise right shift | a>>b |
<< | Bitwise left shift | a<<b |
Example:
# Examples of Bitwise operators
a = 20
b = 11
# Print bitwise AND operation
print(a & b)
# Print bitwise OR operation
print(a | b)
# Print bitwise NOT operation
print(~a)
# print bitwise XOR operation
print(a ^ b)
# print bitwise right shift operation
print(a >> 9)
# print bitwise left shift operation
print(a << 9)
Output:
After running the above code we get the following output in which we can see that bitwise operations are performed.
Assignment operators
The assignment operator is used to define that operator is used to assign the value to the variable.
Operators | Use of operators | Syntax |
= | We can assign the value to the right-side expression to the left side of the operand | a=b+c |
+= | Add right operand with the left side operand and then assign it to the left operand | a+=b a=a+b |
-= | Subtract left operand from the right operand then assign the value to the left operand | a-=b a=a-b |
*= | Multiply right operand with the left operand and then assign value to the left operand | a*=b a=a*b |
/= | Divide left operand with right operand and then assign value to the left operand | a/=b a=a/b |
%= | We can take the modulus using the left and right operand and then assign the value to the left operand | a%=b a=a%b |
**= | We can calculate the exponent value using operands and assign the value to the left operand | a**=b a=a**b |
&= | It can perform Bitwise And on operands and assign value to the left operand | a&=b a=a&b |
|= | It can perform Bitwise Or on operands and assign value to the left operand | a|=b a=a|b |
^= | It can perform bitwise XOR on operands and assign value to the left operand | a^=b a^b |
>>= | We can perform bitwise right sift on operand and assign the value to the left operand | a>>=b a>>b |
<<= | We can perform a bitwise left shift on operand and assign the value to the left operand | a<<=b a<<b |
Example:
# Examples of Assignment Operators
a = 25
# Assign value
b = a
print(b)
# Add and assign value
b += a
print(b)
# Subtract and assign value
b -= a
print(b)
# multiply and assign
b *= a
print(b)
# bitwise lishift operator
b <<= a
print(b)
Output:
After running the above code we get the following output in which we can see that the assignment operation is performed on the screen,
Identity operators
is or is not these operators are used to check the values are located on the same part of the memory.
is | This is true if the operand is identical |
is not | This is true if the operand is not identical |
Example:
x = 20
y = 30
z = x
print(x is z)
print(x is not y)
Output:
After running the above code we get the following output in which we can see that the identity operation is performed on the screen.
Membership operators
in and not in are the two membership operators this operator is used to test the values or variables are in sequence.
in | True if the value is found in the sequence |
not in | True if the value is not found in the sequence |
Example:
x = 34
y = 30
list = [20, 30, 40, 50, 60]
if (x not in list):
print("x is not present in given list")
else:
print("x is present in given list")
if (y in list):
print("y is present in given list")
else:
print("y is not present in given list")
Output:
After running the above code we get the following output in which we can see that membership operations are performed on the screen.
Precedence operators
The precedence operator is used as an expression with one or more operators with different precedence to determine which operation to perform first.
Example:
# Examples of Operator Precedence
# Precedence of '+' & '*'
expr = 20 + 30 * 40
print(expr)
# Precedence of 'or' & 'and'
name = "Elbert"
age = 0
if name == "Alex" or name == "John" and age >= 2:
print("Welcom to Pythontpoint.")
else:
print("Thank You For visit!!")
Output:
After running the above code we get the following output in which we can see that the precedence operation is performed on the screen.
Associativity operators
The associative operator is defined as an expression containing two or more precedence than the associative operator is used to determine.
Example:
# Examples of Associativity operator
print(1000 / 100 * 100)
print(50 - 20 + 30)
print(50 - (20 + 30))
print(20 ** 30 ** 20)
Output:
After running the above code we get the following output in which we can see that the associativity operation is performed on the screen.
So, in this tutorial, we discussed Python operators and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.
- Python operators
- Arithmetic operators
- Comparison operators
- Logical operators
- Bitwise operators
- Assignment operators
- Identity operators
- Membership operators
- Precedence operators
- Associativity operators