What are Python operators?
Python operators are used to perform operations on values and variables in general. These are standard symbols used in logical and arithmetic operations. In this article, we will look at various Python operators.
Note: OPERAND is the value on which the operator is applied.
Types of Operators:
Python language supports the following types of operators.
- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Arithmetic Operators:
Arithmetic operators are used to performing operations on two operands. It contains the + (addition), – (subtraction), * (multiplication), / (divide), % (remainder), /(floor division), and exponent (**) operators.
Operator | Description | Example a=10 b=20 |
---|---|---|
+ Addition | Adds values on either side of the operator. | a + b = 30 |
– Subtraction | Subtracts right operand from the left operand. | a – b = -10 |
* Multiplication | Multiplies values on either side of the operator | a * b = 200 |
/ Division | Divides left operand by right operand | b / a = 2 |
% Modulus | Returns the remainder after dividing the left operand by the right operand. | b % a = 0 |
** Exponent | An exponential (power) calculation is performed on the operands | a**b =10 to the power 20 |
// | The division of operands in which the result is the quotient with the digits after the decimal point is removed. If one of the operands is negative, the result is floored, that is, it is rounded away from zero (towards negative infinity). | 9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0 |
Comparison Operators:
Comparison operators compare the values of two operands and return a Boolean true or false result.
Operator | Description | Example a=10 b=20 |
---|---|---|
== | If the values of two operands are equal, the condition is satisfied. | (a == b) is not true. |
!= | If the values of two operands are not equal, the condition is met and returns true. | (a != b) is true. |
<> | If the values of two operands are not equal, then the condition becomes true. | (a <> b) is true. This is similar to != operator. |
> | If the value of the left operand exceeds the value of the right operand, the condition is satisfied. | (a > b) is not true. |
< | If the value of the left operand is less than the value of the right operand, then the condition is met. | (a < b) is true. |
>= | The condition is satisfied if the value of the left operand is greater than or equal to the value of the right operand. | (a >= b) is not true. |
<= | The condition is satisfied if the value of the left operand is less than or equal to the value of the right operand. | (a <= b) is true. |
Assignment Operators:
The assignment operators assign the right expression’s value to the left operand.
Operator | Description | Example |
---|---|---|
= | Values are assigned from the right operands to the left operands. | c = a + b assigns value of a + b into c |
+= Add AND | It multiplies the right operand by the left operand and assigns the result to the left operand. | c += a is equivalent to c = c + a |
-= Subtract AND | It takes the right operand and subtracts it from the left operand, then assigns the result to the left operand. | c -= a is equivalent to c = c – a |
*= Multiply AND | It multiplies the right operand by the left operand and assigns the resulting value to the left operand. | c *= a is equivalent to c = c * a |
/= Divide AND | It divides the left operand by the right operand and returns the result to the left operand. | c /= a is equivalent to c = c / a |
%= Modulus AND | It performs modulus with two operands and assigns the result to the left operand. | c %= a is equivalent to c = c % a |
**= Exponent AND | Calculates the exponential (power) of operators and assigns a value to the left operand. | c **= a is equivalent to c = c ** a |
//= Floor Division | It divides operators by their floors and assigns a value to the left operand. | c //= a is equivalent to c = c // a |
Bitwise Operators:
The bitwise operators operate on the values of the two operands bit by bit.
Operator | Description | Example a=60 b=13 |
---|---|---|
& Binary AND | If a bit exists in both operands, the operator copies it to the result. | (a & b) (means 0000 1100) |
| Binary OR | If a bit exists in either operand, the operator copies it to the result. | (a | b) = 61 (means 0011 1101) |
^ Binary XOR | It copies the bit if it is set in one operand but not both. | (a ^ b) = 49 (means 0011 0001) |
~ Binary One’s Complement | It is unary and has the effect of ‘flipping’ bits. | (~a ) = -61 (means 1100 0011 in 2’s complement form due to a signed binary number. |
<< Binary Left Shift | The value of the left operand is shifted left by the number of bits specified by the right operand. | a << 2 = 240 (means 1111 0000) |
>> Binary Right Shift | The value of the left operand is shifted right by the number of bits specified by the right operand. | a >> 2 = 15 (means 0000 1111) |
Logical Operators:
To make a decision, logical operators are primarily used in expression evaluation.
Operator | Description | Example a=10 b=10 |
---|---|---|
and Logical AND | If both operands are true, the condition is true. | (a and b) is true. |
or Logical OR | If either of the two operands is non-zero, the condition is satisfied. | (a or b) is true. |
not Logical NOT | Used to reverse the logical state of its operand. | Not(a and b) is false. |
Membership Operators:
The membership operators in Python look for members in a sequence, such as strings, lists, or tuples.
Operator | Description | Example |
---|---|---|
in | If it finds a variable in the specified sequence, it returns true; otherwise, it returns false. | x in y, here in results in a 1 if x is a member of sequence y. |
not in | If it does not find a variable in the specified sequence, it returns true; otherwise, it returns false. | x not in y, here not in results in a 1 if x is not a member of sequence y. |
Identity Operators:
The memory locations of two objects are compared using identity operators.
Operator | Description | Example |
---|---|---|
is | If the variables on either side of the operator point to the same object, true; otherwise, false. | x is y, here is results in 1 if id(x) equals id(y). |
is not | Evaluates to false if the variables on either side of the operator point to the same object and true otherwise. | x is not y, here is not results in 1 if id(x) is not equal to id(y). |
Note: also read about Math Functions in Python
Follow Me
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment