Python Fundamentals
Feb 14, 2023 · 2 min read
This article shares important context on some of the fundamentals of Python. I recommend coming to understand these concepts right away when you start learning, such as functions, classes, data types & structures, and modules & packages.
I use Pluralsight, an online education platform, to help me learn skills like Python faster and to track my progress. I highly recommend signing up for free to use their Skill IQ tests to assess your proficiency in Python and receive helpful feedback on what you should focus your learning on!
NOTE: This article was inspired by my much larger article on Learn How to Program in Python.
You can install Python onto your computer following these instructions. As a note, there are two major versions of Python that are in use today: Python 2 (legacy version) and Python 3. The older Python 2 is no longer actively maintained, but is still in operation due to the use of legacy applications from that time period. Python 3 is the recommended version of Python and is the version that is actively maintained today.
As you start learning Python, there are important building blocks to be aware of, namely functions, classes, modules, packages, data types, and data structures. These topics are closely inter-connected with one another on their use-cases and how they operate.
Functions:
A fundamental building block used across programming languages is the "function." Functions are just like what you might've learned about in some of your Math classes. A function is a pre-defined set of steps that takes an input and transforms it into a desired output. Python comes pre-equipped with built-in functions that are always available to you for solving common problems found in programming. You can also find more functions from modules and packages (more on this in the next section!) or you can create your own.
Classes:
A similar, yet different, building bock of the language is the "class." Classes serve as the essence of the object-oriented programming style in Python. Everything is treated as an "object" (i.e., A function, integer, list, etc.), every object has a "type," and every object type is created through the use of classes. A helpful distinction between functions and classes is that functions do specific things while classes are specific things.
An object that belongs to a class is called an "instance" of that class. For example, the list data structure (see more on data structures below) is a class in Python. When we create a list, we have an instance of the list class.
Classes often have what are called "methods" built into them, which are functions that allow us to do things with instances of the class. For example, if I am working with an instance of the list class, I can use the method list.append() to add more items to that list!
Data Types & Structures:
We use the above building blocks to work with and manipulate different data types and structures. Data types are common across programming languages and are simply forms of data like text (e.g., strings), numeric values (e.g., integers, floats, and complex numbers), and boolean values (e.g., true or false). Along with these data types, data structures are templates of how we can organize data in useful ways.
Python comes with powerful data structures that cover ~80% of what you'll need for programming. The other 20% of use-cases can be met through data structures found in other Python modules/packages, such as the Pandas "dataframe." Included data structures are as follows:
List - An ordered, mutable (e.g., can change) collection of data of any type that are contained within square brackets - [ ]. Lists are heterogeneous in Python, meaning they can contain different data types within the same list, such as numbers and strings. You can change a list at any time, including at runtime, by adding, removing, or changing the included objects. You can have a list of lists!
Dictionary - An unordered set of key-value pairs that are contained within curly braces - { } . This data structure is almost self-explanatory since each of us have had to use a dictionary when we needed to look up a definition of an English word! The term "dictionary" might also be referred to by another name in other programming languages, such as associative array, map, symbol table, or hash.
Tuple - An ordered, immutable (e.g., cannot change) collection of objects that are contained within parentheses - ( ). The tuple is very similar to the list, except for the fact that once you have a tuple you can't change it. You might wonder, "What use is a list that cannot change?" Well, it turns out that there are plenty of use cases that you'll discover as you use Python more and more.
Set - An unordered group of unique objects. It is a handy data structure for remembering a collection of related objects while ensuring none of the objects are duplicated. Sets come with the added bonus in that it's easy to perform common data operations, like unions, intersections, and differences.
Modules & Packages:
The last important building blocks that I want to mention are "modules" and "packages," which will lead us into our next section! A module is a collection of related functions stored in a .py file. You can source that file into your various Python projects so that you can re-use those functions, as needed. A package is very similar, but the difference is that a module is a singular .py file, while a package is a collection of modules.
If you found any of my content helpful, please consider donating
using one of the following options — Anything is appreciated!