1.1. Start Running Python: Execution and Importation

The best learning is always from doing. As a starting point, you know how to execute Python programs. Several basic concepts will be introduced in this chapter, but the very first thing is to prepare a runtime.

1.1.1. Running Python

On Debian/Ubuntu.

1.1.2. Interactive Interpreter

Invoke and use the interactive environment for simple tasks.

1.1.3. Python Script

Write Python code in a file.

Use shebang and set the executable bit.

1.1.4. Pythonic Code and PEP8

The Zen of Python (Python Enhancement Proposal (PEP) 20):

$ python -c 'import this'
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

When writing a Python program, it is important to write it “pythonically”. “Pythonic” is quite a vaguely defined adjective, and it roughly means “writing a program in the way that an experienced Python programmer feels comfortable”. Therefore, pythonicity is not something can be taught. Instead, writing pythonic programs needs deliberate reading and mimicking good code, which in fact is the sure way to learn programming. You will gradually understand the Zen of Python mentioned above, and gain the productivity enabled by pythonic programming.

Python programming is centered around “the one way to do it”. Python encourages using a best practice to solve a problem in code. Although one can always find many approaches to program, using more than one way to do it is not considered a friendly coding style for code readers. The one-way spirit indeed takes away a certain amount of the diversity of our code, but give us in return readability, maintainability, and the eventual productivity.

Perhaps using Python-specific constructs could be the easiest way to demonstrate pythonicity. If you are familiar with C and come to learn Python, you tend to write code like:

lst = [1, 3, 5, 2]
literals = []
i = 0
while i < len(lst):
    i += 1

Because you know for has a different semantic in Python than in C, you chose to use while. Perfectly valid but not pythonic. You can then improve it by using for with the sequence lst:

lst = [1, 3, 5, 2]
literals = []
for it in lst:

A bit better but still unpythonic. This can change by using a list comprehension:

lst = [1, 3, 5, 2]
literals = [str(it) for it in literals]

Now the five lines of code at the beginning becomes a one-liner. Although you might not know what’s a list comprehension, you could still guess from the expression that the resulting literals is a list and the code involves something about looping (because of the for). It is now pythonic.

But note, not all code using Python-specific constructs is pythonic. Although the following version is even shorter than the previous one, it’s not really more readable than the longer one:

literals = [str(it) for it in [1, 3, 5, 2]]

Things can be trickier if there’re nested list comprehensions:

literals = [str(it) for it in [val+10 for val in [1, 3, 5, 2]]]

It’s OK, but split it into two lines isn’t harmful either. Remember, pythonicity is vaguely defined. Finding a quick way to “a good Python coding style” is usually an effort of vain. Relying on the Zen of Python and constant practicing is more rewarding.