Javaexercise.com

Python Operators

Python operators

Python is a high level, interpreted programming language, designed and developed by Guido van Rossum in the 1990s. It is known for its simplicity and easy to understand capability. It is one of the most popular programming languages present in the software industry. One of the most important but often overlooked concepts in python is that of operators. Let us have a look at them in detail.

 

What is an operator?

In python, an operator, just like other programming languages, is used by programmers to perform operations. Operators can be used with variables and values. The variable or value used with an operator is called an operand. Operators are usually used to perform logical or arithmetic operations.

 

Syntax

The syntax of using operators is very straightforward and is as follows. We simply use the operator with the required operands. Usually the operator is present between the operands. 

<operand1> <operator> <operand2>

 

The different types of operators in python are arithmetic, comparison, logical and assignment. Let’s have a look at them in detail - 

Arithmetic operators

Operator Usage
+ a + b
- a - b
* a * b
/ a / b
% a % b
** a ** b
// a // b

Addition operator (+) is used to add two operands. Let us look at the python code and corresponding output for this example - 

a = 2
b = 3
c=a+b
print(c)

Output

5

Subtraction operator (-) is used to subtract the second operand from the first. Let us look at the python code and corresponding output for this example - 

a=3
b=2
c=a-b
print(c)

Output

1

Multiplication operator (*) is used to multiply two operands. Let us look at the python code and corresponding output for this example - 

a=2
b=3
c=a*b
print(c)

Output

6

Division operator (/) is used to divide the first operand by the second. Let us look at the python code and corresponding output for this example - 

a=4
b=2
c=a/b
print(c)

Output

2

 

Modulus operator (%) is used to divide the first operand by the second and get the corresponding remainder. Let us look at the python code and corresponding output for this example - 

a=5
b=2
c=a%b
print(c)

Output

1

 

Exponentiation operator (**) is used to raise the first operand to the power of the second operand. Let us look at the python code and corresponding output for this example - 

a=2
b=3
c=a**b
print(c)

Output

8

 

Floor division operator (//) is used to divide the first operand by the second and get the floor. Let us look at the python code and corresponding output for this example - 

a=5
b=2
c=a//b
print(c)

Output

2

Comparison operators

Operator Usage
< a < b
a > b
<= a <= b
>= a >= b
== a == b
!= a != b

Less than operator (<) is used to check if the left operand is less than the right one. Let us look at the python code and corresponding output for this example - 

a=2
b=3
c=a<b
print(c)

Output

True

Greater than operator (>) is used to check if the left operand is greater than the right one. Let us look at the python code and corresponding output for this example - 

a=2
b=3
c=a>b
print(c)

Output

False

Less than or equal to operator (<=) is used to check if the left operand is less than or equal to the right one. Let us look at the python code and corresponding output for this example - 

a=2
b=3
c=a<=b
print(c)

Output

True

Greater than or equal to operator (>=) is used to check if the left operand is greater than or equal to the right one. Let us look at the python code and corresponding output for this example - 

a=2
b=3
c=a>=b
print(c)

Output

False

 

Equal to operator (==) is used to check if the left operand is equal to the right one. Let us look at the python code and corresponding output for this example - 

a=2
b=3
c=a==b
print(c)

Output

False

 

Not equal to operator (!=) is used to check if the left operand is not equal to the right one. Let us look at the python code and corresponding output for this example - 

a=2
b=3
c=a!=b
print(c)

Output

True

Logical operators

Operator Usage
and a and b
or a or b
not not(a)

And operator (and) returns True if both the left and right operands are True. Let us look at the python code and corresponding output for this example - 

a=True
b=True
c=a and b
print(c)

Output

True

Or operator (or) returns True if atleast one of the left or right operands is True. Let us look at the python code and corresponding output for this example - 

a=True
b=False
c=a or b
print(c)

Output

True

Not operator (not) returns the negation of the operand. Let us look at the python code and corresponding output for this example - 

a=True
c=not(a)
print(c)

Output

False

 

Bitwise operators

Bitwise operators are used to perform bit by bit operations on the operands. The bits here represent the binary representation of the operands. For example, 4 in binary is equivalent to 0000 0100 and 6 in binary is equivalent to 0000 0110. Let us look at the bitwise operators - 

Operator Meaning Usage
& Bitwise AND - 1 if both bits are 1 a & b
| Bitwise OR - 1 if atleast one bit is 1 a | b
~ Bitwise NOT - complement all bits ~a
^ Bitwise XOR - 1 if both bits are same a ^ b
<< Bitwise LEFT SHIFT - Shift all bits to the left by number of times equal to operand on the right a << b
>> Bitwise RIGHT SHIFT - Shift all bits to the right by number of times equal to operand on the right a >> b
a = 6
b = 4
c=a&b
print(c)
c=a|b
print(c)
c=~a
print(c)
c=a^b
print(c)
a=a>>2
print(c)
c=a<<2
print(c)

Output

4

6

-7

2

1

24

Assignment operators

Assignment operator (=) is used to assign a value to a variable. Let us look at the python code and corresponding output for this example - 

x = 3

print(x)

Output

3

There are other special types of assignment operators that are used as a shortcut or a combination of arithmetic operators with assignment. For example, (+=) is used to assign a value to a variable after adding a value to it. Let us look at the python code and corresponding output for this example - 

x = 3

print(x)

x += 1

print(x)

Output

3

4

Let us look at other shortcut assignment statement with arithmetic operators that have been explained before - 

Operator Usage
+= a += b
-= a -= b
*= a *= b
/= a /= b
%= a %= b
**= a **= b
//= a //= b
&= a &= b
|= a |= b
^= a ^= b
>>= a >>= b
<<= a <<= b
= a = b

Conclusion

In this topic, we have learned the use and advantages of operators in a Python program, following a few running examples, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to [email protected] in case of any suggestions