From the course: Programming Foundations: Fundamentals

Working with numbers - Python Tutorial

From the course: Programming Foundations: Fundamentals

Working with numbers

And before you start to have flashbacks to high school algebra, I'm not talking about complex mathematical calculations. We use numbers numbers to store a person's high score in a game, or even the number of likes on a picture. Python treats numbers in a few different ways depending on how they're being used and the difference is largely based on whether it's a whole or fractional one. Let's look at a few examples. We can use the command line for this as we just want to do some basic math. So let's go to the command line and start our Python prompt. We'll start out easy, two plus three. Let's try another one. Two, asterisk and then three. When we hit Enter, we get six. An asterisk or star is used for multiplication in Python. Let's go to another one. This is used for exponents. It's exactly the same as two times two, times two. Let's try another one. This is how we do division inside of Python. Now, what if we didn't want to have a remainder as a result of our division. Then, we could take advantage of a separate operator called the floor division operator. Let's see that one now. We'll use a three with a space and two forward slashes and then our two. This time, we only get one as a result. And the converse is also available to you. If you only wanted the remainder, For that one, we're going to do five, the percent sign This is going to give us two because three only goes into five one time and has a remainder of two. like 45,000, how does Python handle that? Let's take a look. We'll type 45,000 and then we're going to divide it by five. Not what we were expecting. That's because the use of the comma really confused the Python interpreter. So no matter how large the number is, we use no commas when we're working with Python. So let's try that again, this time, we're going to remove the use of the comma. 45,000 divided by five and there you have it, we get the answer that we were looking for. Now, here's a table of the most common operators that you'll work with. So far, we've only been using whole numbers or integers to do our calculations but in the real world, fractional numbers are a daily part of our lives. would you be okay if we just drop the 75 cents and only paid you $24? Of course not. That extra 75 cents has value to you. In some programming languages like JavaScript, There's no distinction between numbers with and without decimal places. and without decimal places. Whereas in most programming languages, Whereas in most programming languages, numbers with decimal places are considered special numbers with decimal places are considered special and they're typically called floats. and they're typically called floats. A float is any number with a decimal point. A float is any number with a decimal point. It's short for a floating-point number. It's short for a floating-point number. It's used for when more precision is required like in the case of our hourly rate. Let's look at how Python works with floats. First, we're going to try 0.2 plus 0.3. First, we're going to try 0.2 plus 0.3. And when we hit Enter, we get 0.5. We use two floats and we get back a float in our answer. We use two floats and we get back a float in our answer. Let's try another one. 0.2 divided by 0.3. 0.2 divided by 0.3. Whoa, it's a huge number. That's because Python tries to be as precise as possible. That's because Python tries to be as precise as possible. You may notice lots of extra decimal places when working with floats. Just ignore them for now. Just ignore them for now. Later, we'll learn how to tweak it to our own liking. Later, we'll learn how to tweak it to our own liking.

Contents