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

String
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.

Booleans
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
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).

ASIDE
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).

Lists
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
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
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).

Dictionaries
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 (linkedin.com/in/jonamichahammo). 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:
https://www.w3schools.com/python/python_datatypes.asp

Citations:
(1)https://en.wikipedia.org/wiki/Data_type
(2)https://www.geeksforgeeks.org/python-data-types/#:~:text=Tuple-,String,is%20represented%20by%20str%20class.
(3)https://www.datacamp.com/community/tutorials/f-string-formatting-in-python?utm_source=adwords_ppc&utm_campaignid=1565261270&utm_adgroupid=67750485268&utm_device=c&utm_keyword=&utm_matchtype=b&utm_network=g&utm_adpostion=&utm_creative=332661264365&utm_targetid=aud-438999696719:dsa-429603003980&utm_loc_interest_ms=&utm_loc_physical_ms=9001941&gclid=Cj0KCQjw9O6HBhCrARIsADx5qCQwM75K4E4MKs5Zf90V5j3VSQZPMii1_Kaiv6zxNeTAgIpfy47gq-0aAthCEALw_wcB
(4)https://www.python.org/dev/peps/pep-0498/#:~:text=F%2Dstrings%20provide%20a%20way,which%20contains%20expressions%20inside%20braces.
(5)https://www.geeksforgeeks.org/python-data-types/#:~:text=Tuple-,String,is%20represented%20by%20str%20class.
(6)https://www.w3schools.com/python/python_datatypes.asp
(7)https://www.w3schools.com/js/js_numbers.asp#:~:text=Unlike%20many%20other%20programming%20languages,the%20international%20IEEE%20754%20standard.
(8)https://stackoverflow.com/questions/6563058/how-do-i-use-accurate-float-arithmetic-in-python#:~:text=Floating%20point%20arithmetic%20is%20by,as%20another%2C%20more%20general%20article.
(9)https://stackoverflow.com/questions/11522933/is-floating-point-arbitrary-precision-available
(10)https://www.freecodecamp.org/news/truthy-and-falsy-values-in-python/#:~:text=Truthy%20values%20are%20values%20that,type%2C%20None%20%2C%20and%20False%20.
(11)https://en.wikipedia.org/wiki/George_Boole#Symbolic_logic
(12)https://developer.mozilla.org/en-US/docs/Glossary/Parameter
(13)https://www.hackerearth.com/practice/python/working-with-data/expressions/tutorial/#:~:text=Python%20Expressions%3A,of%20the%20string%20as%20well.
(14)https://data-flair.training/blogs/identifiers-in-python/
(15)https://www.programiz.com/python-programming/keywords-identifier
(16)http://net-informations.com/python/iq/literals.htm
(17)https://www.tutorialsteacher.com/python/string-isascii
(18)https://www.ascii-code.com/
(19)https://www.programiz.com/python-programming/methods/built-in/bool
(20)https://www.freecodecamp.org/news/truthy-and-falsy-values-in-python/#:~:text=Truthy%20values%20are%20values%20that,type%2C%20None%20%2C%20and%20False%20.
(21)https://www.brainyquote.com/quotes/mark_twain_137872
(22)https://datalya.com/blog/python-data-science/difference-between-primitive-and-non-primitive-data-types
(23)https://docs.python.org/3/library/stdtypes.html#:~:text=There%20are%20three%20basic%20sequence,%2C%20tuples%2C%20and%20range%20objects.
(24)https://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/SequenceTypes.html
(25)https://www.w3schools.com/python/python_lists.asp
(26)https://stackoverflow.com/questions/11387752/am-i-safe-mixing-types-in-a-python-list
(27)https://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Iterables.html#:~:text=An%20iterable%20is%20any%20Python,over%20in%20a%20for%2Dloop.
(28)https://ntgard.medium.com/tuples-in-javascript-cd33321e5277
(29)https://www.youtube.com/watch?v=NI26dqhs2Rk
(30)https://www.w3schools.com/python/python_sets.asp
(31)https://www.w3schools.com/python/python_tuples.asp
(32)https://www.w3schools.com/python/python_dictionaries.asp
(33)https://www.google.com/search?q=can+a+dictionary+value+be+any+data+type&rlz=1C1CHBF_enUS922US922&oq=can+a+dictionary+value+be+any+data+type&aqs=chrome..69i57j33i160.7258j0j9&sourceid=chrome&ie=UTF-8
(34)https://realpython.com/lessons/restrictions-dictionary-keys-and-values/#:~:text=Almost%20any%20type%20of%20value,objects%20like%20types%20and%20functions.&text=Second%2C%20a%20dictionary%20key%20must,Boolean%20as%20a%20dictionary%20key.
(35)https://towardsdatascience.com/https-towardsdatascience-com-python-basics-mutable-vs-immutable-objects-829a0cb1530a
(36)https://www.coursera.org/learn/learning-how-to-learn

16