My latest experience in learning Python has centered around classes, which is a concept that is really at the heart of what makes Python an object-oriented programming language. It’s been challenging, and I’m just barely scratching the surface, but it has been a lot of fun so far. I can tell that this concept is core to defining whole categories of objects and the actions and behavior that your code will take towards those categories. Classes allow you to apply this logic towards objects more consistently, at least that’s the theory. Below is a super simple example I’ve taken from Eric Matthes’ Python Crash Course:
What this example is showing is that we’re able to make an attempt at modeling a dog, creating the behavior and actions around any dog (it could be millions of dogs, but we only will need to do this once), and then proving this logic by applying various methods to different dogs.
Lately, I’ve been learning how to take advantage of dictionaries in Python to store and organize information. Dictionaries have a lot of different purposes in Python, but as it applies to data visualization, it can be extremely helpful. Simply put, dictionaries allow us to store almost limitless amounts of information about whatever we want, and then call it when we’re writing our programs or creating visualizations. Doing this allows us to model real-world information more accurately, whether it is storing information about a person’s information, statistics about sports, econometric data, and so on. Another beautiful thing about dictionaries is that they’re easy to manipulate. Rather than using variables for everything, we can sort, loop, or write functions using dictionary values. We can even create other lists of dictionaries. The example below shows how we can create a dictionary where the values are lists, and then cycle through the lists to create messages about each person’s favorite language. Yes, I realize it’s a bit weird just to post a screenshot of my code. However, it seems like a pretty intense process to show an iPython notebook in WordPress. (Edit: Updated the post to show iPython notebook)
Another example of using dictionaries is the basketball data I analyzed for an introductory course I took. The course was called “Python A-Z: Python for Data Science” and I found it through Udemy. We stored each player’s statistics for the past five years into dictionaries and then were able to call upon them way further down the script to run some analysis. Below you’ll see just one of the visualizations I was able to create using dictionaries for organizing the data, NumPy for the statistical functions we needed to run, and Matplotlib for the visualization part. Watch out for another post on Matplotlib and Seaborn shortly.