3 Useful Projects to learn Python Classes
I have recently wanted to learn more about Python classes, which is a concept at the core of object-oriented programming languages. While reading article tutorials gave me somewhat of a high-level view of what classes are and how they work, I could tell I did not really retain the content of the tutorials I was going through.
Definitions and examples seemed too abstract, and I could not picture a need for using my own classes while using a language that already has so many ready-made packages and libraries that tackle 99% of your common needs as a programmer which uses Python to work with data.
I then decided to look for a project-based approach to learning, and I stumbled onto the FreeCodeCamp Scientific Computing with Python Projects, which I completed this year. 3 out of these 5 projects are class-based, and thus looked perfect to tackle my learning goals.
What follows is then a brief overview of what to expect if you will decide to jump onto a project-based approach to learning classes via these 3 projects. It worked wonders for me and hopefully it will be useful for you as well.
The focus of the challenges(to be honest, they are more self-contained than fully fledged projects, and thus the name “challenge” feels more appropriate here) is more around building self-contained pieces of functionality (i.e. Python objects that display a certain set of features and that allow for either numerical computation or visual representation) rather than building a massive workflow. In this regard, the setup is not too dissimilar from that of classic coding challenges, although these are probably a bit longer.
Each challenge is tested against various unit tests which challenge your code under different conditions.
This is not massively different from the work a Python programmer will find himself/herself doing once on a project, where you will be potentially required to define your own custom objects that will be tailored to the problem at hand, maybe because other open-source packages just do not fit the bill in terms of getting you the precise result you want
The projects are based in “pure” Python, with almost no external libraries needed. Project number 2 is the only one if which you will make use of the random and copy packages (which are recommended, not mandatory).
1. Budget App
Image for post
Budget Icon designed by Freepik
Goal: “Create a Budget class that can instantiate objects based on different budget categories like food, clothing, and entertainment. These objects should allow for depositing and withdrawing funds from each category, as well computing category balances and transferring balance amounts between categories”
Considerations: this is a very interesting project as it allows not only to comprehend how a class is initialized and used, but also represented and used as input to other functions. You will learn how to add methods to your classes and print them in a way that allows complex representation of your class object at different points in the program. As a bonus, you will define a function that computes how much money you are spending across class categories as a % of your total expenses, something that can be very useful for the money-savvy programmers among you.
Approach: define the purpose and flexibility of a class object; build its class methods using a modular approach and develop an understanding for how different instances of the same class can interact.
Key concepts: Class initialization, instance methods and instance representation. Defining and using functions that take class instances as input parameters
2. Polygon Area Calculator
Image for post
Geometry icon designed by Freepik
Goal: “Create class and sub-class objects which represent different geometrical shapes, such as Rectangles and Squares”
Considerations: this is the perfect project to understand how classes can develop relationships in the form of sharing class methods. Such relationships will be conveyed through the use of geometry. The main takeaway is the understanding that Python is a flexible language is which you can define your own custom objects, whose functionality and features (class methods) can be shared with other similar objects.
Approach: represent geometry shapes as classes and give them the ability to compute routine geometry calculations in the forms of class methods. Leverage class parent/child relationships to extend class functionality.
Key concepts: Class initialization, instance methods and instance representation. Class inheritance.
3. Probability Calculator
Image for post
Lottery Icon designed by Flat Icons
Goal: “Create a lottery ball, or Hat, that takes a variable number of arguments that specify the number of balls of each color that are in the hat. Give the object the ability to pick a random number of balls from the hat, which will then be used to compute the probability of picking a certain distribution of balls over a large number of experiments”
Considerations: this project resides at the intersection of probability and class creation. Using a Class object as an input to model a probabilistic output should allow for neat understanding of how python classes can be used iteratively to estimate event probabilities. The project also introduces the idea of creating classes ready to handle a dynamic number of input arguments.
Approach: create class object and use it to set up a probabilistic experiment in which the class object will be continuously utilized to fetch and record the result of a lottery pick.
Key concepts: Class initialization with variable number of arguments, instance methods and instance representation. Using classes to compute probabilistic experiments. Class clones.
Summary and next steps
These projects got me super excited about working with Python classes since they made me understand the true power and flexibility of defining your own objects and behavioral features.
Getting hands-on exposure to fundamental class concepts will allow you to develop a solid intuition of these concepts way more than passively reading through a few tutorial articles.
If you enjoyed FreeCodeCamp projects, I also recommend their Data Analysis with Python challenges, which I also wrote about in the below article:
How I finished FreeCodeCamp Data Analysis Python Projects in 2 days
An analyst’s overview of key concepts and what to expect
Thanks for reading!