Python Operators
## Python2.x Python Operators
* * *
## What are Operators?
This chapter mainly explains Python operators. Take a simple example **4 + 5 = 9**. In this example, **4** and **5** are called **operands**, and + is called an operator.
Python supports the following types of operators:
Next, let's learn Python operators one by one.
* * *
The following assumes variables: **a=10, b=20**:
| Operator | Description | Example |
| --- | --- | --- |
| + | Addition - Adds two objects | a + b outputs result 30 |
| - | Subtraction - Gives the negative of a number or subtracts one number from another | a - b outputs result -10 |
| * | Multiplication - Multiplies two numbers or returns a string repeated a certain number of times | a * b outputs result 200 |
| / | Division - x divided by y | b / a outputs result 2 |
| % | Modulus - Returns the remainder of division | b % a outputs result 0 |
| ** | Exponentiation - Returns x to the power of y | a**b is 10 to the power of 20, outputs result 100000000000000000000 |
| // | Floor Division - Returns the integer part of the quotient (**floor division**) | >>> 9//2 4 >>> -9//2 -5 |
The following examples demonstrate the operations of all Python arithmetic operators:
## Example (Python 2.0+)
a = 21
b = 10
c = 0
c = a + b
print "1 - Value of c is:", c
c = a - b
print "2 - Value of c is:", c
c = a * b
print "3 - Value of c is:", c
c = a / b
print "4 - Value of c is:", c
c = a % b
print "5 - Value of c is:", c
a = 2
b = 3
c = a**b
print "6 - Value of c is:", c
a = 10
b = 5
c = a//b
print "7 - Value of c is:", c
[Run Example Β»](#)
The output of the above example is:
1 - Value of c is: 31
2 - Value of c is: 11
3 - Value of c is: 210
4 - Value of c is: 2
5 - Value of c is: 1
6 - Value of c is: 8
7 - Value of c is: 2
> **Note:** In Python2.x, dividing an integer by an integer only yields an integer. To get a decimal part, change one of the numbers to a float.
>
> >>> 1/2 0
> >>> 1.0/2 0.5
> >>> 1/float(2) 0.5
* * *
## Python Comparison Operators
The following assumes variable a is 10, variable b is 20:
| Operator | Description | Example |
| --- | --- | --- |
| == | Equal - Compares if two objects are equal | (a == b) returns False. |
| != | Not Equal - Compares if two objects are not equal | (a != b) returns True. |
| | Not Equal - Compares if two objects are not equal. Deprecated in Python 3. | (a b) returns True. This operator is similar to !=. |
| > | Greater Than - Returns if x is greater than y | (a > b) returns False. |
| < | Less Than - Returns if x is less than y. All comparison operators return 1 for true, 0 for false. This is equivalent to the special variables True and False respectively. | (a = | Greater Than or Equal To - Returns if x is greater than or equal to y. | (a >= b) returns False. |
| <= | Less Than or Equal To - Returns if x is less than or equal to y. | (a <= b) returns True. |
The following examples demonstrate the operations of all Python comparison operators:
## Example (Python 2.0+)
a = 21
b = 10
c = 0
if a == b:
print "1 - a is equal to b"
else:
print "1 - a is not equal to b"
if a != b:
print "2 - a is not equal to b"
else:
print "2 - a is equal to b"
if a b:
print "3 - a is not equal to b"
else:
print "3 - a is equal to b"
if a b:
print "5 - a is greater than b"
else:
print "5 - a is less than or equal to b"
a = 5
b = 20
if a = a:
print "7 - b is greater than or equal to a"
else:
print "7 - b is less than a"
The output of the above example is:
1 - a is not equal to b
2 - a is not equal to b
3 - a is not equal to b
4 - a is greater than or equal to b
5 - a is greater than b
6 - a is less than or equal to b
7 - b is greater than or equal to a
* * *
## Python Assignment Operators
The following assumes variable a is 10, variable b is 20:
| Operator | Description | Example |
| --- | --- | --- |
| = | Simple assignment operator | c = a + b assigns the result of a + b to c |
| += | Add and assign | c += a is equivalent to c = c + a |
| -= | Subtract and assign | c -= a is equivalent to c = c - a |
| *= | Multiply and assign | c *= a is equivalent to c = c * a |
| /= | Divide and assign | c /= a is equivalent to c = c / a |
| %= | Modulus and assign | c %= a is equivalent to c = c % a |
| **= | Exponent and assign | c **= a is equivalent to c = c ** a |
| //= | Floor divide and assign | c //= a is equivalent to c = c // a |
The following examples demonstrate the operations of all Python assignment operators:
## Example (Python 2.0+)
a = 21
b = 10
c = 0
c = a + b
print "1 - Value of c is:", c
c += a
print "2 - Value of c is:", c
c *= a
print "3 - Value of c is:", c
c /= a
print "4 - Value of c is:", c
c = 2
c %= a
print "5 - Value of c is:", c
c **= a
print "6 - Value of c is:", c
c //= a
print "7 - Value of c is:", c
The output of the above example is:
1 - Value of c is: 31
2 - Value of c is: 52
3 - Value of c is: 1092
4 - Value of c is: 52
5 - Value of c is: 2
6 - Value of c is: 2097152
7 - Value of c is: 99864
* * *
## Python Bitwise Operators
Bitwise operators treat numbers as binary and perform calculations. The bitwise operations in Python are as follows:
The following table assumes variable a is 60, b is 13, in binary format:
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
| Operator | Description | Example |
| --- | --- | --- |
| & | Bitwise AND operator: If both corresponding bits are 1, the result is 1, otherwise 0. | (a & b) outputs result 12, binary explanation: 0000 1100 |
| | | Bitwise OR operator: If either of the corresponding bits is 1, the result is 1. | (a | b) outputs result 61, binary explanation: 0011 1101 |
| ^ | Bitwise XOR operator: If the corresponding bits are different, the result is 1. | (a ^ b) outputs result 49, binary explanation: 0011 0001 |
| ~ | Bitwise NOT operator: Inverts each bit of the binary representation, 0 becomes 1, 1 becomes 0. ~x is similar to -x-1. | (~a) outputs result -61, binary explanation: 1100 0011 (in two's complement form), for a signed binary number in two's complement form. |
| << | Left shift operator: Shifts all bits of the operand left by the number of positions specified by the number to the right of <<, discarding high bits and filling low bits with 0. | a <> | Right shift operator: Shifts all bits of the operand right by the number of positions specified by the number to the right of >>. | a >> 2 outputs result 15, binary explanation: 0000 1111 |
The following examples demonstrate the operations of all Python bitwise operators:
## Example (Python 2.0+)
a = 60
b = 13
c = 0
c = a & b;
print "1 - Value of c is:", c
c = a | b;
print "2 - Value of c is:", c
c = a ^ b;
print "3 - Value of c is:", c
c = ~a;
print "4 - Value of c is:", c
c = a <> 2;
print "6 - Value of c is:", c
The output of the above example is:
1 - Value of c is: 12
2 - Value of c is: 61
3 - Value of c is: 49
4 - Value of c is: -61
5 - Value of c is: 240
6 - Value of c is: 15
* * *
## Python Logical Operators
Python supports logical operators. The following assumes variable a is 10, b is 20:
| Operator | Logical Expression | Description | Example |
| --- | --- | --- | --- |
| and | x and y | Boolean "AND" - If x is False, x and y returns False, otherwise it returns the evaluated value of y. | (a and b) returns 20. |
| or | x or y | Boolean "OR" - If x is non-zero, it returns the evaluated value of x, otherwise it returns the evaluated value of y. | (a or b) returns 10. |
| not | not x | Boolean "NOT" - If x is True, returns False. If x is False, it returns True. | not(a and b) returns False |
The output of the above example is:
## Example (Python 2.0+)
a = 10
b = 20
if a and b:
print "1 - Both variables a and b are True"
else:
print "1 - Either variable a or b is not True"
if a or b:
print "2 - Both variables a and b are True, or one of them is True"
else:
print "2 - Neither variable a nor b is True"
a = 0
if a and b:
print "3 - Both variables a and b are True"
else:
print "3 - Either variable a or b is not True"
if a or b:
print "4 - Both variables a and b are True, or one of them is True"
else:
print "4 - Neither variable a nor b is True"
if not(a and b):
print "5 - Both variables a and b are False, or one of them is False"
else:
print "5 - Both variables a and b are True"
The output of the above example is:
1 - Both variables a and b are True
2 - Both variables a and b are True, or one of them is True
3 - Either variable a or b is not True
4 - Both variables a and b are True, or one of them is True
5 - Both variables a and b are False, or one of them is False
* * *
## Python Membership Operators
In addition to the operators mentioned above, Python also supports membership operators, which test whether a value is a member of a sequence, including strings, lists, or tuples.
| Operator | Description | Example |
| --- | --- | --- |
| in | Returns True if the value is found in the specified sequence, otherwise returns False. | x in y, returns True if x is in the sequence y. |
| not in | Returns True if the value is not found in the specified sequence, otherwise returns False. | x not in y, returns True if x is not in the sequence y. |
The following examples demonstrate the operations of all Python membership operators:
## Example (Python 2.0+)
a = 10
b = 20
list = [1, 2, 3, 4, 5];
if(a in list):
print "1 - Variable a is in the given list list"
else:
print "1 - Variable a is not in the given list list"
if(b not in list):
print "2 - Variable b is not in the given list list"
else:
print "2 - Variable b is in the given list list"
a = 2
if(a in list):
print "3 - Variable a is in the given list list"
else:
print "3 - Variable a is not in the given list list"
The output of the above example is:
1 - Variable a is not in the given list list
2 - Variable b is not in the given list list
3 - Variable a is in the given list list
* * *
## Python Identity Operators
Identity operators are used to compare the memory units of two objects.
| Operator | Description | Example |
| --- | --- | --- |
| is | is checks if two identifiers refer to the same object | **x is y**, similar to **id(x) == id(y)**, returns True if they refer to the same object, otherwise returns False |
| is not | is not checks if two identifiers refer to different objects | **x is not y**, similar to **id(a) != id(b)**. Returns True if they do not refer to the same object, otherwise returns False. |
**Note:** The [id()](#) function is used to get the memory address of an object.
The following examples demonstrate the operations of all Python identity operators:
## Example (Python 2.0+)
a = 20
b = 20
if(a is b):
print "1 - a and b have the same identity"
else:
print "1 - a and b do not have the same identity"
if(a is not b):
print "2 - a and b do not have the same identity"
else:
print "2 - a and b have the same identity"
b = 30
if(a is b):
print "3 - a and b have the same identity"
else:
print "3 - a and b do not have the same identity"
if(a is not b):
print "4 - a and b do not have the same identity"
else:
print "4 - a and b have the same identity"
The output of the above example is:
1 - a and b have the same identity
2 - a and b have the same identity
3 - a and b do not have the same identity
4 - a and b do not have the same identity
> Difference between is and ==:
>
> is is used to determine if two variables refer to the same object (the same memory space), == is used to determine if the values of the referenced variables are equal.
>
> >>> a = [1, 2, 3]
> >>> b = a
> >>> b is a
> True
> >>> b == a
> True
> >>> b = a[:]
> >>> b is a
> False
> >>> b == a
> True
* * *
## Python Operator Precedence
The following table lists all operators from highest to lowest precedence:
| Operator | Description |
| --- | --- |
| ** | Exponentiation (highest precedence) |
| ~ + - | Bitwise NOT, unary plus and minus (the last two have method names +@ and -@) |
| * / % // | Multiplication, division, modulus, and floor division |
| + - | Addition and subtraction |
| >> << | Right shift and left shift operators |
| & | Bitwise 'AND' |
| ^ | | Bitwise operators |
| <= >= | Comparison operators |
| == != | Equality operators |
| = %= /= //= -= += *= **= | Assignment operators |
| is is not | Identity operators |
| in not in | Membership operators |
| not and or | Logical operators |
The following examples demonstrate the operations of all Python operator precedence:
## Example (Python 2.0+)
a = 20
b = 10
c = 15
d = 5
e = 0
e = (a + b) * c / d
print "(a + b) * c / d evaluation result is:", e
e = ((a + b) * c) / d
print "((a + b) * c) / d evaluation result is:", e
e = (a + b) * (c / d);
print "(a + b) * (c / d) evaluation result is:", e
e = a + (b * c) / d;
print "a + (b * c) / d evaluation result is:", e
The output of the above example is:
(a + b) * c / d evaluation result is: 90
((a + b) * c) / d evaluation result is: 90
(a + b) * (c / d) evaluation result is: 90
a + (b * c) / d evaluation result is: 50
YouTip