 In this video we are going to look into the integer data type in detail. So let's go ahead and create a new file and call it integers. And let's look at a couple of examples. So first of all, how do you create an integer? Well, you can simply use all the digits on your keyboard, simply type them without any other symbol in a cell and Python can understand it right away. This is what we call a numerical literal. So literal is any combination of symbols in a cell or in a code block that Python can literally understand. And if you simply type the digits 4 and 2, Python literally understands it as 42. Okay, that's almost trivial. So let's go one step further. So what else can we do to make working with integers quite nice? So let's say I wanted to model the number one million just like this with six zeros. So in a source code, this may be very hard to read, right? Because we cannot really identify the groups of three. And in practice, oftentimes, we write big numbers in groups of three. So how can you do that in Python? Well, obviously, you cannot use the dot because the dot is obviously the syntax that creates a floating point number, which we will look into in the next video. But how else could we group numbers? Well, in recent versions of Python, you can simply use the underscore. And the underscore is basically ignored, as we can see. So Python doesn't store them. But for us as the reader of the source code, we can now easily read and identify this number to be one million. So oftentimes, you will see that in code. Don't be confused. Basically, Python just ignores it just like a comment. And that makes a code a little bit nicer. And then also, one thing to note, and we will look into that in more detail, let's say if you want to write the number 10. And if you start that with a zero like this here, you already see in JupyterLab that the first zero is red. And if I execute this, I get a syntax error. And here it says leading zeros are not allowed, are not permitted. And we will soon see why this is the case when we look into more details. But before we do so, we can, of course, create also numbers using the constructors. So I'm talking about the int constructor that we have seen a couple of times before. We call it, let's say I give it floating point number 42.0, I get back simply the number 42. And also here you have to be a bit careful. So let's say, for example, you have 42.87, the decimals, the 87 are just cut off and forgotten. So this is not rounding. This is just extracting an integer out of some numeric object by simply cutting away the decimals. And this is formally known as truncating, but it is definitely not rounding. So be careful here. And this is using the int constructor. And now the int constructor, one detail we didn't see so far in this course yet. The int constructor can also take text data. And now you may wonder, why is that the case? Well, first of all, let's check that. So if I go ahead and I write a text object with the number 42 in it, I get back a proper 42 as an integer. And the contrary, if I copy paste it, the 42 in a cell on its own, obviously I get back 42 as a text object, right? So the int object can be used to cast a textual data into numeric data if the text string attains to some formatting rules. For example, you cannot have, you cannot simply put an A in there. This is not, this cannot be interpreted. By simply move, if you simply have digits inside a string, then you can convert that so to say, or cast it as an integer. So why is that kind of useful? Well, remember in one of the exercises when we implemented a guessing game, and there is also an exercise that I did not cover yet in the video where you have to throw a die and you have to guess which side of the die comes up top. You may use the input function. And let's say enter a number, Python adds one to it. If you go ahead and you execute this, and let's go ahead and enter any number in the text box, we see that the input function returns a string, okay? So in other words, if I go ahead and let's say I add one to it, just as I wanted to hear, what's going to happen is I'm going to get a type error because obviously the left hand operand is a string and the string plus a number is not defined in Python, okay? Sometimes plus works for non-numeric types. We saw that when we looked at list concatenation and so on. But usually it doesn't work. So what could you do? Well, you could simply wrap the entire expression with the inconstructor. And now if you go ahead, this works, okay? So this is a little trick that is worthwhile to know. And also this is very helpful to know as sometimes when you load in data from an external data source, for example CSV files, all the columns sometimes come as text strings even though they may only hold numeric data. So I don't want to go into detail here, but sometimes you know for sure when you load in some data from a CSV file or an Excel file that some column or some row must only have numbers and you know that for sure. Then what you could do is you could loop over all the cells in the CSV file and simply take whatever values in there and put it inside the inconstructor and get back a proper integer object, okay? So this is quite helpful to know when you're given non-integer data and you know it's actually integers. Okay, so far so good. And then what I'm not going to go into detail in this video, of course, are the arithmetic operators. We saw them very early in the course. You can do all kinds of arithmetic with integers. We're not covering that here. But what we do cover here is how the numbers are going to be represented in a computer's memory. So let's go ahead and write here binary representations. So binary representations is the idea by which a number is represented in a computer's memory, okay? So maybe let's write here how numbers are stored inside a computer's memory. So we don't want to cover that in too much theory, but I want to cover it a little bit so that you don't run into the most common errors people make or practitioners make when working with numbers, okay? So let's go ahead and look, first of all, at the memory diagram that I have here. So let's say we go ahead and let's say you create the number, let's say three, for example, okay? So what Python would do is it would go ahead and it would create a box. And it would go ahead and put a type to the box here, to the object. And then it has to write some 0s and 1s in here, okay? So previously I was just usually writing dots in here or some random 0s and 1s. But now let's get a bit more formal and let's try to understand what the numbers are going to look like. So in particular, the numbers that are in here, they will look like this, 1, 1, and then many, many 0s leading that, okay? There would be even more 0s. And let's say we store that as the variable A, then the whole memory diagram would look like this. So now the question is, how do I know that this is just the sequence, all 0s and then 1, 1 at the end, okay? So let's flip the page and look at how computers count and how computers store numbers. So the first concept that you need to understand is the concept of a bit, okay? So bits in general are just the ones in 0. So a bit of information is what we refer to as either a true or false statement about the world, so either one or zero. However, in the context of numbers, every bit has a position. So sometimes when we use the word bit, what we really mean is the position inside the system I'm going to draw here, okay? And we are going to go from right to left and the reason why is, because we also do that at least in the Western world when we do high school or elementary school mathematics. So let's put the bits up here and we are going to put there exactly eight bits for now. This is enough for the example. And then right here, we have simply dots. Why? Because a number may consist of more than eight bits, okay? So we start by using, by writing with zero here on the right-hand side, because always in computer science and also in Python, oftentimes we simply start to count at zero, shouldn't confuse you by now. Now, we introduce the idea, maybe let's do that in orange, of a digit. So what the digit really is, is what is the meaning of a piece of information in that position, okay? And the only thing you need to understand is that the meaning is, can be derived from the formula two to the power of, let's call it b, lowercase b for the position of the bit that we are in. So the zero's bit would be two to the power of zero, which of course is one. Then we have one, so this is two to the power of one, which of course is two. This would be two to the power of two, which of course is four. This here would be two to the power of three, and this of course would be eight. So what we see here is numbers doubling, right? One, two, four, and so on. So already we can guess how this will go on. So this would be two to the four, which will be 16. This will be two to the fifth, which will be 32. This here will be two to the sixth, which will be 64. And this here will be two to the seventh, which will be the number 128. So this is what it means. So in other words, an integer in Python consists of a fixed number of bits. We read them by convention from right to left, but we could also do that differently, but again, the convention just goes from right to left. And the reason why is because Arabic numbers, so the number system that we also use in the Western world is going from right to left. So the tens go to the right and the 100s go to the left of it. So that's just the way of how we do arithmetic. And depending on what a certain piece of information at that position is, so if it's zero or one, that means we will simply take this number here as the meaning, the semantic meaning of it. So let's do a couple of examples. I know that this may have been so far a bit theoretic. So let's do a very easy example. So several examples. So the example that I'm going to use is the number three. And again, I told you that this is the number three here, OK? So maybe let's put the three here. So why is that the number three that I just draw in the memory diagram? Well, if we go from right to left, if I put a one in the zero bit and I put a one in the one bit, then this would mean this first one here implies that we use this one. And this one here implies that we are going to use this two. And then comes only a series of only zeros. And what that means is we will use none of these numbers, OK? And then what we are going to do is we are going to add up. And maybe I can use that. I can do that here. We write the one here. And we add together the two that we get. And we get from up here, we always get the zeros back. And so if we add all the zeros plus two plus one, we get the number three here, OK? So and what usually sometimes people write is to express the idea that this is a number in the decimal system. We will simply go ahead and write a lower 10 here to indicate that this is a number in the decimal system as you know it from elementary school, OK? So this is it. So let's do another example so that you get this down. It's not super hard. So the number nine, how does it work? Well, let's find a combination of bits, so of zeros and ones, that make up the number nine, OK? Well, quite easy. We have eight here. We have one here. And that means we have one and one. And all of the other bits simply go to zero again, OK? So we have one plus eight gives me nine. That's it. That is the number nine in bits, in so-called binary representation. So binary means that there is one of two values, and the one of two values is basically referring to the zeros or ones that we see here. So that is how a computer represents the number nine. So in a system where we only use eight bits, so eight ones and zeros to model numbers, this would be the sequence. Of course, in Python we can model numbers greater than numbers with more than eight bits, OK? But before we continue, let's do one more thing. So let's say we want to add the number three to the number nine, just simple plain addition. So how could that be done here? Well, it works just like you all remember from elementary school, so maybe you don't remember. So let's do a little review. So we have here one plus one gives me two. And whenever we go beyond one, so whenever the summation becomes greater than one, what we do is we carry over one and we write down here zero. So in other words, if I say one plus one is zero and we carry over one, so let's carry it over here, and then we have zero plus one gives me one, plus one gives me two. So in other words, I'm going to write down zero and carry over one. Then I have one plus zero plus zero gives me one. One plus zero gives me one. And then we have four times simply zero plus zero. So now let's translate what this means. Well, this is the number eight and four, and eight and four together are 12. So in other words, three plus nine gives me 12, okay? So this is just to exemplify to you that you could do all the arithmetic, everything you learned in your time in elementary school, you could basically also use it in the world where we only live with zeros and ones. So that's how computers work. And computers are very fast at this. So at the end of the day, that is what computers do when we do anything. Okay, so let's put in two more numbers on this page and then leave it at that. So special numbers, of course, the number zero in the decimal world, the number zero in the decimal world is simply the sequence of all zeros. And now there's another kind of special number, the sequence of all ones. And the sequence of all ones, in this case, is the number 255, okay? So in other words, I have eight bits here and eight ones make up the number 255. Now, you may wonder how many bits are there? Well, this is basically implemented inside Python and we don't really care about this. So Python will, what Python has built in, what we don't have to do is, Python automatically makes these integer objects so big so that the number, the zeros and ones in there are enough to model basically any number we want up until to some built-in upper limit. There's a very big number, very, very big number beyond which Python cannot go and other programming languages also have this limitation and the reason why there is an upper limit to all the natural numbers, to all the integer numbers is of course because the numbers, the bits here that we can use to make up a number, they are just at some point limited, right? We don't have infinite memory. Now, maybe another question you may have is, is it possible to have a second combination of zeros and ones to make up the same number? So in other words, could I make up the number nine or the number 12 with a different combination of ones and zeros here? And the answer to that is simply no, okay? So it is theoretically proven that I can only make up or I can only, there's only one combination of zeros and ones to make up every single number here. So why is 255? So well known in computer science, well, if you group together eight bits, just like this, we have eight bits, we call that simply one byte, okay? So if I say something is one bit of information, it is simply, the information is simply a yes or no. And if I say something has one byte of information, then it is simply the basically saying eight yes and no answers in parallel, okay? So groups of eight is usually how computer programmers think. So let's quickly go back into the lecture materials and trip the lab and see how you could, if you wanted to see these binary numbers in Python, there is a built-in function called bin for binary and it takes a integer as the argument. So let's give it three and indeed I get back one one. So maybe you're confused about the zero B here. Well, the zero B, first of all note, we get back a text object and what does the zero B mean? Well, zero B, so let's write it like this, zero B simply implies an integer number in binary representation. So what we could do is if I wanted to say the number three in binary, I could say zero B one one and I get back three, okay? Because it's the same three as the three I write in here, okay? So this is the binary representation and this is also the reason why we cannot simply go ahead and prefix a numeric literal with a zero, okay? If we start with a zero in a code cell, then for example, the B must follow. There's also other options that we will see in a bit but if the B's following, so zero B always indicates that we have a binary number and of course, if I want to say the number 255, we just learned that 255 is eight ones, so let's try that. So let's simply go ahead and write one one one one and then use the underscore one one one one and maybe also put in a second underscore simply to make this more readable. So what this now says is in the binary system, have a number of two groups of four. So let's see what number that is and indeed it's the number 255. It's one byte of information, okay? And that is how numbers are represented. Why do I talk about this in much detail? Well, the reason is that the memory in the computer is always discreet. So there's always a fixed position of places where we can put either energy or no energy, so either zero or one. And so in other words, whenever we want to model something that in the real world is infinite, for example, real numbers, we will see that in the next video, then how can we do that? Well, we can't really do that. So you cannot model something infinite or you cannot model everything that is infinite in real world with just finite amount of information. We are going to lose something. And what we see here is why we are going to use something because there's only a finite number of zeros and ones. And but one thing we do know is what I can already tell you, integer data type is a so-called precise data type. So the number three here is precise and we will see what preciseness means in next video when we talk about floating point numbers, which are inherently imprecise. That's already a warning, something that you really, really have to keep in mind even as a practitioner that numbers, especially floating point numbers, they are not what they appear to be sometimes. So let's maybe go ahead and take this to one further level. So what you could do is you could take, for example, this text object and you could also use the constructor to create from a text object, let's make double quotes. And integer, however, if you do so, what you need to do is you need to go ahead and you give the constructor a base and we give it a base of two for binary and now we get back to the integer three as well. So sometimes data is obtained in this way and we have to like make it real numeric data. So this would be one way to do it. So let's now come to another topic which is very, very simple or very, very similar actually. So let's look at the so-called hexadecimal representation. So what is the hexadecimal representation? So it is like the system that you know from high school, the decimal system with 10 digits. So the 10 digits in the decimal system are zero, one, two and until nine. Now the binary representation is basically a system with just two digits. So maybe let's write that. A system with just two digits, namely zero and one. And the hexadecimal system is a system with 16 digits. Okay, so now you may wonder how do they look like? Well, obviously they start with a zero and one and then also let's write down two and so on. And now when we reach the nine, so let's maybe write here nine, how do they go on? How do the digits go on? So how can we create for ourselves more digits than 10? Because on our keyboards, we only have 10 digits. Well, what happens is we are going to use the letter A simply, maybe let's do it like this, A. And then of course, the next one would be B and so on. And the last one in the hexadecimal system would be F, okay? So simply instead of, yeah, having simply 10 digits, as we know it from high school math, we now have 16 digits. Okay, so that's the only really thing that is different. And one thing that you can know from this kind of diagram I drew here is if you look at groups of four bits, this is basically one hexadecimal digit. So let's say maybe mark it here. So this is a hexadecimal and this is so-called hexadecimal digit. So in other words, when we feud the data as groups of eight, we call it a byte. When we feud as groups of four bits, each we look at a hexadecimal representation. So how can we see that in Python? So let's go ahead and use the built-in function called hex and let's give it the hex function, the number three as before, and we get back zero x three. And what does the x stand for? Well, the zero x stands for hexadecimal representation. So maybe let's also write it down. Zero x means hexadecimal representation. And the three here means this is just a digit three coming from the right. And now let's look at a bigger number. So let's look at, for example, at the integer 11 and we get back the digit B indeed, okay? Because A is 10, obviously, and B then is 11. And so let's do two more of those. So the hexadecimal representation of zero would be just zero x zero, zero x as the prefix. And if we go ahead and say, what is the hexadecimal representation of the number 255, it would be simply be double F. So F for the first bits and for the first group of four bits and the other F for the second group of four bits. And why is that important? Well, the numbers between zero and 255, you may have seen in many, many places on the internet. So when it comes to colors in HTML, for example, and many, many other things, oftentimes numbers are represented on a range between zero and 255. And whenever you ask yourself, why is 255 such a weird limit? Why is it so often used? Well, if you look at only whole numbers between zero and 255, you're talking about all the numbers that can be represented using two hexadecimal digits. So going from zero, zero to FF, basically. And I only cover this here because oftentimes when you're given data, they come in this format. Okay, so oftentimes when you really look at really raw data, they come in the hexadecimal representation. And that is also the most generic representation in which, for example, different computers exchange information in. So one computer in the background may send hexadecimal or data in hexadecimal representation to another one, and they always encode and decode all the other data into this format. So it is something that as a data science practitioner, we will not see in daily life, of course. It is a little bit more theoretical, I know. However, it is something really worthwhile to at least know that it exists because yeah, it is every once in a while, maybe once or twice a month, you are given data that comes in a form in this form and you have decoded, so to say, and do something with it. Okay, so this is everything you need about numbers. So a lot of the stuff you knew before, the underscore is quite new, I guess, but it's still nice to know. And then the binary representation, the whole point of this is just to get you an idea of how this works in memory. And then in the next video, when we talk about floating point numbers, we will contrast how floating point numbers work in memory with that here. And then we will see why floating point numbers are inherently imprecise. That is the whole point of this. So I will see you in the next video.