Posted on

Learn Python Episode #6: Numbers

Enroll in The Complete Python 3 Course: Beginner to Advanced!

Click here to subscribe for more videos like this!

Alright, so before we actually get into functional programming, or object oriented programming for that matter, with Python we need to discuss some basic types, and then variables, and some other stuff. So in this video we’re going to be covering numbers. So, what is a number? Well, I’m sure you guys know what a number is and I’m sure we’ve all been through grade primary and one so we’re all familiar with that. Now there’s two different types of numbers, one is called an integer, and then a floating point. The floating point is basically any number that has a decimal included in it. So let’s go ahead and write out some numbers on the screen. Now I’m going to be using the Python console down here for this video so watch down there, that’s where stuff is going to happen. So, for an instance of a number we can just write a number, it’s going to appear in the console, or the ID, or whatever you know has syntax highlighting that you’re using it’s going to show as blue or it should you know, actually that really depends on your color scheme that you’ve got going on, it’s going to show a specific color. Now, we can just use numbers in Python just by writing the numbers and if we hit enter it’s going to print out 5. Now with numbers we can perform mathematical operations on these numbers as you saw in some of the previous videos that we’ve done so far. So I could write 5 + 6 and we’re going to get a response of the sum of 5 + 6. Now what would happen if I wrapped these numbers in quotations? So let’s go ahead and write “5 + 6” if you think that this will produce 11, you’re wrong, It’s going to produce 56, and you might be thinking, hold up, 5 + 6 does not equal 56. You’re absolutely right. Not in math, however when you’re concatenated strings it does. So basically when you wrap something in quotations in Python, Python is going to treat it as a string, not a number. So if you’re trying to perform mathematical operations just remember don’t wrap your numbers in quotations. Now additionally we can use floating points to perform math operations, and you can see it returns a floating-point. Now if we ran 5.5 + 5.5 it’s going to still return a floating point, however you know it’s 11.0 because we’ve passed it, you know, decimal floating-point numbers and so it’s going to return with one because that’s just how it works. Now, we’re going to talk about how to convert different types of types, I guess. These are called types. A number is a type, a string is a type, and right here we’ve got, we’ve shown, both numbers and strings and we’re going to cover strings in the next video, but for right now let’s say well I have some numbers but they’re wrapped in quotations and I still want to perform mathematical operations on them, how can I do that? So, basically in Python there’s a built-in function called int which will convert a string to a number if that string contains only numbers. So let’s look at a correct usage of int and then an incorrect usage and see the output for both. So let’s say I have the number “5” wrapped in quotations, what I’m going to do is wrap that in the int function and this is also how you write a call to a function in Python, and then I’m going to perform a mathematical operation on it with another number wrapped in quotations. Now this is not going to produce 57, in fact it’s going to convert both of those into integers and then perform the math operations on them. So that’s how to add up different numbers that are contained within quotations, let’s have a look at trying to convert text to an integer. So let’s write “int” and let’s write “(hello)” and hit enter. As you can guess it’s printing out an error because there’s an invalid literal for int with base 10. Basically, what that means is we’ve passed it something that is not an integer. So think of this function basically as it takes whatever you put into the function, it removes the quotations, and then returns that. Same if I just type this, Python is going to freak out because it doesn’t know what hello is because this is going to be interpreted not as a string, but it’s going to be interpreted as computer code, and we’re going to get more into strings in the next video.