1.3 operators
Contents
Note
Click here to download the full example code or to run this example in your browser via Binder
1.3 operators#
This lesson describes Basic operators in python.
The traiditional mathematical operations can be performed
on python objects just by using their symbels. This means addition, subtraction,
multiplication and divitsion can be perfomred just by using +
, -
, *
and
/
symbols/operators between two objects respectively.
Basic operations#
+
is used for addition
30
If we want to add something to variable ‘a’, one way of doint it is using +=
.
40
Above we added 10 to a and assigned the new value again to a. In this way, the value of a is updated.
Similarly we can use -
for subtracting one object from another
10
5
*
is used for for multiplication
20
40
The modulo operator %
returns remainder
print(14 % 5)
4
If one of the value is float, result will be float.
print(17 % 5.0)
2.0
The sign of the result will be same as sign of divider.
print(17 % -5.0)
-3.0
/
is used for division
3.3333333333333335
// is used for truncated division
print(20//6)
3
print(20//6.0)
3.0
If the answer of the truncated division is negative, the answer is rounded to the next smallest iteger (greater negative)
print(20//-6.0, -20//6.0)
-4.0 -4.0
print(-20 // -6.0)
3.0
** is used for exponentiation i.e. to raise one object over another.
print(2**3)
8
The application of basic mathematical operators is not limited to floats or integers.
We can also apply +
to strings in python
Materialism leads to injustice.
However we can not do same for -
operator. This means we can
not subtract two strings.
# uncomment following line
# a - b # -> TypeError
print(a * 2)
Materialism leads to Materialism leads to
Above we are multiplying a string with an integer because a is a string and 2 is integer.
If however, we do a +2
, we will again get TypeError
.
What kind of mathematical operations can be applied on an object or between two objects, depends purely upon the objects. To our surprise, we can even modify the behavior of these mathematical operators in python. More about this will come later in 3.16 magic methods.
Comparisons#
If we want to compare one object with another and tell whether both are
equal or not, we make use of ==
operator.
print(2 == 3)
False
The ==
operator returns either True or False depending upon the values being compared.
print(2 ==2)
True
print(2.2 == 1.1 + 1.1)
True
However, we should avoid comparing floats in this way. This is because computers can not represent accurate values of floats. 1.1 + 2.2 results in an approximated answer so we avoid comparing floats.
print(3.3 == 1.1 + 2.2)
False
print(abs((1.1 + 2.2) - 3.3) < 1e-15)
True
If we want to check whether a number lies between two numbers or not
we can make use of <
or >
operators twice.
print(8<10<12)
True
capitalism = 'a system based on individualism'
print(capitalism != 'justice')
True
Logical operators#
not
results in opposite to what comes after it.
print(not True)
False
print(not False)
True
False
capitalism = False
communism = False
justice = True
print(capitalism and communism)
False
print(capitalism or communism)
False
print(capitalism and justice)
False
print(capitalism or justice)
True
print(capitalism is not justice)
True
Default values#
bread
curry
If the first argument before or
is True
, the value after or
is discarded.
currey
currey
Identity#
is
operator compares whether both variables on its right and left side refer
to same memory location or not.
True
(140715274639216, 140715274639216)
Because ali and hasan are stored at different location at different location, thus and answer
True
However, python already stores some commonly used smaller numbers in memory, so when they are created, python refers to that same memory location and does not really create them. Thus for smaller numbers (from -5 to 256 integers) is returns True.
True
140715684850064 140715684850064
feudalism = 'slavery'
capitalism = 'slavery'
print(feudalism is capitalism)
True
feudalism = 'a system of slavery'
capitalism = 'a system of slavery'
print(feudalism is capitalism)
True
Order of operations#
The multiplication *
is performed before addition +
even if +
appears
before *
.
print(20 + 4 * 10)
60
Similarly exponentiation (**
) is performed before multiplication.
print(2 * 3 ** 4 * 5)
810
Complete order of precedence of operators in python can be found from here .
Total running time of the script: ( 0 minutes 0.015 seconds)