Python - Lesson 2 (Arithmetic & Assignment Operators)
Kannan Piedy
IIM | Python, Data science | Technical Lead and Manager with experience leading large teams and building large projects.
I hope you've already been through Python Lesson 1, If you haven't You may find it here.
Let us look at various operators in python :
Arithmetic Operators
a = 5 b = a + 5
Here in the above example we see the arithmetic operator + being used. It adds the value of a and the numeric value 5. What you need to understand is that this is not some magic by which this happens, Operators are just symbols which have been given the instruction to call an inbuilt operation of the class numeric type.
An integer like 5 is an Object of class int (We will go into classes in detail later, for the time being let us assume it means type.) Every class no matter what it might be has certain attributes or features associated with it. Some of them are objects which have a value, others are objects which specify an action. (let us term the former as variables and the latter as functions) To view these attributes that are associated with an Object of any class there is a function that can be invoked called dir and it is used as such.
dir(1)
If we print the outcome of this function :
print "The Outcome of DIR is : ", dir(1)
we see something very interesting
The Outcome of DIR is : ['__abs__', '__add__', '__and__', '__class__',
'__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__',
'__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__',
'__getnewargs__', '__hash__', '__hex__', '__index__', '__init__',
'__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__',
'__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__',
'__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__',
'__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__',
'__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__',
'__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__',
'__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__',
'__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate',
'denominator', 'imag', 'numerator', 'real']
We will go into why some of these strings are mentioned prefixed with "__" characters , but you should notice a few interesting strings in this list.
__add__
__mul__
__sub__
__div__
These represent the functions which are of type integer and can be used instead of their corresponding symbols as well.
a = 3
b = a + 3
x = 3
y = x.__add__(3)
print b
print y
If you attempt the above scenario you will notice similar results for b and y.
You can attempt this with the other operations of +, -, / (for division, do realise that there is a difference between / and \ , the former is for the division operation) and *(used for multiplication)
There are other operators and symbols to use too , for example to find the square of a number or the square root of a number.
a = 4**2
print a
Here a would be printed as 4 squared , which is 16. However , to find the square root :
b = a**0.5
b here would be identified as the square root of a. We may use this technique to find the nth power or the nth root of any number.
The same operation of finding the power of a base number can be done using one of the inbuilt functions of the class as well. for example :
c = a.__pow__(3)
d = a**3
both the above lines would return the value of the cube of a.
We may find the reminder of any division operation using the modulo operator % , for example :
a = 10
b = 3
c = a % b
print c
The above lines of code will print the value of c which outputs the reminder when a is divided by b.
You may ask me, there seems to be an operator for everything , I'd say have you noticed we haven't spoken about the " =" sign. Yes , it is an operator too. It is called the assignment operator. There are many assignment operators and lets look at them.
# to assign an object to a variable
a = 5
# to perform an addition and assign to result to a variable
# since a is currently five we want to add another 10 to it
# and save it in again in the variable " a "
a += 10
print a
# at this point the value of a is printed as 15
# to divide a by a number 3 for example and save it in the same variable
# we can use do it as such.
a /= 3
print a
# at this point the value of a is 15/3 which is 5.
We may perform any arithmetic operation combined with an assignment operator to save the result in an assigned variable.
Note : When using an assignment operator other than " = ", the object where it is being assigned should have a pre-existing value and should support the arithmetic operation.
To be contd. (will see you shortly with Part 3.)
Strategy @ Uber | UC Santa Barbara | UBS | VIT
6 年I'll get to learn a lot. Keep up the work!