How to Teach Yourself to Solve Problems Using Code
7-Minute Read Covering the Dos and Don'ts of Learning to Code
Hello world!
Over the last few weeks, people have asked me how I’ve learned to code. I started my career as a consultant with no coding knowledge. I’m now singlehandedly building AI-powered web applications. How did I get here? I previously posted about the attitude that helped me learn to code in “How to Learn to Build a Software Company,” but I never covered the specifics. You’ll leave this article with my specific dos and don’ts of learning to code.
Subscribe below to continue learning from my experiences as a founder through weekly <10-minute reads.
Three Things that Didn’t Teach Me Python.
Here are 3 things I never did when I was learning Python (the coding language I typically use). You may have preconceived notions about how you are “supposed to learn” to code, and my goal here is to break them. I learned, and I did it my way:
I didn’t take a Python course.
Reason: I’d get bored. I wanted to build something, and Python was a tool I needed. If you wanted to learn to build a shed, you wouldn’t spend 3-months studying how to build chairs. Sure, some of the skills would carry over. But, I’d much rather only learn the skills needed to achieve my desired outcome. I wanted to build something too specific for a course to help me with (an AI video game coach), so I didn’t take a course.
Advice: If you can find a course that directly teaches you how to build what you want to build, then great! Otherwise, don’t waste time learning skills you won’t need.
I didn’t buy a Python textbook.
Reason: I’d get bored. Similar to a course, a textbook would have a lot of useful information in it but, again, reading a textbook end-to-end wasn’t on the critical path to building my AI video game coach.
Advice: You can use textbooks as a reference, or if your desired outcome is “I want really deep knowledge about a specific area of Python (e.g., data science).” Otherwise, don’t start your learning journey by reading a textbook end-to-end.
I didn’t work on simple, abstract, introductory projects.
Reason: I already had an idea I wanted to build. If I hadn’t had an idea, building a “blog website” may have been a great way to spark one, but, again, this wasn’t on the critical path to building what I wanted.
Advice: Pick something you want to build and build it. If you don’t know what you want to build, you can find inspiration in these simple introductory projects.
If you set out to learn Python, these are probably the strategies you’d follow. But, the idea of following any of these three strategies was incredibly boring for me. Instead, I figured out why I wanted to learn Python and identified the shortest path to get there.
Four Things I Learned as I Built.
Here are four practical things you’ll need to know as you get started with any Python project. If you are learning another language consider how these principles would apply.
#1 Downloading Python
Initially, your computer doesn't know how to understand Python applications. You need to download Python from the internet to teach it. You can do that here.
During installation, look for the “Add Python X.X to PATH” checkbox and make sure you check it. The PATH is where your computer looks for certain commands. If you don’t do this, later on, when you tell your computer to use “python,” your computer won’t be able to find the installed software.
#2 Downloading Visual Studio Code
Your computer can now speak Python. But what program do you use to use to write the Python code? An Integrated Development Environment (“IDE”) is the software you use to write code. You use Microsoft Word to write an essay; you use an IDE to write a software program. There are a bunch of IDEs, I use Visual Studio Code. It’s free to download here. Once you have an IDE, you can point it to a folder on your computer where you want the code to live.
#3 Use Virtual Environments
You don’t write all of your code from scratch. In Python, you use other people’s code as the building blocks for what you want to build. These libraries (or “packages”) of code are easily downloadable through a Python function called “pip.” But where do these packages get installed? A “virtual environment” is where you install these packages of borrowed code. In your IDE, you’ll have a terminal window at the bottom to run commands. You’ll set up the virtual environment from there.
To set up a virtual environment in Python, run:
python -m venv env
Here’s a quick explanation:
“python” - remember earlier when I said to click the “Add Python X.X to PATH” box. Here’s where you’ll run into an issue if you didn’t. The first keyword in this command is saying, “Hey, computer! Use this ‘python’ software to process the rest of my command.”
“-m” - I didn’t know what this did until now. It was never important, and I’m not going to spend time explaining it.
“venv” - This is the keyword that tells your computer you’re setting up a virtual environment.
“env” - This is the name of the environment you’re setting up (it ends up as a folder on your computer where the packages will be saved).
To activate your virtual environment, run the following:
Mac:
source env/bin/activate
Windows:
.\env\Scripts\activate
Now your computer knows what virtual environment to use, and you can install Python packages to kick-start your app. For example, you could now run:
pip install pandas
This will install a package in your virtual environment to help you quickly work with and manipulate data sets. The packages you install will depend on what you’re building. We’ll talk about finding the right packages a bit more in tip #4.
The last callout I’ll make is that you can easily save a snapshot of your virtual environment by running:
pip freeze > requirements.txt
This function outputs a file with all the packages you have installed for this virtual environment. It makes it easy to set up an identical virtual environment on a different computer or share your Python environment with another developer.1
#4 Start Building
You’ve got all of the components you need. Now, you need to get started on whatever you want to build. Pick a problem you want to solve and follow the shortest path to get you to a solution.
Nowadays, the shortest path is often to ask an AI assistant like ChatGPT to help you get started with a Python script (e.g., “Write a Python script that outputs every prime number between 1 and 1000”). It’ll give you some starter code to read and try to understand. It will even tell you which Python packages to install to make the code work. You can ask why it made certain decisions and how the script works.
Add the code to a file (e.g., find_primes.py) through your IDE, and run it with:
python find_primes.py
You can ask ChatGPT for help or Google it if you hit errors. As you want to do more and more, ask for guidance and/or take what you’ve learned and start writing code yourself. You’ll want to split up your project across multiple files and folders for more complicated projects. You can even ask ChatGPT for the best way to do this.
Pick a Project and Go!
I started my career as a consultant with no coding knowledge. I’m now singlehandedly building AI-powered web applications. How did I get here?
The best way to learn to code is to pick a project and go. Decide what you want to achieve, and follow the critical path. Avoid textbooks, courses, and abstract introductory projects unless they're on your critical path. Instead, learn as you go. Set up your coding environment and use AI to achieve your desired outcome quickly.
#code #coding #programming #softwareengineering #learning
Thanks for reading! Questions or ideas for topics? Email me.
When setting up the new environment, you’d run “pip install -r requirements.txt” which would install all of the packages from your original environment.