foreign
if you use numbers in your python code
then you'll need to learn the rules of
arithmetic how to add subtract multiply
and divide arithmetic may seem trivial
but there are some tricky details when
combining numbers of different types or
when dividing
in this video we'll cover arithmetic and
python version 2. if you are using
version 3 then you'll want to watch our
video for that version let's go
in version 2 there are four types of
numbers ins Longs floats and complex
numbers and in arithmetic there are four
operations addition subtraction
multiplication and division to use
arithmetic in Python you first need to
understand the idea of narrower and
wider types
any int can be converted to a long by
adding an L at the end
another way to convert an end to a long
is to pass it to the long Constructor
but some Longs are too big to be
converted into ins
for example on my system 10 billion is
too big to be an INT python treats it as
a long see how it puts an L at the end
so we say that ins are a narrower type
than lungs you can also say that longs
are a wider type than ins
similarly any long value can also be
represented by a float but not the other
way around to convert along to a float
just add a point zero at the end
another way to do this is to pass a
whole number to the float Constructor
but not all floats can be converted to
Longs for example the number 3.14 is a
decimal it's not a whole number
if you try to change it to a long the
number is first rounded and then made
into a long it's no longer the same
number
so Longs are narrower than floats
floats are wider than lungs
lastly any float can be made into a
complex number but not vice versa to
convert a float to a complex number just
add 0 J to the number or pass the number
to the complex Constructor
if you try to convert a complex number
to a float you get a type error
so floats are narrower than complex
numbers we can also say that complex
numbers are wider than floats
let's recap ins are narrower than longs
Longs are narrower than floats
and floats are narrower than complex
numbers
going in reverse complex numbers are
wider than floats
floats are wider than longs
and Longs are wider than ins
this is similar to how in mathematics
integers are a subset of the real
numbers and the reals are a subset of
the complex numbers in Python ins and
Longs both represent integers
let's now explore the four arithmetic
operations in Python we'll start by
creating four numbers one for each type
a is an INT
B is a long
C is a float
and D is a complex number
the rule of thumb is when combining two
numbers of different types python will
convert the narrower type to The Wider
type then perform the operation
addition
if we add a and b we are adding an INT
and a long Longs are wider than ins so
python converts a to a long then adds
the result is five as a long and not 5
is an INT
subtraction
if we compute C minus B we are
subtracting along from a float Longs are
narrower than floats so python widens B
to a float then subtracts the result is
3.0 which is a float
multiplication
if we multiply a and C we are
multiplying an INT and a float
floats are wider than ins so a is
changed to a float and then the numbers
are multiplied the result is 12.0 afloat
division
next divide d by C
floats are narrower than complex numbers
so C is widened to a complex number
before the division
we get 2 plus 0 J
while this is the same as the integer 2
python stores it as a complex number you
can tell because it has an imaginary
part
don't forget that python uses J as the
square root of negative one not I
in addition to knowing what happens when
combining numbers of different types you
also need to fully understand a vision
if you are dividing two whole numbers in
Python get ready the answer may not be
what you would expect look what happens
if you divide 16 by 5.
the mathematical answer is 3.2 but the
Python answer is 3. this is because when
dividing with two whole numbers python
Returns the quotient not the actual
value
recall that when you divide one number
by another using long division you get
two numbers a quotient and a remainder
dividing whole numbers Returns the
quotient if you want the remainder you
use the percent operator
if you do want the real number value and
not just the quotient you can convert
either the numerator or denominator to a
float
don't forget the big exception in
arithmetic you cannot divide by zero in
math it is undefined and in Python it
throws a zero division error be careful
when dividing two numbers unless you are
certain the denominator is not zero
you'll have to be prepared for this
possibility
when combining two numbers using
addition subtraction multiplication or
division python will widen any numbers
to make sure all values are of the same
type
and when dividing two integers python
Returns the quotient
and don't forget what happens when you
divide by zero
foreign