Practical Guide to Python Numbers
Transcript from the "Numbers" Lesson
>> So, numbers in Python, these are all integers. And we can always verify that by calling type. There are also floats in Python so numbers that have decimal points Something that's a little tricky that might bite you. This is a valid float. You don't need to have a decimal point after.
[00:00:40] You shouldn't really define your floats like this. It's kind of confusing. But if you see this in Python code, just know that's not a syntax error. And then there are also complex numbers. And these end with the character j I'm not a math person. I don't think I've ever used complex numbers in my Python code, but they're there if you need them.
[00:01:05] Because like you saw earlier, these types are just objects under the hood. You can also declare them by calling the constructor for that built in type. So I can say, into five I can also pass in a string in here. Same thing for float if I pass in, let's say an integer into the float constructor will return a float back.
[00:01:34] Generally you never want to do this in your code unless you are converting between types. But this is something that you might see. One example of converting between types as if you're using an old style string formatting. If you try to, for example, concatenate when we'll talk about strings a little bit later.
[00:01:57] A string with a number you're going to get a type error that says you can only concatenate strings and not stings. So in this case, you can pass in the number five to the stir method. Again, with modern Python string formatting, you won't really ever need to do this, but could happen.
[00:02:18] You can do all the standard mathematical operations that you might expect with Python numbers. You can add them up, and you can multiply them. You can divide them. Note that if you divide two integers, the type that you get back is going to be a float. If you want to do integer division, you need to use 2 slashes and that will get you an integer back.
[00:02:46] You can also raise to the power. So let's say we have x equals 4 and y equals 3 we can say x to the power of 3 or x to the power of y. So everything that you might expect, and then you can also use parentheses to make sure that things happen in the order that you want.
[00:03:16] And that is all valid Python code. Python has a few more built in methods that are useful for numbers. And you might hear me say built in methods, what does that mean? You saw earlier in our tour of a Python program that I had to import the request module.
[00:03:34] Later on, we'll see how to import modules from the standard library. When I'm using these built ins, I don't need to import anything they're just available in Python. And the three helpful methods for math are a min and max or round. So for min you could pass in some values, And it will always return the smallest of them.
[00:04:00] The same is true for Mac's except the opposite. This will return 10 thousand because it is the largest number in that list. You can also use round to round down decimals. Or round them up. There are built in Boolean types in Python just like we saw that none is a keyword.
[00:04:26] In Python the Boolean types are defined with uppercase true and lowercase false. These are keywords. Something of note in Python that I've seen trip people up. Again in Python, everything is an object. These Boolean are objects and in fact, under the hood they are subclasses of integers. And that means that under the hood true has a value of one and false has a value of zero.
[00:05:00] Don't do this Please, but because of that, you can actually add these together or do anything, but you could do to numbers, don't do this but Python fun fact.