Why Even I Could Learn Python

Coding has a certain mystique surrounding it. After all, it involves learning a whole new language and grammatical structure – not to mention a new way of thinking. I remember in college one of my GIS courses had a section on Python, but we spent most of the time making flowcharts of how we’d accomplish a task. I remember thinking that it seemed like a huge waste of time and that I just wanted to get to the useful, and foreboding, instruction on how to actually write the code. Years later, I realize just how correct that professor was in having us start with flow charts, though I do wish we’d had more perspective on why. If you are interested in learning to code, but don’t have the time or interest in professional training (for example, you just want to automate the task that’s been taking you 6 hours to complete manually), then this post is for you!

Coming back to flowcharts, I think this is the most important concept you can take from this blog post. You could know all the language and grammar, but if you don’t know what you want to say then what’s the point? I believe the most powerful way to learn how to code is by having a business problem you want to solve. In this scenario, you know what you are starting with and where you want to end up. These are critical components of building the flowchart in your head of how to code your task. If you were in a French-speaking country and wanted to know where the bathroom was, would you try to get fluent in French before asking? Or would you just google “How to ask where the bathroom is in French”?

The same logic applies to coding. If you know where you’re starting and where you’re heading, you can start to google the syntax for everything in between. For example, let’s say that you want have 100 SHP files that you want to add a field to, and that you want to import into a file geodatabase. That’s going to take a while by hand. So how could you automate this task with absolutely no knowledge of how to code? Well, let’s start by understanding two things. Python is the language of choice for GIS and many other applications, which means that if it’s a good candidate language for completing this task because there will be a lot of answers on Google. Next, let’s remember that ArcMap and ArcGIS Pro each have a built-in Python command line. This makes our lives even easier because we don’t have to install a Python executable, nor do we need to worry about the IDE, or environment we will run the code in. Just fire up ArcMap/ArcGIS Pro and open the Python command line!

Now that we’ve identified the language and how we are going to run the language, let’s simply Google “How to use Python to add fields to many SHP files in a directory”. We’ll take someone else’s code and start modifying it with our file paths and folder names – and sooner rather than later you’ll likely find that you’ve got fields added to your SHP files. Next, let’s google “How to import SHP files into a file geodatabase in bulk with Python” (let’s pretend there isn’t an easy way to do this in the GIS GUI). Once again, we’ll take that code, start modifying the variables, and sooner rather than later we will, ourselves, be coders.

I know this might sound overly simple, but it’s meant to encourage you to just start trying! You don’t need formal training to be a coder, and the time it can save you in your every day work is massive. If you are new to coding, here are some recommendations for getting you up and running quickly:

  1. Most importantly, never run code on production data. Copy your data and create a test environment. Better yet, make 2 copies of your data so that when you blow up your test environment, you can easily restore it.
  2. Code interactively! It’s going to make your life a nightmare if you write 100 lines of code, without good error handling, and then go to run it. The default errors that languages tend to give you are confusing and difficult for newbies to troubleshoot. If you code line by line in an interactive terminal, it’s much easier to you to figure out where you went wrong – you’ll know right away which line you went wrong in.
  3. Use other people’s code! I’m not encourage you to steal intellectual property – but there are bazillions of answers and samples of code out there on websites such as Stack Overflow and Github that can help you get started in answering your question.
  4. Be choosy with the training you pursue. Is a 1 month Python book camp going to be beneficial to you, if you’re just looking to automate GIS workflows? Sometimes too much information is a bad thing – it might just cause you to zone out. Look for training that specifically looks to address the problem you want to solve.
  5. If you are a GIS person, I can’t recommend these videos enough: https://www.youtube.com/c/DavidVerbyla/videos

Anyway. Maybe these tips are useful for you, or maybe not. But hey – if I can write some code, you can DEFINITELY write some code.

Let me know what you think!