Lesson 04: Python Integers and Floats

Watch the video above.

Now that you understand how strings work, let’s begin exploring another type of data: numbers. Numbers in Python exist in two chief forms: integers and floats. As noted in Lesson 02integers are numbers without a decimal point, whereas floats are numbers with a decimal point. This is am important distinction that you MUST remember, especially when working with data imported from and exported to Excel.

As digital humanists, you might be thinking to yourself, “I just work with text, why should I care so much about numbers?” The answer? Numbers allow us to form quantitative analysis. What if you want to know the times a specific author wrote to a colleague or to which places he wrote most frequently, as was the case with the Republic of Letters project at Stanford? In order to perform that kind of analysis, you MUST have a command of how numbers work in Python, how to perform basic mathematical functions on those numbers, and how to interact with them. Further, numbers are essential to understand for performing more advanced functions in Python, such as Loops, explored in Lesson 09.

The way in which you create a number object in Python is the to create an object name, use the equal sign and type the number. If your number has a decimal, Python will automatically consider it a float. If it does not, it will automatically consider it an integer.

Examples of integer and float:
an_int = 1
a_float = 1.1

If we want to change a float to an integer, we can do so using the int() function. When we do this, the float will lose its decimal and the numbers behind the decimal place. We can similarly change an integer to a float using the float() function. In this case, the integer will receive a .0 at the end. In some instances, you may need to convert an integer or a float to a string. This is particularly useful when you are trying to create files based on the number of iterations in a loop. I will explain this process in future lessons, but for now, you should know how to do it. To do this, you use the str() function. In all of these cases, these functions take a single argument, the item that you want to convert.

Example of conversion functions:
int(a_float) #This will result in: 1
float(an_int) #This will result in 1.0
str(a_float) #This will result in “1.0”

Throughout your DH project, you will very likely need to manipulate numbers through mathematical operations. Here is a list of the common operations:

  1. Addition +
  2. Subtraction –
  3. Multiplication *
  4. Exponential Multiplication **
  5. Division /
  6. Modulo % #This will return the remainder, e.g. 2%7 will yield 1.
  7. Floor // #This will return the max number of times two numbers can be divided into each other, e.g. 2//7 will yield 3.

In addition to this, you will very often in loops need to identify Comparison Operators (equal to, less than, etc). Here is a list of those:

  1. Equal to (==)
  2. Greater than (>)
  3. Less than (<)
  4. Less than or equal to (<=)
  5. Greater than or equal to (>=)
  6. Not equal to (!=)

Once you have these key concepts down, test your knowledge in Lesson 04: Coding Exercise.