How to Quickly Pick Up Python

This is a quick post highlighting the basic features of Python, highlighting some topics to help experienced developers understand python quickly.

Basic Project/Code Management

In my opinion Python works best on Linux, though people using it on Windows or OSX can also work it just fine. The best practice is to use Python 3 whenever possible, while knowing some things about 2.7 just in case. The biggest difference you’d see right away between 2.7 and 3 is that print() is a function in 3, but a language keyword in 2.7

Pip is your best friend, it’s the package manager for python and is one of the best things about Python. You can install just about anything with pip install <package>. This is often used in conjunction with virtualenv to isolate package environments. A common practice is to create an environment with virtualenv, then install some packages with pip and get started on your project.

Here’s how I start many projects

virtualenv venv creates a virtual environment in a directory named venv (this is what many python devs use as a convention). The environment is entered by sourcing the script in <environment>/bin/activate. When you want to exit it you can use the alias deactivate from within the virtual environment. Within this environment, all packages are installed in this isolated area.

Another convention for a project is to use a requirements.txt file to track libraries. You can get a list of the current environments’ packages with pip freeze. You can pipe the output of this command to a requirements.txt file so that another machine or user can pull down dependencies with pip install -r requirements.txt.

For managing python code in a git repo, you’ll want to add *.pyc and venv/ to your .gitignore to ignore cached python code files and your virtual environment.

Learning the Syntax and Dabbling

As you probably already know, Python is interpreted and spacing is part of the syntax. Rather than using curly brackets to contain block scope, Python uses levels of indentation. Tabs are permitted but most Python programmers will use 4 spaces for tabs. Python has a culture of appreciation for tidy code, pep8 can do some basic format nit-picking for you.

IPython is a handy tool for playing with basic syntax and exploring code and libraries.

The basic data types you’ll mess with in python are int, float, bool, str, object, list and dict.  Nearly everything in Python is implemented by a PyObject, and Python has a rich set of OO features that are mostly provided by that underlying object.

Dabble through the official Python Tutorial to practice, it’s a relatively quick but thorough avenue to learn the basic syntax, control flow, how to define functions and classes, and it explores the included standard library.

Getting Things Done

Here are some resources for specific topics from the Python Tutorial and Manual:

Beyond the included libraries, there are many other incredibly useful libraries, this is certainly not close to a comprehensive list but these libraries are tried and true:

Hopefully this can serve as a quick reference for those interested in adding Python to their repertoire. If you have any feedback or comments please drop a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *