On Learning Python
Recently I was asked for some good resources for learning Python. I was not aware of anything specific but no doubt there are plenty of good blog posts, video tutorials, puzzles, and books that are available. Unable to recommend any particular resource, other than the official language tutorial and documentation, I thought about the approach I wish I had taken when I first encountered Python. As a reminder to myself when learning a new language, and in case it is helpful to anyone else, I have posted my response.
If I think about what I wish I knew about Python when I started using it, it would be understanding what it means to write 'pythonic' code and understanding the design decisions made in the language. Of course, the language has changed over the many years that it has existed, so what once was the pythonic way to do something may be a little blurred now. Some points on this topic in no particular order...
Guido Van Rossum, the creator of Python and the Benevolent Dictator For Life of the project (until 2018), wanted Python to read like a natural language, such as English. So the readability of code is certainly emphasised.
If you start a Python interpreter type
import thisand hit enter. The Zen of Python will be printed. You can also read it here: https://www.python.org/dev/peps/pep-0020/.
These aren't hard and fast rules but Tim Peters wanted to capture the spirit of writing Python. There's some good advice there for programming in general (and perhaps for life too).
Guido wasn't really a fan of incorporating functional programming paradigms into Python. Functions like
filterdo exist but list comprehensions can go a long way and I personally find them more readable. That said, this point is probably more of a discussion that teams need to have, asking each other "what is our style of programming in Python?" A consistent use of the language across the team beats one particular way.
Guido also didn't adopt lots of Object Oriented patterns, such as interfaces, contracts, private members, etc. So you won't see Java style programming with tonnes of classes, large inheritance chains, interfaces, etc.
The Python Enhancement Proposal (PEP) system is where proposed changes and improvements to the language are made. It can be interesting reading. One of the most well known is PEP8 which is the style guide. Python projects should follow this and it's made very easy with tooling that's available. Black is one such tool.
Python virtual environments are a great way to contain the installation of dependencies of your project, instead of installing them globally in your system. You can create a new Python virtual environment by typing
python3 -m venv envin your project directory (you should have v3 of Python installed on your system). You can then activate this environment by typing
Now when you run
python, the executable that is run is located inside
env/bin/, as opposed to your system Python executable. In this virtual environment you'll also have a copy of the package manager for Python, called
pip. You can install a dependency into this project specific virtual environment by running
pip install <package-name>(e.g.
pip install requests- requests is a http client).
If you no longer want to be in the context of the virtual environment you can type
deactivateand hit enter. This is a pretty brief overview of virtual environments and pip but there's plenty of good documentation about this.
Ok, so I guess that covers (surface level only): spirit of the language, how the language is designed (via the PEPs) and the most common tooling (virtual environments and pip).
As for actual coding...
Be familiar with the collection data structures: list, dict, set, tuple.
Be familiar with comprehensions for each of these (there's list comprehensions, dict comprehensions, and set comprehensions).
Understand the difference between args and kwargs and the use of * and **.
Generators are good to know about. You may not need to write them but they are good to be aware of.
Function decorators - these can be really handy and are great to know about and to be able to use.
Get familiar with typing hints. Typing hints are a fairly new feature in Python and with tooling like mypy you can have your code checked to ensure you aren't mixing types. Being a dynamic language this provides quite a nice layer of protection. That said, in my experience at work, we rarely ran into issues where we were passing unexpected or different types to functions. However, typing hints will become common place so it would be best to be familiar with them and use them. Here is a good document showing the typing hints that are available: https://docs.python.org/3/library/typing.html.
That first example would look like this without typing hints.
def greeting(name): return 'Hello ' + name
As for tutorials, I'm not aware of any but the official language tutorial is very nice: https://docs.python.org/3/tutorial/index.html
If I think of any other tutorials or come across a good resource I'll let you know.
What I would try and do is take an existing program you've written, perhaps a small utility, and try and rewrite it in Python. Or, if you don't have something small, try and rewrite a small part of a larger program. I find this to be a pretty good way to become familiar with a new language. If you do this let me know and I'd be more than happy to read over your Python code and provide feedback.