 FREELessons: 48Length: 7.6 hours  • Overview
• Transcript

# 2.2 Numbers

You can't do much in the world of programming or computers without numbers. So it only makes sense that we start with the numeric data types when we're learning Python.

## 9.Connecting to Network Services3 lessons, 34:27

### 9.2Getting Websites With HTTP12:57

Now that you are becoming a little bit more familiar with the programming environment within Python using the interactive show let's try to use it a little bit more to learn about how Python and the shell can be used to start to formulate and structure and application. But before we do that, we need to talk about some of the building blocks of Python. And when I talk about building blocks, I'm talking about some of the foundational data types that you're gonna find yourself using quite often. Now there are three main data types that kind of are put together and built into more complex data types that we'll start to talk about a little bit later. But basically what I'm talking about now is numbers, strings, and lists. Now this lesson we're gonna talk a little bit about numbers and then ultimately variables, and how you can store different data types, and how you can store these values in memory so that you can use them throughout your application, or throughout your session within your interactive shell. So, in order to start we're going to go into Python and we're going to start to work on numbers. Now, you keep hearing me say the word numbers and you may be thinking to yourself well that seems kind of generic,what about in integers, and decimals, and floats, and things of that nature. Well in Python, we absolutely have those things, but because we are not explicitly telling the shell or the environment or Python itself, what exactly the data type is it's being inferred for you. So, if I were to type in the number one, it's going go say okay, I'm going to infer that this is the integer value of one and that I can do integer type operations on it. Same if I were to say 1.0, it's going to interpret that as being a floating point number. So, these types of things are done behind the scenes for you, you really don't have much control over them. There are a few operations here and there that you can look through and find in the documentation about converting one to the other. But for the most part these are these things are going to be handled for you behind the scenes. So as I mentioned in the previous lesson you can start to do basic mathematical operations. I can say things like 2+2, that's going to receive result in 4, and 2-2 is going to result in 0. I can do basic multiplication. I can do 5 x 2. I'm gonna get 10. These things do not come as a surprise. Now some things that make come as a surprise to you are how you combine these data types and get possibly other data types. So as you can see here, I'm dealing with pretty much all integer values here and when I do addition and subtraction and multiplication on integer values, I get back integer values. The sum is an integer value, or the result is an integer value. But when I start to work in division, things start to change a little bit. So let's say I had the integer value of ten, and I divided it by the integer value of two, the result is going to be possibly a little surprising. You're going to see here that yes It is five and that is a correct answer, but it's giving us back the value in a floating point number. And that is one thing that you have to bear and note when you're dealing with mathematics and especially division within Python. The result is always going to be a floating point number, regardless of whether or not the two values going into the equation are integers or floating points. The result is always going to be a floating point. The same thing can be said if I'm multiplying two numbers together where at least one of the values is a floating point value. And the result is going to be a floating point value as well. Just something to keep in mind as you're doing mathematical operations within your Python script. Now other somewhat more interesting operations that you might find yourself working on or seeing out in the wild. Has to do with being able to slice the floating point values up a little bit and see different parts of them. So let's say you were to do some sort of operation here where I did 17 divided by 3. I'm going to get a five, followed by a rather long decimal portion of this floating point number. So let's say I wanted to just get this 5. What we would call the floor of this floating point number. How would I get that? Well, there's another operation that you can use, another symbol, which is going to be double divided by sign. Now this is going to return to me just the integer portion, the floor of that result, and that's going to be five. Now another operation that you're probably gonna see relatively frequently out there is going to be, how do I get the remainder of this division operation? So, yes 3 does not go into 17 evenly. It's going to give me five and a remainder that's represented by this decimal here, by this floating point number. But how do I get what the actual numeric value of that remainder is? And you're going to use the modulus operator. And if you've ever done any sort of other programming out there before, which I'm assuming you have, then you probably know that's typically represented by the percent sign. If I were to do that, you're going to see that the remainder of this division operation is going to be, 2. And then also if you ever wanted to strip off this back end of this floating point number and get just the decimal portion, you could absolutely do that as well by doing 17 / 3, and then subtract from that the floor division value, which is 17 with the double divided sign, 3. And you're going to get back just the equivalent of that decimal portion. Yes, you still get to 0 here, but at least you're just getting that decimal portion. So if you ever needed to really slice and dice some numbers and get some of those values that way, that would be how you would do it. Now when it comes to the multiplication side, a lot of times in multiplication you start to deal with things like exponential numbers, or exponents, or, numbers raised to a power if you've ever heard of it referred to that way as well. And there's a very simple way to do that within Python as well. And that's simply using the double multiplication symbol. So let's see how we would do something like that if I wanted to take 2 and raise it to the third power I would simply say 2 with a double multiplication sign. Raised to the third power that's going to result in eight. So that should come as no surprise, but just a kind of bear in mind and keep that in mind that you can use the double multiplication and the double division signs to do additional mathematical operations. Now, yes I do agree that this can get a little boring just playing around with numbers, so let's start to make this a little bit more interesting and bring this home a little bit, and give you a little bit more of a real world example. Let's say I wanted to build a calculator that's going to calculate for me the total amount that I owe to a restaurant or some sort of local watering hole after I have finished going there and enjoying the service. Now let's say that's gonna be broken up typically into a total bill amount and then a tax amount, and then I need to figure out how much I owe the establishment for their services and ultimately the meal or the drinks that I've just enjoyed. So let's say I have the bill, so I can define what's known as a variable within Python and I can simple just give it a name. I don't have to give it a type, it's going to be figured out or inferred for me. So I can say that the bill, we'll just give it the name, is going to be equal to say $15 and 25 cents. So now I've assigned a value, the floating point value, of$15.25 into the variable with the name of bill. So now any point I can just type in bill and it's going to give me that value and I can see that very simply. Now let's say that the current tax rate at the establishment that I'm in or the state or wherever it is that I am is 7.25%. Now this is a percentage value, so if I wanted to use it in any sort of operations, mathematical operations, that I can't does use it this way it has actually have to divide it by 100 first and then work with it. So then let's say I needed to figure out what the total is that I'm going to owe at the end of the night. So I can take $15.25. And then add to it 7.25% of that and that will give me my total. So then I could do something like the total is going to be equal to, and I know it's going to be at least$15.25, and then I need to add to that 7.25% of that to add in the tax. So the first thing that I want to do, is I want to convert that 7.25 into the actual, decimal percentage rate that I need to multiply by 15.25. So I'm going to do that operation within parentheses and i'm going to say I want to first take 7.25 and I want to divide that by a 100. And then the result of that, I want to multiply by the bill. So that's going to take the bill value. Which I could, actually, instead of saying all of these things here. I could simply say, I want the total to be equal to the bill, plus tax divided by 100, once again, times bill. So, once I finish that operation, I can see that my total is going to be a little over \$16.35, or 36 if you wanted to rounded up. So there's a basic operation that you can use very quickly and very simply to create an application to do just that. So I can now calculate the total amount that I owe taking into consideration not only the bill total, but also the tax amount. Now a couple notes about variables now that we've begun talking about them. Variables are always given a default value. And typically if a variable is not defined, so as you can see here I have defined bill here to be 15.25, so if I ask for the value of bill, I'm given 15.25. But if I were to try to type in the name of a variable that doesn't exist, like say sum, which is a bad example, because it ultimately is the name of a function, so let's just say SumTotal. You're going to see here I'm going to get an error to say that sum total is not defined. So that's just something to keep in mind that just because I can start typing in names of variables or what could be variables. You always have to be cognizant of whether or not those things actually exist because if they don't then you're gonna see this error, and I can promise you you're gonna see similar errors to this quite often. So now that we've gone through the process of understanding the basic process of using Python to work with numbers, and do basic division and multiplication, and even assigning values to variables so that we can work with them in basic equations. Let's start to move on to the next basic building block within Python, which just so happens to be strings.