Python, in my view at least, is an extraordinarily approachable language because:
- The syntax is extremely readable, descriptive and easy to follow
- It's interpreted, meaning you don't have to go through the bother of compiling your code every time you want to run it.
- The community and documentation is phenomenal.
It didn't take too long for me to start writing code that just about did what I wanted it to do. In the main, I'd have an idea (say, to write some code to extract information from a group of text documents), sketch out roughly how I thought the code should be structured (e.g. do this, then do that, then do the other), and hunt through StackOverflow in search of examples of how to accomplish each step.
I'm now at the point where I'm able to do 80% of the code for any particular project without having to resort to StackOverflow and I'm more confident in starting to write my code unassisted.
Looking back, several habits I've started to practise have helped me move my knowledge of Python forward at a faster rate and I think that had I have started to practise these habits a bit sooner, I'd have reached the level I'm at a little quicker.
So, what follows are some general tips that may help you if you're a beginner who wants to make progress with Python a bit faster.
TIP #1: USE PYCHARM CE
For the first six months, I did the vast bulk of my coding in the interpreter at the command line, a la:
> print "hello, world"
This worked really well early on when I was trying trivial examples, because I was immediately seeing the result of the code I was writing. However, as soon as I wanted to produce something with more than 20 lines of code, I got into a horrible mess.
I soon realised that I needed to start using a proper Python IDE and after a bit of digging around, I settled on PyCharm CE. It's free and it's excellent, because:
- You can run your code in the IDE whenever you like, so the instant feedback of the command line interpreter is available at the click of the button, but your code is still sitting there in the editor helping you keep track on what's going on where.
- PyCharm automatically deals with whitespace indents, so you won't run into errors because you've forgotten to indent the first statement in a for loop.
- You can easily flip between the 2.7 and 3.6 interpreter
- The editor provides feedback line by line on errors and style
- The editor provides inline information that helps you see, for example, what arguments a particular function takes
- The editor auto-completes your code
TIP #2: MAKE LIBERAL USE OF THE PRINT FUNCTION
Whether I'm writing my own program from scratch or playing with a complex example, I initially pepper my code with print. It really helps to be able to see what's going on in the code at various stages. So, my advice is as follows: if it moves, print it to the console and take a look.
TIP #3: LOOK AFTER YOUR CODE
You're going to find that as you begin to grow more competent you'll start referring back to your own code to remind yourself how to perform certain types of operation. Your own code, in a sense, will become a valuable source of reference.
Keep your code organised in a way that makes sense to you. I recommend these simple rules to help you keep your code accessible:
- Store each distinct project in its own folder and give the folder a sensible name
- Give your individual Python files sensible names, too
- At the top of your scripts, add a short comment block explaining what the code sets out to achieve (this comes in handy when you're looking back over your older code)
TIP #4: FOCUS ON GETTING THE CODE TO WORK
Personally, I'm of the view that as beginners our first focus should be getting the code to work rather than ensuring it looks beautiful or runs blisteringly fast. Writing optimised, beautiful code comes later - the priority is first to get your code to do what you want it to do.
TIP #5: keep notes
Note-taking forms an important part of our general process for learning anything (whether it's courses on a law degree, learning quadratics in secondary school or learning Python), so take notes as you progress through the fundamentals in Python.
Notes serve at least two really useful functions:
- They reinforce your learning through the process of writing the notes themselves
- They act as an invaluable reference source as you start to accumulate a larger base of knowledge.
Your notes can be as basic or elaborate as you like, but it's a good idea to get a note of the fundamentals, such as the structure of different loops, iterating over files, reading and writing files etc.
TIP #6: DON'T BE AFRAID TO BREAK YOUR CODE
As a beginner, there's nothing like slogging over a coding project and finally seeing it run and work as expected. Once this point has been reached, there may be a strong reluctance to touch the code again for fear of breaking it. This is understandable, but it will prevent you from reviewing your code and making improvements to it.
If you're really worried that you're going to break the code, copy and paste it into a new file and work on it away from the original. Asking how you can make the intention of your code more obvious and finding ways to reflect that in your code is a really important part of the Python learning curve.
TIP #7: IMMERSE YOURSELF IN ERRORS
Errors that cause your code to halt in its tracks are inevitable. I run into at least half a dozen whenever I'm attempting something new or complex. As beginners, error messages can be a bit bamboozling, but it's important that you consciously engage with them and pursue a way to fix them rather than being tempted to abandon the project.
To begin with, errors will look pretty alien. However, one of the things I've come to love about Python is how descriptive its error handling is once you've got used to it. My advice is to make an effort to read the entire error trace, which will tell you exactly where in your code the interpreter is running into problems. If the error doesn't speak for itself, copy and paste it into Google: chances are there will be a StackOverflow question and answer about the error you're hitting.
Persevere with errors and work hard to fix them.
TIP #8: READ DOCUMENTATION
As with any new technical subject, the surrounding literature will initially be difficult to penetrate. However, the more you make a habit of reading documentation, the more sense it will start to make over time.
Python benefits from an excellent community of developers who put an incredible amount of thought into the documentation they produce for their code, take advantage of it.
TIP #9: THINK ABOUT CODING WHEN YOU'RE NOT CODING
A significant amount of the cerebral heavy lifting for a coding project occurs when you're nowhere near your machine. More often that not, an idea forms in my mind when I'm walking over Waterloo Bridge or sitting on a train. When those moments arrive, I start planning the structure of the code in my mind and identifying obstacles and dependencies (am I going to need to restructure or wrangle of a dataset? what packages will I need to do that? what processing do I need to do before I get to the heart of the code? what do I think I'm going to need to learn before I can do what I want to do?).
Allowing ideas to have to chance to marinate before you even start writing the code will get you off to a far better start than blindly jumping straight in.
TIP #10: LISTEN TO TALK PYTHON TO ME
The TalkPythonToMe podcast is fabulous. The podcast's host, Michael Kennedy, has curated what is in all probability the definitive Python podcast and he knows his onions.
There's definitely something about listening to experts talk about a subject you're trying to get a grip on, even if for some or most of the time you have no idea what they're going on about.
There's your starter for ten. Good luck and have fun!