Pragmatic Python I

Thinking Like a Programmer

Shuvo Saha
intelligentmachines
7 min readAug 31, 2020

--

What makes this tutorial different?

“I’m gonna need you to extract the courses taken and the corresponding semester grade point average. Oh and put it on a fancy chart,” my professor said as he emailed me 50 Excel files containing student information across multiple classes. This task usually takes business students like me about a week thanks to the extensive data cleaning required — people mix up letter grade points with numeric ones, course names are written in different ways, etc. It took me about three hours.

This was all due to my knowledge of Tableau (for the fancy charts), Python, and some particularly helpful Python libraries. My philosophy is that anything you learn will be useful regardless of how specialized it is. Programming has helped me countless times outside my current field, not just because I regularly use Python to speed up repetitive stuff in my life but also because understanding how programmers think and the problem-solving skills that are developed with it have helped me immensely in everyday life.

This tutorial aims to teach you programming to help you in your daily life — no matter what you studied before, what field you’re in, and what your future goals are.

This tutorial will not focus on teaching you programming to become a software engineer, a data scientist, or a machine learning practitioner and it won’t help you earn tons of money in these fields either. This tutorial aims to teach you programming to help you in your daily life — no matter what field you’re in, what you do in life, and what your future goals are.

The main problem with most tutorials is that they assume you’re going to use X language to get into Y career. Some will bog you down with algorithms and some with complex or niche ways to use X language. In my opinion, the worst tutorials are those that just teach you the syntax (the set of rules that govern whether the statements you type are considered correct) and leave you to figure out everything to yourself.

The joke here is that all programmers spend 80% of their time googling a solution.

The Syllabus

We will talk about programmatic thinking, installing Python, and using it natively on your PC, building multiple programs to help us in daily tasks and some fun projects.

By the end of this series, you’ll have enough knowledge to build your Python programs from scratch that run just like a program you’d download off the internet. You’ll also learn to tolerate my childish humor as I explain programming jokes and make fun of CS students.

I will also explain this extremely overused joke since you’re still learning to code. Python is known for many libraries that just do a lot of stuff very easily. Java is incomprehensible. In C++, it takes many lines of code to do something. UNIX is an OS and there’s a lot of permission issues involved. Assembly is so basic you would need to redefine the English language to make it useful. C has a lot of edge cases. LATEX is a document writing language. Finally, HTML isn’t a programming language.

Programming Concepts

The following sections are about some extremely important programming concepts, some of which are also applicable outside of coding. I’d suggest you keep all of these in your head whenever you’re trying to solve a problem.

Understand the Problem

Everything you do in programming will start with a problem. Focus on the problem. Learn the problem. Learn what’s causing the problem. Take the problem out on a date. Marry the problem. Figure out everything you can before starting on a solution.

To illustrate these concepts, let’s assume that we’re a car manufacturer and our new car model has a critical problem that will require a callback.

The problem seems simple enough, even the solution seems clear. Coming to a rushed conclusion, however, is the worst thing you can do when coding (right next to using spaces over tabs, we’ll get to this). Why would we think about the solution before we’ve analyzed the problem? Right now, even the simplest solution to our problem might be unnecessary, useless, or could even make the problem worse.

Let’s dissect the problem then.

We know that we need to find every person who’s bought the new model and then contact them to return the car so that we can check it. Do we have everyone’s information? We have 100 receipts containing names and mobile numbers along with some other info. Our problem only requires 3 things — their mobile numbers, their names, and the purchased vehicles’ models. So, the solution here seems to be to look up any receipts with the new model and call the owners. Maybe a better solution would be to store these in a database for future reference and ease of use. Let’s ask some people to transfer these to a database.

I’m pretty sure most of you came to this simple conclusion beforehand, but I assure you, there will be times when your instincts are going to be wrong and rushing into the solution is only going to cause you problems further down the line. So, take a step back and analyze the problem.

Great, our data entry operators have transferred the receipts to our database and we now have an excel file containing everyone’s names and purchases. The new car is called the Model B. Simple enough, let’s just filter it on excel.

The dataset (truncated)

There’s a problem here. Either the operators or the salesmen must have mistyped the purchase names. A simple filter won’t work. We need to devise a new solution.

One Step at a Time

Try to break down your problems. When learning to code, the toughest challenge is understanding where to begin. The easiest way to begin is not to start with code, but with the general idea of what you’ll do. You may have heard of the term “pseudocode”. This is somewhere in between actual code and planning (a high-level description of the solution in simple language). Try to write down the steps you’d use to solve this problem by yourself and then compare it with my thought process.

What we need to do is somehow turn all the words resembling Model B into one format. Maybe we could use the capitalized or lowercase form of all the letters. That would help with some problems but when the spelling is incorrect, our filter wouldn’t work. The proposed solution could instead be to find words with a B separated by a space (“ B”) in the purchased vehicle names. Our other two car models don’t have a “ B” in their names.

But you should always account for possible future problems and try to give the most robust solution possible. Although, don’t go overboard with this. A lot of programmers tend to go overboard and account for every possible problem and overdesign their solutions.

Thus, our solution could instead be something even more simple, let’s just find any car model with the letter B in it. As long as any future car models don’t have a B in them, no matter what typo our data entry operators make (unless they completely forget the B and effectively make it impossible to identify the car model), our solution will work. The steps are outlined below:

  1. Load the dataset to view the “Purchase” column that contains the car names.
  2. Turn every word in the “Purchase” column into uppercase.
  3. Find any row that has a “B” in the “Purchase” column of our dataset.

This is pretty simple in Python with the help of a very useful library called Pandas. Pandas can even make step 2 obsolete and ignore the case of the words for us.

Working Python code for the example above:

You don’t really need to understand what all of this is, it’s just to show you how easy it is to do complex stuff in Python.

Python is designed to be like human language; even without any prior knowledge of programming, you can probably guess what these lines do from the code alone. You can also probably guess what the hashtags are for (hint: they’re comments left for humans to read).

Iteration

Accept the fact that you’re new and you have no clue how to code. You will make mistakes and you will slowly learn to solve them. You’ll learn good practices and make efficient programs. Every coder complains about how they can’t write good code, even the developers of Team Fortress 2, a massively multiplayer game built by one of the largest gaming companies in the world.

A source code leak led to some pretty hilarious comments left in by the developers.

Jokes aside, you will most likely mess up with your first attempt at a problem. Here’s what your workflow should look like:

  1. Implement that terrible solution.
  2. See what went wrong.
  3. Retrace your steps and find the problem(s).
  4. Fix the problem and re-run.
  5. Go back to Step 3 if there’s another problem.

Sometimes, you’ll run into a roadblock and have an “unsolvable” problem. In cases like these, you should step away, come back with a fresh mind, and try a different solution. Try something completely different or try to fix only the errors in your code in a different way. There are so many ways to solve problems in Python and most other languages; you should never feel disheartened because you’re a bad coder — you just haven’t arrived at the solution yet. That sounds pretty cool; I’ll make it a quote.

You should never feel disheartened because you’re a bad coder — you just haven’t arrived at the solution yet.

Once you do figure it out and make a good solution, the next time you come across a similar problem in the future, you’ll see that your old solution was terrible. You’ll do better this time because you have more coding and problem-solving knowledge. It’s all about practice, after all.

This concludes the first part of the tutorial. In the next part, we dive into Python. Time to write your first program!

Who invited the PHP developers?
*crowd breaks into laughter. Java developers chuckle nervously*

The joke here is that no one likes Java and PHP developers.

--

--

Shuvo Saha
intelligentmachines

A business grad who spends way too much time on his computer. Currently a Marketing Analyst at Wise!