Data Types

In the print section, we looked at printing text. However, text isn't the only form of data that is used when programming. There are other types of data that can be used, manipulated, and printed out to the user. These types have their uses and their quirks. In this subchapter, we will look at 4 basic data types. These data types are strings, integers, floats, and booleans. While there are more than four data types, this chapter will simply look at the most fundamental data types. In subsequent chapters, the other data types will be explored in more detail. This chapter will briefly list those other data types, but to avoid confusion, this chapter will not go into detail about these data types and save it for the dedicated chapters. These additional data types are lists, sets, tuples, and dictionaries. Additionally, while this chapter will introduce strings, it will also not go heavily in-depth into the topic as there will be a dedicated chapter for strings.

Strings

Strings were the first data type we looked at in the print section. These are values contained within quotation marks ("). These can be single (') or double (") quotes. Python doesn't differentiate between the two types of quotation marks and views them both as strings. Despite this fact, it is important to note that text must be surrounded by the same type of quotation mark for it to be considered a complete string. For example, "hello is an incomplete string as there isn't a closing quotation mark. Adding a closing quotation mark after hello would fix the code, this would look like: "hello". It is also important that the closing quotation mark must be the same as the opening quotation mark. For example, "hello' and 'hello" both do not work as the opening and closing quotes don't match. "hello" and 'hello' are correct. Anything can be put inside these quotation marks and python will read it as a string. When we used the print function in the print chapter, we put every word in quotes. This is because we wanted each word to be considered text, or a string, for Python to correctly read it. In the following section on variables, we will discuss why this is so important. For now, just remember that if there's any text that should be read as text, place it between two quotation marks and Python will interpret it as a string.

Integers

While text is important, we often have to work with numbers too. In this case, we use a data type called Integers, or an int for short. These follow the same idea of integers in Mathematics being whole numbers. In order to use an integer, we can use the whole number as is. For example, 1 is the integer of 1. As these types must be integers, or whole numbers, the value 1.0 is not a valid integer. This, instead, is a valid floating point. This is a different type, but still has similar properties as they are both numeric values. As integers are simply whole numbers, we're able to use different mathematical operators to modify these numbers.

Floats

We briefly touched upon floats in the integer section. The main idea of the float type is that it consists of any decimal value. For example, 1.0 is a float. 3.14 is also a float. Floats in Python 3 can go up to 16 decimal points. These are useful when we want to keep track of decimals or need more precise information than integers. Similar to Integers, Mathematical operators can be used to compute different values upon floating point values. Some of these may have strange behavior, such as adding two floating point values together1, but otherwise, these operations behave normally.

Booleans

The last main data type is a boolean. There are two types of booleans. There are literal boolean values and comparison boolean values. At its core, a boolean is something that gives us a True or a False value. These can then be used to execute code based on a condition. We will see more of these in use when we cover conditional statements later.

Literal Booleans

Literal booleans are the explicit values of True and False. These are important as they can be used to validate data, keep code running, check if information is consistent, and serve as a placeholder for a future condition.

Comparison Booleans

Comparison booleans are statements that evaluate to True or False. These use comparison operators to check for the equality or comparative value between two values. These use the ==, >, <, <=, >=, and != to check whether two values are equal, greater, lesser, greater than or equal to, less than or equal to, or not equal to each other respectively. These are incredibly important when we write code that must be executed when some statement is True. We will see what these look like more in action later when we go into conditional statements.

Collection Types

The last data type is not one of the four main types and serves as a collective type. These are Collection Types. This type encompasses four sub-types that we will look deeper into later. These Collection types allow us, as programmers, to store multiple values in a single location or instance. We will dive much deeper into collections later in the tail end of the "Foundation" section when we look at the topics of Lists, Tuples, Sets, and Dictionaries. For now, just remember that there are means of storing different amounts of data in one location. We will address these means much later.

None Type

There is one more type that may be used called the None type. This is the literal term None and can be used to temporarily set a value or be used when no other value makes sense to be used. This will come in handy later when we need to set variables to placeholder values.


1

This is due to how decimal values are represented in binary and how they conform to IEEE-754. To read more, there's an appendix section on this situation called IEEE-754, Binary, and Floating Point Values