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

a = 10 + 20
print(a)
30

If we want to add something to variable ‘a’, one way of doint it is using +=.

a +=10
print(a)
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

a = 20 - 10
print(a)
10
a -= 5
print(a)
5

* is used for for multiplication

a = 2*10
print(a)
20
a *= 2
print(a)
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

a = 20/6
print(a)
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

a = "Materialism leads to "
b = "injustice."
print(a + b)
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
x = 5.2
print(not x<=10)
False
capitalism = False
communism = False
justice = True
print(capitalism and communism)
False
False
print(capitalism and justice)
False
print(capitalism or justice)
True
print(capitalism is not justice)
True

Default values#

food = 'bread'
lunch = food or 'curry'
print(lunch)
bread
food = None
lunch = food or 'curry'
print(lunch)
curry

If the first argument before or is True, the value after or is discarded.

food = None
lunch = 'currey' or food
print(lunch)
currey
food = 'bread'
lunch = 'currey' or food
print(lunch)
currey

Identity#

is operator compares whether both variables on its right and left side refer to same memory location or not.

a = 257
b = 257
print(a == b)
True
id(a), id(b)
(140715274639216, 140715274639216)

Because ali and hasan are stored at different location at different location, thus and answer

print(a is b)
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.

a = 256
b = 254 + 2
print(a is b)
True
print(id(a), id(b))
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)

Gallery generated by Sphinx-Gallery