If you have not read the first blog on the why, how, and hope of this series, check out the first 👉🏾 here.
Python 2 vs Python 3 🐍: Which one?!?!
I think the biggest reason why I have chosen Python 3 over Python 2 is that starting this year, there will be no maintaining of Python 2 and almost no more bug fixes (see more here on Python’s official documentation). Thus, I’ve decided to go ahead and code in Python 3 for the remainder of this series. Now, with that comes an interesting question: what’s changed between versions? What I’ve seen on the internet and many of blog posts, it’s a few things that come to mind:
While this is not at all an exhaustive list, I hope this gives an idea of somethings that have changed from Python 2 to Python 3. Ultimately, if you are working on a legacy system of some sort, you might have to switch to Python 2 so the best case scenario is to be comfortable with both versions 😃.
What’s a Class?
Seeing as the blog posts will focus on the understanding and implementation of data structures and algorithms, I think it’s important to explain a fundamental part of any object-oriented programming language, and that’s classes. Rather than give you a definition first, it may be easier to think about a picture. Let’s think about your standard calculator.
A calculator has many different functions that a person has access to. A person can come to the calculator with their given numbers and add, subtract, multiply, divide and (depending on if it’s a fancy calculator) also plot graphs, utilize charts, and much more. A class in python is no different than a calculator. You can think of a class as an interface with defined functionality and attributes (the calculator’s job is not to tell you the weather 👉🏾 it’s to crunch numbers) to be accessed through an object (your finger would be the object, in this case, accessing the functionality of the calculator, providing the input in numbers and operations and the calculator gives you the desired output). This can take you down a rabbit hole of examples when you think it through (a car can be considered a class, the remote you use for you tv can be considered a class, the possibilities and internet are endless with examples 😂). We’ll choose to stay above ground and see what this simple example looks like in code 😃
Building a Calculator From Scratch
The def __init__ is considered to initialize the class¹. Most, if not all classes have this at the top of the class file. You might be wondering about the self in the argument list. This took me a while to figure out when first learning object-oriented programming but in a nutshell, the self is “used to refer to the object being created at the moment” of initialization¹. Num1 and Num2 are both considered as attributes and more so represent how you can extend your class (you don’t have to use the same two numbers every single time you use your calculator).
The add, subtract, multiply, and divide functions above would be considered the actual “buttons” that a user of the calculator would physically press for the desired usage. These functions work in the exact same way! Once you create your object, you can then access the class functions via the object as you can see below 👇🏾
You Did It!
If this example makes sense and you are solid on the information, you did it! Pat yourself on the back 😃. Being comfortable with classes moving through the series is very important as the data structures and algorithms that we will be invoking are not native to Python (or really any other language), therefore they will need to be built before implemented.
Seeing as the only way to get better is through practice, I will be posting some programming problems sometime next week via online flashcards that I will share publicly so you can practice your programming anywhere.
Until next time! 👋🏾 Tweet me your thoughts! 😃
 Shovic, John C, and Alan Simpson. “Doing Python with Class.” Python All-In-One For Dummies, John Wiley & Sons, Inc., 2019, pp. 213–220.