Python Data Types

This article is going to explore Python data types.

What is a Data Type?
Data Types are attributes of data which tell compilers and interpreters how the data is intended to be used(1). In Python, there are over a dozen data types, which can be categorized in several ways.

Built-in Data Types: Primitive

Python contains the Text Type 'str,' which is short for string. Strings are arrays of bytes representing Unicode characters (2). In Python, strings are surrounded by single or double quotations, and can be formatted in a number of ways. For example, formatted strings known as string literals or f-strings come with an attractive design pattern that can be enclosed in single, double, or even triple quotes. f-strings utilize curly {} braces so you can embed variables within a string. It even allows us to evaluate expressions like arithmetic, function calls, etc.! That's part of what makes f-strings so powerful and popular in Python. Technically speaking, it is totally within reason to perform calculations with numeric types within an f-string. However, f-strings, which are expressions evaluated at runtime, are not a constant value and ultimately return a string value, so even the numbers within your expression become strings in this regard.(3, 4, 5).

Other primitive data types include numeric types such as 'int' (integer), 'float' (decimal points) and complex, as well as Boolean types ('bool'). A full list of Python Data Types can be found in the links ending this article(6).

Floats & Integers
Floats stand for "floating point number" and contain one or more decimals, which differentiate them from the integer primitive data type. In simple terms, think of why you would want this: 4 / 2 = 2, but 5 / 2 = 2.5. In some limited forms, a computer might even process this to 3 or 2. To perform the math correctly, we need decimal points. However, There are a couple things to note about Floats. One, that Python is like many other programming languages. However, by comparison to the JavaScript programming language, which does not define different types of numbers, because in JavaScript they are always stored as double precision floating point numbers (7). I just thought I would point that out, as I myself am coming from a JavaScript background, so this was a new, albeit basic data type for me. Second, floating point arithmetic is by definition not completely 100% accurate, because values are represented by fractions (8, 9). This might not affect your day to day programming life, but it is good to know. For example, at work, where I support a Salesforce application that is built with Apex, precise calculations can commonly be off by a penny, which doesn't seem like it would matter, until you are working with multiple multi-billion dollar businesses who frequently utilize reports and perform calculations requiring exact precision. Oh, major third one I just learned now: Floats can also be scientific numbers with an "e" to indicate the power of 10: 35e3.

An integer is a whole number, positive or negative, without decimals, of unlimited length. I ripped this straight from w3 schools, which I have already linked below. Common integers include 0,1,2,3,4,5,6,7,8,9,10, and so on. There isn't a whole lot to cover for integers right now. The number 0 is kind of a unique case however, in that it is also a way to say False (as opposed to True, which could be described as 1 in programming). This is because in Python and other languages, individual values can evaluate to either True or False: values that evaluate to False are considered Falsy, and values that evaluate to True are considered Truthy(10). Before I get into any more examples, allow me to first discuss the Boolean primitive data type.

In the 1800s, George Boole defined an algebraic system of logic(11). All that matters for now is that Boolean data types in Python represent one of two values: True or False. This simple Yes/No sort of value works perfectly for if-statements, as well as for loops. Wherever your code requires a condition or to meet a certain criteria, that criteria should be cleanly defined. Simply put, that condition is either met or not met. Boolean data types are critical pieces of data because of how powerful they are. Also, you won't exactly be seeing True and False written out all the time, but rather the evaluation of an expression that reduces to one of those values. Keep in mind I am a beginner, so part of this information is my observation and should not be taken as gospel.

Here is an example of a string, integer, and Boolean data type:
print('Are you legal drinking age? ', 18 < 21)

Even in such a simple statement above, we are using a built-in Python function that returns human-readable output to the screen. We are passing arguments (not parameters) into this function(12). We are using the 'Less than' comparison operator to compare if 18 is less than 21. Since this expression evaluates to False, what the print function returns to us is actually the word False, since the above expression evaluates to a falsy value.

Expressions are representations of value - they need to be evaluated(13). In Python, an expression only contains identifiers, literals, and operators. Identifiers are names given to the fundamental building blocks in a program, are are user-defined to represent a variable, function, or any other object(14, 15). Literals are a succinct way to write a value. In Python and other languages, literals represent the possible choices in primitive types for that language. A full list is linked below, but some examples include Boolean (True, False), String ('lesser', 'greater') and Integer literals (0, 1, -2)(16). Operators are special symbols in Python that carry out arithmetic or logical computation.

If we were to analyze our above example, 18 and 21 would be examples of Integer Literals, and < would be an example of an Operator. If we wanted to make our expression more flexible and inclusive, we could use a variable. For instance, if a user inputs their age (for me: 30, for you it may be different), we can store that number in a variable then use it in our example like this: age < 21. If we were to do this, we would then also be using an Identifier.

Logical and Identity Operators
The reason for this detour on expressions is to highlight how complex an expression can become that simply evaluates to True or False. For example, we can utilize logical operators to combine conditional statements, or identity operators to compare objects. Here are some example that use all the Logical Operators:

  1. 18 < 21 and 24 < 21 evaluates to the Boolean data type False, because with an and operator, both statements must be True.

  2. 18 < 21 or 24 < 21 evaluates to True, because with an or operator, at least one statement must be True, and the above essentially states False or True.

  3. not(1 < 0 and 2 < 0) evaluates to True, because the not operator reverses a result. If you are familiar with PEMDAS, Python follows this and so contents within a parentheses are evaluated before exponents, subtraction, etc. In this case, the inner contents evaluate to False, so reversing False evaluates to True.

Here are examples using the Identity Operators:

  1. sister, brother = 21, 21 # sister is brother - If we were to print(sister is brother) we would read True, because we are essentially asking Python to compare 21 to 21 to see if they are the same object.

  2. print('sister' is not 'brother') evaluates to True. Here, we are comparing two strings to see if both sides are the same object. I am not certain, but I believe Python String characters are comprised of ASCII encoding, which uses numbers to present English characters(17, 18). As you may have inferred, the characters in each of the above strings would boil down to different number sequences, therefore it is True that, in essence, a is not b.

Truthy and Falsy Values
One simple way to determine an item's Boolean value is by using the bool() function(19). Truthy values that will evaluate to True include Non-empty sequences or collections such as lists, tuples, strings, dictionaries, and sets, Numeric values that are not 0 (remember earlier?), and the word True. Conversely, Falsy values include Constants such as False or None, 0 of any numeric type, and anything empty of the following: lists [], tuples (), dictionaries {}, sets set(), strings "" or ranges range(0)(10, 20).

To put things into perspective, consider how problematic binary thinking is in many societal discussions. We could be talking about political policy, pandemics and vaccines, personal topics and so forth. Most therapists would advise against absolutist thinking, which can be characterized by viewing things as all or nothing, black or white, etc. This "all or nothing" principle extends to polarized thinking. On top of that, many people often generalize things that are incredibly nuanced. They don't even extrapolate. They merely assume by effortlessly abstracting away relevant contextual concrete information, until all that remains is an opinion that is hardly rooted in any fact at all. As the old saying goes, "All generalizations are false, including this one."(21)

By comparison, computers due not tend to fall to common psychological follies as humans are apt to do. If your if-statement does not run, it may have evaluated to False. Understanding something as simple as this should help you debug your program, as you investigate the Error of your code. This digression has been brought to you by writing this without a break.

Built-in Data Types: Non-Primitive

In case I didn't mention it earlier, primitive or basic structures contain pure and simple values of data. By comparison, non-primitive data structures store not just value(s), but a collection of values in various formats(22).

In Python, there are three basic sequence types: lists, tuples, and range objects(23). A sequence is any ordered collection of objects whose contents can be accessed via "indexing"(24). Lists are used to store multiple items in a single variable: friends = ['suchan', 'ross', 'desirée']. List items are indexed and start at 0. If I wanted to access 'Suchan' above, I could do this: print(friends[0]). List items have a defined order that will not change (25). I won't say it's a best practice, but I will say that it is very common that a list will all contain the same data type. With that being said, your code won't break simply by having a float, integer and string within a list(26). Bear in mind that lists and other sequence and mapping types are iterable, meaning it is capable of returning its members/elements one at a time, permitting it to be iterated over in a for-loop(27). While there may be no issue looping over a list of [1, 'two', False], note that data types have specific built-in methods that are only applicable to that data structure. So for instance, if you were looping over my friends list with the .capitalize(), that would be fine because the first letter of each element could be capitalized. However, as seen with my mixed data-type example, applying the same method would result in a Python error: AttributeError: 'int' object has no attribute 'capitalize'.

Tuples are usually used when you have a known collection of mixed types. I am still learning about them, as I rarely used them in JavaScript except in React, and even then I am pretty sure they weren't technically Tuples(28). Tuples are used to store multiple items in a single variable. Personally I think they are really cool and fascinating, but as I am brand new to them, I really don't feel qualified teaching you anything about them. So, maybe join me in watching this video Socratica has on Python Tuples!(29)

Sets are another data type I am not familiar with yet. All I know is that they are also used to store multiple items in a single variable. The only thing I will state for now, is that in comparison to Tuples, which are Ordered, Unchangeable and Allow for Duplicates, Sets are Unordered, also Unchangeable and do not Allow for Duplicate values(30, 31).

I can finish off this article strong by talking about Dictionaries. In JavaScript, what I know there as Objects are considered in Python to be Dictionaries. In all honesty, I think it's a better name, since both languages are object-oriented.

Dictionaries are Mapping data types that are used to store data values in key:value pairs. Things get pretty wild here, because a dictionary can hold dictionaries. For instance, two keys in a dictionary could be Quarterback names like 'Patrick Mahomes' and 'Tom Brady', but their value could be a dictionary that holds further keys such as passing stats, with each corresponding value being a list of say, yards, touchdowns and interceptions, over the course of each week of a season. In contrast to Tuples and Sets, Dictionaries are Changeable, meaning that we can change, add or remove items after the dictionary has been created(32). Compared to List syntax, dictionaries will roughly look like this: parents = { "mom": "Marie" }, though a dictionary's values can be of any data type(33). By comparison, a dictionary's keys must be of an immutable data type such as strings, numbers or tuples (34). A list of Immutable and Mutable Data Types in Python can be found here(35).

Wrapping Up
As you can see, there is a lot to learn about Python data types. Mastering the fundamentals practically takes you into becoming hirable in many fields. I know I have a lot to learn when it comes to the non-primitive data types in particular. Understanding how to use each, or at least the most common data types in Python are exponentially extended once you begin forming programs with built in methods, user defined functions, classes, if-statements, for loops and so forth. At first, so much information will populate your short-term memory, that you may feel anxious because no brain is wired to absorb all that information at once. Even computers have finite memory.

If you are interested in learning more about Python, I strongly recommend googling FreeCodeCamp and Socratica. If you are eager to learn more about the brain and how to learn, I strongly recommend the course below(36).

In closing, I understand this was not a complete tutorial into the data types of Python, but for you and for me, this was a beginning and a direction as to what you can learn next. Feel free to reach out to me on Twitter (@jonamichahammo ) or LinkedIn ( I keep it informal on the bird and formal on the business social media, so choose one or both, or neither!

I hope you've enjoyed. Bye bye.

A full list of Python Data Types can be found here: