Arithmetic Operators in Python:
Operator Name |
Operator |
Description |
Example |
Addition |
+ |
|
A = 10 + 2
The addition operator adds 10 and 2 and returns 12
|
Subtraction |
- |
i.e., A-B ≠ B-A
i.e., A-(B-C) ≠ (A-B)-C
|
B = 2 – 4
The subtraction operator subtracts 4 from 2 and returns -2.
|
Multiplication |
* |
The operator symbol is an asterisk.
i.e., A* B = B* A
i.e., A*B*C = C*B*A
|
C=4*5
The multiplication operator multiplies the number four by five times resulting in the value 20. |
Division- Floating point |
/ |
|
D=25/5
The denominator divides the numerator 25 by 5 times resulting in a value of 5.0. |
Division - Integer |
// |
|
E = 45.0//9
The denominator divides the numerator 45.0 by 9 times resulting in a value of 5. |
Modulo Division |
% |
|
Rem = 10%3 The denominator divides the numerator by 3 times resulting in a quotient of 3 and a remainder of 1. |
Relational Operators in Python:
(Also called as Comparision Operators)
Operator Name |
Operator |
Description |
Example |
Less Than |
< |
Compares whether the left hand side operand value is less than the right hand side operand value. If so, the less than operator returns true. Returns false otherwise. |
Count = 10 CurIndex =8 If (CurIndex < Count): print(CurIndex)
In the above example the relational expression inside the if statement will evaluate to True.
|
Less Than or Equal to |
<= |
Compares whether the left hand side operand value is less than or equal to the right hand side operand value. If so, the less than or equal to operator returns true. Returns False otherwise. |
size =21 currentValue= 21 if(currentValue<=size): process(currentValue)
In the above example process will be called because the current value is 21 which is equal to the size. |
Greater Than |
> |
Compares whether the left hand side operand value is greater than the right hand side operand value. If so, the greater than operator returns true. Returns false otherwise. |
Length = 20 Position = 21
while (length > Position): print(position) In, the above snippet the expression inside the while loop will evaluate to False and the loop will terminate. |
Greater Than or Equal to |
>= |
Compares whether the left hand side operand value is greater than or equal to the right hand side operand value. If so, the greater than or equal to operator returns true. Returns False otherwise. |
if(x >=y): generate()
Here the generate function will be called if y is greater than or equal to the value of x. |
Equality or Equal operator
|
== |
Compares whether the left hand side operand value is equal to the right hand side operand value. |
if(reminder==1) : isEven = False In this example if the reminder is one than expression inside the if statement will return true making isEven as False. |
Not Equal to operator |
!= |
Checks whether the left hand side operand value is not equal to the right hand side operand value. Returns true if the operands are not equal. Returns False if the operands are equal. |
isValidToken = (Token != None) In the above example is ValidToken becomes True only if the identifier Token is not equal to None. |
is operator | is |
|
m1 = Moon("Earth", "Moon") if m3 is m1: m1 = m2 |
Logical Operators in Python:
Operator Name |
Operator |
Description |
Example |
And |
And |
The And operator is the implementation of Logical AND operation. The And operator returns True only if both the operands evaluate to True. Otherwise it returns False. |
if (validUserName == True) and (nameAvailable==True): GetPassword()
In the above example Python will invoke GetPassword() only if the user name is valid and it is available. |
Or |
Or |
The Or operator is the implementation of logical OR operation. The Or operator returns True if any or both of the operands evaluate to True. It returns False only when both the operators return False. |
if HoldsSSN or HoldsStateID: permit()
Here the permit() is called if either SSN or State ID is held. permit() is not called only when both SSN and StateID is not available. |
Not |
Not |
Not operator returns True when negation of a truth-value evaluates to True. Returns False otherwise. |
While char is Not None: emit(char)
In the above example, A character is emitted only when it is not None. |
Bitwise Operators in Python:
Operator Name |
Operator |
Description |
Example |
Left shift operator |
<< |
|
8<<2 results in 32 |
Right shift operator |
>> |
left hand side operand to the right by number of bits specified in the right hand side operand.
|
32>>4 results in 2 |
Bitwise AND operator |
& |
Each operand is considered as a binary bit pattern and a bitwise AND is performed.
Each bit of operand one is done an AND operation with the corresponding bit of operand two.
|
The bit patterns for number six and number one are Given below: 110 001
Bitwise AND on these binary bit patterns will result in 0.
|
Bitwise OR operator |
| |
Each operand is considered as a binary bit pattern and a bitwise OR is performed.
Each bit of operand one is done an OR operation with the corresponding bit of operand two.
|
The bit patterns for number eight and number three are given below: 1000 0011
Bitwise OR on these binary bit patterns will result in 1011, which is equal to number eleven in decimal system. |
Binary XOR |
^ |
Each operand is considered as a binary bit pattern and a bitwise XOR is performed.
Each bit of operand one is done an XOR operation with the corresponding bit of operand two.
|
The bit patterns for the numbers seven and four are given here: 111 100
Bitwise XOR on these binary bit patterns will result in 011. Recall that XOR results in 1 if only one bit is set and other bit is not set. If both bit or set or not set XOR results in 0. |
Ones Complement |
~ |
Returns the ones complement of the number. Remember that unlike other bitwise operators this is a Unary operator. |
In a bit pattern, ones complement is equivalent to flipping all ones by zero and all zeros by one.
e.g., 2 corresponds to a bit pattern of 00000010 and taking ones complement results in 11111101. |