Let me start off by asking two questions: Why should we learn to code? Why use Python?
If you type either question in Google, you’ll receive a long list of differentiating, yet persuasive answers. Knowing how to code is an empowering feeling and skill. From my perspective as a data analyst, coding helps me get the data I need, format it and automate the process. You can do a gazillion things with programming. Your imagination is the only limit.
To answer the second question, I chose Python because it’s very easy to learn. It has a big community with plenty of courses and online help. According to this article, Python is the fastest growing programming language and is in top six most used languages as of 2017, according to TIOBE Index.
If you’re a beginner or don’t have much experience in Python but are keen on learning it fast, then hopefully this blog post will help you on your journey. First, I’ll explain how I learned Python and what resources I found useful. Then I’ll describe how I wrote my first meaningful Python script and share some recommended practises that helped me along the way.
How I Learned Python
If you are a beginner, the best and most interactive way to learn is to sign up for an online course. The course I took with Dr. Chuck three years ago was both easy and fun. However, I recommend it only if you don’t have any prior experience working with Python. Otherwise, I would say you should find a more advanced course.
There are also plenty of tutorials like this one that can bring you up to speed very fast. Here is another good overview of beginner’s guides. Some of them even allow you to explore Python in your browser.
After you play around with Python on your own or in an online beginner’s course, I highly recommend writing small scripts to strengthen your knowledge. To stay motivated, choose a program that is in some way useful to you, so you can gain insight while figuring out Python. Below are a few ways you can begin to build your Python expertise:
- Google and StackOverflow
- Python documentation
- Ask an experienced friend
I also suggest using one of the Integrated Development Environments. IDE usually has plenty of useful features such as autocomplete, debugger and refactoring tools. Some will even check your Python code for little mistakes and encourage best practices through warnings. IDE will help you to find bugs and develop code faster. However, you do have to invest a bit of time to learn the features of your chosen IDE. I use PyCharm and am very happy with it. Here is an overview of other available IDEs.
Short note about Python interpret: without it you won’t be able to use your Python program. But the good news for Unix and Mac OS users is it’s prebuilt in the operating system! You just need to run your Terminal and then type Python. At the time of writing this, there are two versions of Python available: 2.7 and 3.6. Version 2.7 will be supported only until 2020, so you’d be better off with version 3.X.
Now, we are all set up to write something useful!
How I Wrote My First Useful Python Script
The task was to get historical weather data analysed in Tableau. You can find the entire script on github. First, I explored several REST APIs for obtaining weather data. In the end, I went for DarkSky, which has good documentation and is free to use. Then I dived into the coding. To make my life easier, I followed two programming principles:
First, create a very basic version end-to-end. It is much less frustrating than trying to build a super-duper version from scratch. A big plus is that you will have something you can use very fast. Then iterate and add more complex functionality one by one. For example, in the first iteration, my program fetched weather data from DarkSky for one day from one location and saved it into a CSV file readable by Tableau.
Next, I added support for querying multiple days. I extended the solution to first read the resulting CSV file and only fetched the data for missing days that were not in the results yet. Finally, I added support for multiple locations.
Second, decompose large problems to smaller ones by introducing functions. Small, cohesive functions are easy to understand, test and debug. For example, I created little functions for reading and writing CSV files I can reuse in other programs.
Last, but probably the most important thing to keep in mind, is practice makes perfect. Start small, be patient and practice. Happy coding!