Python Operators
What are operators in python?
Operators are special symbols in Python that carry out arithmetic or logical
computation. The value that the operator operates on is called the operand.
>>> 2+3
5
Arithmetic operators
Arithmetic operators are used to perform mathematical operations like
addition, subtraction, multiplication, etc.
Operato
Meaning Example
r
+ Add two operands or unary plus x + y+ 2
Subtract right operand from the left or
- x - y- 2
unary minus
* Multiply two operands x*y
Divide left operand by the right one
/ x/y
(always results into float)
x%y
Modulus - remainder of the division of left
% (remainder of
operand by the right
x/y)
Floor division - division that results into
// whole number adjusted to the left in the x // y
number line
Exponent - left operand raised to the x**y (x to the
**
power of right power y)
Example
x = 15
y = 4
# Output: x + y = 19
print('x + y =',x+y)
# Output: x - y = 11
print('x - y =',x-y)
# Output: x * y = 60
print('x * y =',x*y)
# Output: x / y = 3.75
print('x / y =',x/y)
# Output: x // y = 3
print('x // y =',x//y)
# Output: x ** y = 50625
print('x ** y =',x**y)
Output
x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625
Comparison operators
Comparison operators are used to compare values. It returns
either True or False according to the condition.
Operator Meaning Example
> Greater than - True if left operand is greater than the right x>y
< Less than - True if left operand is less than the right x<y
== Equal to - True if both operands are equal x == y
!= Not equal to - True if operands are not equal x != y
Greater than or equal to - True if left operand is greater than or
>= x >= y
equal to the right
Less than or equal to - True if left operand is less than or equal
<= x <= y
to the right
Example
x = 10
y = 12
# Output: x > y is False
print('x > y is',x>y)
# Output: x < y is True
print('x < y is',x<y)
# Output: x == y is False
print('x == y is',x==y)
# Output: x != y is True
print('x != y is',x!=y)
# Output: x >= y is False
print('x >= y is',x>=y)
# Output: x <= y is True
print('x <= y is',x<=y)
Output
x > y is False
x < y is True
x == y is False
x != y is True
x >= y is False
x <= y is True
Logical operators
Logical operators are the and, or, not 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
and : Both of them should be true , then am true
a>10 b<12 a>10 and b<12
True True True
True False False
False - False
or : Either one should be true, then am true
a>10 b<12 a>10 or b<12
True - True
False True True
False False False
not : opposite
a>10 - true
not(a>10) - false
Example
x = True
y = False
print('x and y is',x and y)
print('x or y is',x or y)
print('not x is',not x)
Output
x and y is False
x or y is True
not x is False
Bitwise operators
Bitwise operators act on operands as if they were strings of binary digits.
They operate bit by bit, hence the name.
For example, 2 is 10 in binary and 7 is 111.
In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in
binary)
Operator Meaning Example
& Bitwise AND x & y = 0 (0000 0000)
| Bitwise OR x | y = 14 (0000 1110)
~ Bitwise NOT ~x = -11 (1111 0101)
^ Bitwise XOR x ^ y = 14 (0000 1110)
>> Bitwise right shift x >> 2 = 2 (0000 0010)
<< Bitwise left shift x << 2 = 40 (0010 1000)
Assignment operators
Assignment operators are used in Python to assign values to variables.
a = 5 is a simple assignment operator that assigns the value 5 on the right
to the variable a on the left.
Operator Example Equivalent to
= x=5 x=5
+= x += 5 x=x+5
-= x -= 5 x=x-5
*= x *= 5 x=x*5
/= x /= 5 x=x/5
%= x %= 5 x=x%5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
&= x &= 5 x=x&5
|= x |= 5 x=x|5
^= x ^= 5 x=x^5
>>= x >>= 5 x = x >> 5
<<= x <<= 5 x = x << 5
Special operators
Python language offers some special types of operators like the identity
operator or the membership operator.
Identity operators
is and is not are the identity operators in Python. They are used to check
if two values (or variables) are located on the same part of the memory.
Two variables that are equal does not imply that they are identical.
Operato
Meaning Example
r
True if the operands are identical (refer to the
is x is True
same object)
True if the operands are not identical (do not x is not
is not
refer to the same object) True
Example
x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
# Output: False
print(x1 is not y1)
# Output: True
print(x2 is y2)
# Output: False
print(x3 is y3)
Output
False
True
False
Membership operators
in and not in are the membership operators in Python. They are used to
test whether a value or variable is found in a sequence
(string, list, tuple, set and dictionary).
Operator Meaning Example
in True if value/variable is found in the sequence 5 in x
True if value/variable is not found in the
not in 5 not in x
sequence
Example
x = 'Hello world'
y = {1:'a',2:'b'}
# Output: True
print('H' in x)
# Output: True
print('hello' not in x)
# Output: True
print(1 in y)
# Output: False
print('a' in y)
Output
True
True
True
False
Python Namespace and Scope
Name (also called identifier) is simply a name given to objects. Everything
in Python is an object. Name is a way to access the underlying object.
For example, when we do the assignment a = 2, 2 is an object stored in
memory and a is the name we associate it with. We can get the address (in
RAM) of some object through the built-in function id().
# Note: You may get different values for the id
a = 2
print('id(2) =', id(2))
print('id(a) =', id(a))
Output
id(2) = 9302208
id(a) = 9302208
# Note: You may get different values for the id
a = 2
print('id(a) =', id(a))
a = a+1
print('id(a) =', id(a))
print('id(3) =', id(3))
b = 2
print('id(b) =', id(b))
print('id(2) =', id(2))
Output
id(a) = 9302208
id(a) = 9302240
id(3) = 9302240
id(b) = 9302208
id(2) = 9302208
All these are valid and a will refer to three different types of objects in
different instances. Functions are objects too, so a name can refer to them
as well.
def printHello():
print("Hello")
a = printHello
a()
Output
Hello
What is a Namespace in Python?
Namespace is a collection of names.
In Python, you can imagine a namespace as a mapping of every name you
have defined to corresponding objects.
Different namespaces can co-exist at a given time but are completely
isolated.
A namespace containing all the built-in names is created when we start the
Python interpreter and exists as long as the interpreter runs.
This is the reason that built-in functions like id(), print() etc. are always
available to us from any part of the program. Each module creates its own
global namespace.
These different namespaces are isolated. Hence, the same name that may
exist in different modules does not collide.
Modules can have various functions and classes. A local namespace is
created when a function is called, which has all the names defined in it.
Similar is the case with class.