• 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 main difference between these two kinds of values:

Integer and Float in Python

The difference between these two is that 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 within the math operation. Using descriptive variable names can help give context about what the math operation represents.

12

We can also store the result of a math operation to a variable as well using the assignment operator =.

We will usually have you do this in the code exercises to check if your expression generates the correct result.

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 calculating the dollar amount of a monthly payment on a loan (Equal Monthly Installment or EMI).

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 code author.

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

154.38548432685917
154.38548432685917