An operator is a symbol (such as +, x, etc.) that represents an operation. An operation is an action or procedure which produces a new value from one or more input values called operands. There are two types of operators: unary and binary. Unary operator operates only on one operand, such as negation. On the other hand, binary operator operates on two operands, which includes addition, subtraction, multiplication, division, exponentiation operators etc. Consider an expression 3+8, here 3 and 8 are called operands, while '+' is called operator. The operators can also be categorized into:
Table enlists the arithematic operators with a short note on the operators.
Operator |
Description |
+ |
Addition operator- Add operands on either side of the operator. |
- |
Subtraction operator - Subtract right hand operand from left hand operand. |
* |
Multiplication operator - Multiply operands on either side of the operator. |
/ |
Division operator - Divide left hand operand by right hand operand. |
% |
Modulus operator - Divide left hand operand by right hand operand and return remainder. |
** |
Exponent operator - Perform exponential (power) calculation on operands. |
// |
Floor Division operator - The division of operands where the result is the quotient in which the digits after the decimal point are removed. |
The following example illustrates the use of the above discussed operators.
>>> a=20
>>> b=45.0
>>> a+b
65.0
>>> a-b
-25.0
>>> a*b
900.0
>>> b/a
2.25
>>> b%a
5.0
>>> a**b
3.5184372088832e+58
>>> b//a
2.0
A relational operator is an operator that tests some kind of relation between two operands. Table enlist the relational operators with description.
Operator |
Description |
== |
Check if the values of two operands are equal. |
!= |
Check if the values of two operands are not equal. |
<> |
Check if the value of two operands are not equal (same as !=operator). |
> |
Check if the value of left operand is greater than the value of right operand. |
< |
Check if the value of left operand is less than the value of right operand. |
>= |
Check if the value of left operand is greater than or equal to the value of right operand. |
<= |
Check if the value of left operand is less than or equal to the value of right operand. |
The following example illustrates the use of the above discussed operators.
>>> a,b=20,40
>>> a==b
False
>>> a!=b
True
>>> a<>b
True
>>> a>b
False
>>> a<b
True
>>> a>=b
False
>>> a<=b
True
Assignment operator is an operator which is used to bind or rebind names to values. Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement. An augmented assignment expression like x+=1 can be rewritten as x=x + 1. Table enlist the assignment operators with description.
Operator |
Description |
= |
Assignment operator- Assigns values from right side operand to left side operand. |
+= |
Augmented assignment operator- It adds right side operand to the left side operand and assign the result to left side operand. |
-= |
Augmented assignment operator- It subtracts right side operand from the left side operand and assign the result to left side operand. |
*= |
Augmented assignment operator- It multiplies right side operand with the left side operand and assign the result to left side operand. |
/= |
Augmented assignment operator- It divides left side operand with the right side operand and assign the result to left side operand. |
%= |
Augmented assignment operator- It takes modulus using two operands and assign the result to left side operand. |
**= |
Augmented assignment operator- Performs exponential (power) calculation on operands and assigns value to the left side operand. |
//= |
Augmented assignment operator- Performs floor division on operators and assigns value to the left side operand. |
The following example illustrates the use of the above discussed operators.
>>> a,b=20,40
>>> c=a+b
>>> c
60
>>> a,b=2.0,4.5
>>> c=a+b
>>> c
6.5
>>> c+=a
>>> c
8.5
>>> c-=a
>>> c
6.5
>>> c*=a
>>> c
13.0
>>> c/=a
>>> c
6.5
>>> c%=a
>>> c
0.5
>>> c**=a
>>> c
0.25
>>> c//=a
>>> c
0.0
A bitwise operator operates on one or more bit patterns or binary numerals at the level of their individual bits. Table enlists the bitwise operators with description.
Operator |
Description |
& |
Binary AND operator- Copies corresponding binary 1 to the result, if it exists in both operands. |
| |
Binary OR operator- Copies corresponding binary 1 to the result, if it exists in either operand. |
^ |
Binary XOR operator- Copies corresponding binary 1 to the result, if it is set in one operand, but not both. |
~ |
Binary ones complement operator- It is unary and has the effect of flipping bits. |
<< |
Binary left shift operator- The left side operand bits are moved to the left side by the number on right side operand. |
>> |
Binary right shift operator- The left side operand bits are moved to the right side by the number on right side operand. |
The following example illustrates the use of the above discussed operators.
>>> a,b=60,13
>>> a & b
12
>>> a | b
61
>>> a^b
49
>>> ~a
-61
>>> a<<2
240
>>> a>>2
15
In the above example, the binary representation of variables a and b are 00111100 and 00001101, respectively. The above binary operations example is tabulated in table.
Bitwise operation |
Binary representation |
Decimal representation |
a & b |
00001100 |
12 |
a l b |
00111101 |
61 |
a ^ b |
00110001 |
49 |
~a |
11000011 |
-61 |
a<<2 |
11110000 |
240 |
a>>2 |
00001111 |
15 |
Logical operators compare boolean expressions and return a boolean result. Table enlist the logical operators with description.
Operator |
Description |
and |
Logical AND operator- If both the operands are true (or non-zero), then condition becomes true. |
or |
Logical OR operator- If any of the two operands is true (or non-zero), then condition becomes true. |
not |
Logical NOT operator- The result is reverse of the logical state of its operand. If the operand is true (or non-zero), then condition becomes false. |
The following example illustrates the use of the above discussed operators.
>>> 5>2 and 4<8
True
>>> 5>2 or 4>8
True
>>> not(5>2)
False
Membership operator is an operator which test for membership in a sequence, such as string, list, tuple etc. Table enlists the membership operators.
Operator |
Description |
in |
Evaluate to true, if it find a variable in the specified sequence; otherwise false. |
not in |
Evaluate to true, if it does not find a variable in the specified sequence; otherwise false. |
1The following example illustrates the use of the above discussed operators.
>>> 5 in [0,5,10,15]
True
>>> 6 in [0,5,10,15]
False
>>> 5 not in [0,5,10,15]
False
>>> 6 not in [0,5,10,15]
True
Identity operators compare the memory locations of two objects. Table provides a list of identity operators including a small explanation.
Operator |
Description |
is |
Evaluates to true, if the operands on either side of the operator point to the same object, and false otherwise. |
is not |
Evaluates to false, if the operands on either side of the operator point to the same object, and true otherwise. |
The following example illustrates the use of the above discussed operators.
>>> a=b=3.1
>>> a is b
True
>>> id(a)
30984528
>>> id(b)
30984528
>>> c,ct=3.1,3.1
>>> c is d
False
>>> id(c)
35058472
>>> id(d)
30984592
>>> c is not d
True
>>> a is not b
False
Dinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.
Basic Courses
Advance Courses