• Save
  • Run All Cells
  • Clear All Output
  • Runtime
  • Download
  • Difficulty Rating

Loading Runtime

There are two ways of representing numbers in Python: integers (ints) and floats.

Let's look at the number 3 represented as both an integer and a float to emphasize the difference:

Integer and Float in Python

The float value 3.0 contains a "decimal portion" (the .0 part).

Integers can only be whole numbers while floats can be any number –as long as that number contains a decimal portion.

  • Examples of Integers: 30, 9001, 1729, -27, 0, etc.
  • Examples of Floats: 2.5, 3.141529, -.03, 3.0, etc.
3
3.0

Data Types and the type() function

The category of value that we store to a variable is called the variable's "data type" or "type" for short. If we ever want to check a variable's data type we can do that using the type() function.

Just like the print() function, when we use the type() function we put –inside of the parentheses– whatever it is that we want to check the type of –and the variable's type will be displayed in the output section of the code cell.

<class 'int'>
<class 'float'>

Math with Python

Python can do math operations using the "Arithmetic Operators". The most common Arithmetic Operators are:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Exponentiation: **

Just like in math, we can also use parentheses: (, ), to enforce a specific order of operations as well.

Any stand-alone math expression will be evaluated and its resulting value displayed –when we run the code cell.

Feel free to change any of the expressions in the following code cells to experiment with your own math calculations.

8
11
16

We also have the option of storing these numbers to variables and then using the variable names as part of the math operation. Using descriptive variable names can help give context about what the math operation represents.

12

We can store the result of a math operation to a variable using the assignment operator = –just like we did when we stored integers to variables back in the "Python Variables" lesson.

I'll ask you to do this a lot in code exercises because it makes it easier for me to check your work when all I have to do is check to see if a variable has the correct value stored to it.

12

Here's another example (this time using floats). This example simulates being charged sales tax after buying items at a store.

39.22

It's totally fine to combine the use of floats and integers in a single math calculation. When we do this, the resulting value will usually have the datatype of float.

Let's show the power of all of these concepts in action by doing a more complex calculation. It's not important that you learn anything specific from this calculation, I just want to give an example of how we might translate traditional math into Python. The example that we'll use is the equation for calculating the dollar amount of a monthly payment on a loan (Equal Monthly Installment or EMI). I chose to use this equation for the demonstration because it involves every single one of the Arithmetic Operators that we mentioned above.

EMI Monthly Payment Formula

In some math-heavy applications of Python, it may be acceptable to use single letter variable names, however, we typically prefer that our variable names be descriptive and somewhat self-documenting with readability and maintainability being our highest priorities. How to properly balance the competing priorities of descriptive variable names with readability and conciseness is ultimately up to you: the author of the code.

To try and demonstrate the two extremes, the following code cells perform the same calculation, but use different variable names.

154.38548432685917
154.38548432685917