Today we shall be discussing floats and integers. The good news is that most of the stuff we shall be covering today should be intuitive. Okay, let’s get moving...
Integers are whole numbers like -6, 10, 1066 etc. In Python, integers work much in the same way they do in mathematics.
There are two main ways to create integers within Python:
Every data-type in Python has a function that can be used to convert objects into that type, for example, lists have the list() function, strings have the str() function and so on. Unsurprisingly then int() is used to convert non-integers into integers. For example, if I have the string “1066” I can use int() to convert it.
In [1]:
string_number = "1066"
integer = int(string_number)
print(int(integer))
print(type(integer)) # <-- the type function returns the type of the object passed in eg 1 is an integer, "hi" is a string, etc.
The int() function also takes an optional argument base as well. So for example “1111”, base=2 will treat “1111” as a binary number and will return whatever that number is in base 10.
In [1]:
int("1111", base = 2)
Out[1]:
If we give the int function a float (more on them later) it will return the integer part of the number. Take care to note it always rounds down.
In [3]:
int(6.99999999999999)
Out[3]:
In [4]:
a = 10
b = 5
print(a + b) # addition
print(a - b) # subtraction
print(a * b) # multiplication
print(a ** b) # exponentation
print(a % b) # modular arithmetic
print(a / b) # divsion (note, returns a float! Also, Python2 and Python3 act differently here, so beware!)
print(a // b) # integer division
# For negative numbers, just add a "-" symbol before the number, for example:
print(-a, a)
Modular Arithmetic is something you can learn more about here. I’m going to skip most of the math and just show you a few common uses:
A % B gives the remainder of A / B. Thus if A % B is 0 then that means the B must be a divisor of A. Ergo if x % 2 equals zero then that means x is an even number.
As for cycles, an obvious use for this is stuff like measuring time, a clocks hand resets after hitting 60:
58 % 60 = 58
59 % 60 = 59
60 % 60 = 0 ←- count ‘resets’
61 % 60 = 1
62 % 60 = 2
Once we hit 61 secs then we would prefer to set the secs timer back to zero and update minutes. thus 61 secs becomes 1min 1sec
And finally, you can get last digits of a number by using x % (power of 10). A load of examples are printed in the console below.
In [36]:
# Grabbing the last digit:
print(123423420 % 10)
print(1234234231 % 10)
print(12342342302 % 10)
print(123423423023 % 10)
print(1234234230244 % 10)
print(12342342302445 % 10)
print("\n")
# Grabbing the last 3 digits:
print(12342342302445 % 1000)
print(1234234230244 % 1000)
print(123423423023 % 1000) # <-- note 0 is dropped, integers cannot start with a 0. Thus 023 is simply 23
print("\n")
# grabbing the last 6 digits
print(12342342302 % 1000000)
print(123423423 % 1000000)
print(1234567 % 1000000)
print(12345 % 1000000)
Integer division returns the whole number part of A / B. for example, 10 // 6 is 1 because 6 fits into 10 exactly once (12 // 6 is of course 2).
So modulo in conjunction with integer division can get you both the whole part of A/B and the remainder of A/B
10 // 6 = 1
10 % 6 = 4
10 / 6 = 1.6666666666666667
Combine these two operations and you can make a nice clock:
In [5]:
def time(time):
"""time measured in total elapsed minutes, function converts to days, hours, mins"""
t = time // 60
days = t // 24 # 24 hours in a day
hours = t % 24 # after working out number of days, get leftover hours
mins = time % 60
return "Input in mins = '{}' which is: {} days {} hours and {} mins".format(time, days, hours, str(mins).zfill(2))
print(time(1))
print(time(60))
print(time(61))
print(time(960))
print(time(1440))
print(time(1447))
print(time(33456456))
What are floating-point numbers?
Well, they are a bit like decimal numbers in Math (Warning! this is an over-simplification, in numbers part 2 I will go into more depth).
Just like Integers there are two main ways to create them:
Floats have more or less the same methods as integers do (e.g +-/*%//). Similarly the float() function works remarkably similar to the int function.
In [20]:
string_number = "10.66"
print(float(string_number))
print(float(-3.2e20)) # < -- Python supports scientific notation...
print(float('inf')) # < -- Python also has a way of representing infinity
print(float(0), float(10), float(5), sep="\n") # notice that when 'float' gets an integer as input '.0' is added to the number
In [89]:
30 / 0
In [8]:
30 % 0
In [9]:
30 // 0
In [21]:
## The only difference a-through-f is where we put a pair of parentheses.
a = 30 - 6 ** 4 * 30 + 20 / 2
b = (30 - 6) ** 4 * 30 + 20 / 2
c = 30 - 6 ** (4 * 30) + 20 / 2
d = 30 - 6 ** 4 * (30 + 20) / 2
e = 30 - 6 ** (4 * 30 + 20 / 2)
f = 30 - (6 ** 4 * 30 + 20 / 2)
print(a, b, c, d, e, f, sep= "\n")
So thats some basic mathmatics for you. Python does have support for fractions, complex numbers and a bunch of other stuff as well. But if you want that functionality you have to go hunting for it.
For example, suppose I want the log(x) or the sqrt(x) what can I do?
In [1]:
from math import log2, sqrt
print(sqrt(100))
## Alternatively, you can also get the square root of a number by raising it to the 0.5 power
print(100**0.5)
print(log2(100))
You might be wondering why I didn't get a name error here. Well, thats because the import statement on the first line tell Python where the definition of log2 and sqrt are; i.e. they come from the math module.
Alright thats it for today. In the next lecture we will go into a bit more detail.
In [ ]: