00:05 Integers—what is an integer? Well, an integer is a whole number, the kind you started out counting out when you first learned about numbers at school. In Python 3, an integer is any length up to the memory limit of the computer, so you probably won’t run out of integers that you can use.
00:25 The default is in decimal—base 10—but as you’ll see, you’ll be able to define them and view them in different bases. You’re going to see how to create integers and different ways you can work with them in the following videos.
00:42 Throughout these workshops, our coding will be done using bpython, which is a version of the Python REPL that uses docstrings and color coding to make understanding code more simple. With that housekeeping out of the way, let’s look at ints.
We can see that
a has a value of
b has a value of
10. It’s possible to do simple arithmetic, such as
a + b, seen here, or
a - b. And the results are as we would expect. As mentioned previously, the default for these numbers is decimal, or base 10, but it’s possible to use other bases.
We can have them in binary, hexadecimal, or octal. If we want to define one in binary, here we can see we’re defining
c, and we start off with
0b and then put in the binary digits that make the number up.
So what’s needed is to convert
my_num into an integer to allow maths to be performed on it. That’s done using the keyword
int(), and inside we’ll put the original variable
my_num, and you can see that now equates to the number
my_num is the string with the quotes enclosing it.
As you’ll see with all of the variables in this course, it’s possible to inspect the type of them using
type(). So here,
type() and then we put any of our variables in, and we can see that they are the
<class 'int'>, which is for integer variables.
Now you’re going to see that in action using the
my_num_2 variable, in converting it to different bases. First, it’s possible to create a binary representation of it using the
bin() keyword, and now you have the binary representation of
oct() gives us the octal representation of
Become a Member to join the conversation.