This post was originally published on this site

How do I, someone who just dabbles in Python and uses it for daily data handling tasks, become an expert in Python?

Anytime you learn a new skill, such as learning Python, you might feel anxiety about wether or not you are “ready”. You’re always looking around at this course or that, wondering “Is this any good?” or “Will I learn the right things?”

You want some way to hone your skills and “level up”. In school you might have a math class where your teacher would throw problem sets at you. You had lots of opportunities to work problem after problem, making corrections and getting (hopefully!) better. Now you know the basics of Python. You can even write a few Python programs by yourself. But, where to go from here? How can you take things to the next level?

An Excellent Way to Improve Your Python Skills

If you want to develop real skill in Python, you need to write as many Python programs as you can. You also need to get feedback about how effective your programs are. There’s no sense in writing defective garbage if you want to get better. You need some way to write lots of programs, get feedback, and be pushed to improve.

Luckily, there’s a superb place for this call Exercism.io.

Exercism.io is a web site that “provides countless small wins”. This is brilliant!

When you are working hard to improve, each win is a bit more momentum to push forward. Believe me, as you progress, you will need to be pushed a little harder to get better each time.

Exercism.io gives you a a lot of Python challenges – over 100 – for you to conquer. Each challenge comes with the tests already written. So in addition to learning how to solve problems in Python, you get to see how unit tests are built along the way.  If you want to become an expert Python programmer then you must learn to write tests.

A Walkthrough of the First Challenge

Go grab the Exercism.io command line interface (cli) tool. This will be different depending on which operating system you are using.

Log in to Exercism.io with your Github account then get your Exercism.io API key.

I created a directory called

and then another sub-directory under that called

.
You’ll next fetch the first challenge (in the

exercismpython

directory:


$ exercism fetch python

Not Submitted: 1 problem
python (Hello World) ~exercismpythonhello-world
New: 1 problem

python (Hello World) ~exercismpythonhello-world
unchanged: 0, updated: 0, new: 1


Let’s set the API key so we can submit our answer. This will allow us to track our progress. It will also show others how we solved this challenge. It’s incredibly instructive to see how other people solved their challenges, too.


$ exercism configure –key=

Configuration written to ~.exercism.json

    –key=
    –dir=~exercism
    –host=http://exercism.io
    –api=http://x.exercism.io


If you list the contents of the hello-world directory you’ll see:

hello_world.py
hellow_world_test.py
README.md

It’s tempting to jump right in and start writing the implementation in

hello_world.py

. Let’s set a good habit right now: run the tests first!

The README.md contains interesting information about how to run the tests. You will need to install

, which also means installing a package manager for your local Python distribution (like

). Assuming I’m using Python 2.7, then I would run:


$ python -m pytest hello_world_test.py
============================= test session starts =============================
platform win32 — Python 2.7.0, pytest-3.4.2, py-1.5.2, pluggy-0.6.0
rootdir: ~exercismpythonhello-world, inifile:
collected 1 item
hello_world_test.py F [100%]
================================== FAILURES ===================================

_________________________ HelloWorldTests.test_hello __________________________
self = <hello_world_test.HelloWorldTests testMethod=test_hello>
def test_hello(self):
      > self.assertEqual(hello_world.hello(), ‘Hello, World!’)
E       AssertionError: None != ‘Hello, World!’

hello_world_test.py:10: AssertionError
========================== 1 failed in 0.10 seconds ===========================


That looks ugly but it’s expected. You just ran the tests (pre-written by the challenge author) but since there is no implementation the tests failed. Our next step is to provide that implementation.

Every time we make an update we should run the tests, as above. We’ll know very quickly if we made progress or introduced a bug (a regression).

Here’s an example implementation you can use in

hello_world.py

:

def hello(name=''):
    return 'Hello, World!'

Let’s rerun the tests and see how we did:


$ python -m pytest hello_world_test.py
============================= test session starts =============================
platform win32 — Python 2.7.0, pytest-3.4.2, py-1.5.2, pluggy-0.6.0
rootdir: ~exercismpythonhello-world, inifile:
collected 1 item

hello_world_test.py . [100%]

<========================== 1 passed in 0.04 seconds ===========================


And that’s that. Let’s push our solution back to Exercism.io:


$ exercism submit hello_world.py
Your python solution for hello-world has been submitted.

Programmers generally spend far more time reading code than writing it.
To benefit the most from this exercise, find 3 or more submissions that you can
learn something from, have questions about, or have suggestions for.
Post your thoughts and questions in the comments, and start a discussion.
Consider revising your solution to incorporate what you learn.

Yours and others’ solutions to this problem:
http://exercism.io/tracks/python/exercises/hello-world


I bet you’ll be ready to devour the next one. Good luck!

If you want a more detailed example of writing tests, check out Keep Calm and Add Unit Tests with Python.