"/" and "//" operator in python
What are division operators in Python?
In Python programming, you can perform division in two ways. The first one is Float Division["/"] and the second is Integer Division["//"] or Floor Division.
Float Division["/"]: Divides left hand operand by right hand operand.
Division works in Python the way it's mathematically defined.
Floor Division["//"]: The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored , i.e., rounded away from zero [towards negative infinity].
Floor division works in Python the way it's mathematically defined.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
The Equality operator [==] is a comparison operator in Python that compare values of both the operands and checks for value equality. Whereas the ‘is’ operator is the identity operator that checks whether both the operands refer to the same object or not [present in the same memory location].
Illustration:
# Equality operator >>> a=10 >>>b=10 >>>a==b True >>>a=10 >>>id[a] 2813000247664 >>>b=10 2813000247664 # Both a and b is pointing to the same object >>>a is b True >>>c=a # Here variable a is assigned to new variable c, which holds same object and same memory location >>> id[c] 2813000247664 >>>a is c True
Example 1:
Python3
list1
=
[]
list2
=
[]
list3
=
list1
if
[list1
=
=
list2]:
print
[
"True"
]
else
:
print
[
"False"
]
if
[list1
is
list2]:
print
[
"True"
]
else
:
print
[
"False"
]
if
[list1
is
list3]:
print
[
"True"
]
else
:
print
[
"False"
]
list3
=
list3
+
list2
if
[list1
is
list3]:
print
[
"True"
]
else
:
print
[
"False"
]
Output:
True False True False
- The output of the first if the condition is “True” as both list1 and list2 are empty lists.
- Second, if the condition shows “False” because two empty lists are at different memory locations. Hence list1 and list2 refer to different objects. We can check it with id[] function in python which returns the “identity” of an object.
- The output of the third if the condition is “True” as both list1 and list3 are pointing to the same object.
- The output of the fourth if the condition is “False” because the concatenation of two lists always produces a new list.
Example 2
Python3
list1
=
[]
list2
=
[]
print
[
id
[list1]]
print
[
id
[list2]]
Output:
139877155242696 139877155253640
This shows that list1 and list2 refer to different objects.
and is a Logical AND that returns True if both the operands are true whereas ‘&’ is a bitwise operator in Python that acts on bits and performs bit by bit operation.
Note: When an integer value is 0, it is considered as False otherwise True when using logically.
Example:
a
=
14
b
=
4
print
[b
and
a]
print
[b & a]
Output:
14 4
This is because ‘and’ tests whether both expressions are logically True while ‘&’performs bitwise AND operation on the result of both statements.
In print statement 1, compiler checks if the first statement is True. If the first statement is False, it does not check the second statement and returns False immediately. This is known as “lazy evaluation”. If the first statement is True then the second condition is checked and according to rules of AND operation, True is the result only if both the statements are True. In the case of the above example, the compiler checks the 1st statement which is True as the value of b is 4, then the compiler moves towards the second statement which is also True because the value of a is 14. Hence, the output is also 14.
In print statement 2, the compiler is doing bitwise & operation of the results of statements. Here, the statement is getting evaluated as
follows:
The value of 4 in binary is 0000 0100 and the value of 14 in binary is 0000 1110. On performing bitwise and we get –
0000 0100 & = 0000 0100 = 4 0000 1110
Hence, the output is 4.
To elaborate on this, we can take another example.
Example:
a, b
=
9
,
10
print
[a & b]
print
[a
and
b]
Output:
8 10
The first line is performing bitwise AND on a and b and
the second line is evaluating the statement inside print and printing answer.
In line 1, a = 1001, b = 1010, Performing & on a and b, gives us 1000 which is the binary value of decimal value 8.
In line 2, the expression ‘a and b’ first evaluates a; if a is False [or Zero], its value is returned immediately because of “lazy evaluation” explained above, else, b is evaluated. If b is also non-zero then the resulting value is returned. The value of b is returned because it is the last
value where checking ends for the truthfulness of the statement.
Hence the use of boolean and ‘and’ is recommended in a loop.