In this tutorial, We will learn about Python Operators, their syntax, and how to use operators in Python.
What are operators in python?
Operators are special symbols in Python.
They carry out arithmetic or logical computation. The operand is the value that the operator operates on.
For example:
>>> 4+5 9
Here, + is the operator that performs addition. 4 and 5 are the operands and 9 is the output of the operation.
Arithmetic operators
Arithmetic operators are used to performing mathematical operations like addition, multiplication, subtraction, etc.
Operator | Meaning | Example |
---|---|---|
+ | Add two operands or unary plus | x + y+ 2 |
– | Subtract right operand from the left or unary minus | x – y- 2 |
* | Multiply two operands | x * y |
/ | Divide left operand by the right one (always results into float) | x / y |
% | Modulus – remainder of the division of left operand by the right | x % y (remainder of x/y) |
// | Floor division – division that results into whole number adjusted to the left in the number line | x // y |
** | Exponent – left operand raised to the power of right | x**y (x to the power y) |
Example #1: Arithmetic operators in Python
a = 10 b = 5 # Output: a + b = 15 print('a + b =',a+b) # Output: a - b = 5 print('a - b =',a-b) # Output: a * b = 50 print('a * b =',a*b) # Output: a / b = 2 print('a / b =',a/b) # Output: a // b = 2 print('a // b =',a//b) # Output: a ** b = 100000 print('a ** b =',a**b) #same as 10*10*10*10*10
Output
a + b = 14 a - b = 5 a * b = 50 a / b = 2 a // b = 2 a ** b = 100000
Comparison operators
We can compare values with the help of Comparison operators. It returns either True or False according to the condition.
Operator | Meaning | Example |
---|---|---|
> | Greater than – True if left operand is greater than the right | a > b |
< | Less than – True if left operand is less than the right | a < b |
== | Equal to – True if both operands are equal | a == b |
!= | Not equal to – True if operands are not equal | a != b |
>= | Greater than or equal to – True if left operand is greater than or equal to the right | a >= b |
<= | Less than or equal to – True if left operand is less than or equal to the right | a <= b |
Example #2: Comparison operators in Python
a = 8 b = 5 # Output: a > b is False print('a > b is',a>b) # Output: a < b is True print('a < b is',a<b) # Output: a == b is False print('a == b is',a==b) # Output: a != b is True print('a != b is',a!=b) # Output: a >= b is False print('a >= b is',a>=b) # Output: a <= b is True print('a <= b is',a<=b)
Output
a > b is False a < b is True a == b is False a != b is True a >= b is False a <= b is True
Logical operators
Operator | Meaning | Example |
---|---|---|
and | True if both the operands are true | x and y |
or | True if either of the operands is true | x or y |
not | True if operand is false (complements the operand) | not x |
Example 3: Logical Operators in Python
a = True b = False print('a and b is',a and b) print('a or b is',a or b) print('not a is',not a)
Output
a and b is False a or b is True not a is False
Here is the truth table for these operators.
Python Bitwise Operators
Bitwise operators are used to comparing (binary) numbers:
Operator | Meaning | Description |
---|---|---|
& | Bitwise AND | Sets each bit to 1 if both bits are 1 |
| | Bitwise OR | Sets each bit to 1 if one of two bits is 1 |
~ | Bitwise NOT | Inverts all the bits |
^ | Bitwise XOR | Sets each bit to 1 if only one of two bits is 1 |
>> | Bitwise right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
<< | Bitwise left shift | shift left by pushing zeros in from the right and let the leftmost bits fall off |