Introduction

00_introduction

Introduction to Python

This guide is strictly about Python3. It is specifically for use with Python versions: 3.6, 3.7 and 3.8. At the time of this writing 3.8.0 is the newest version of Python. The author of this guide typically refers to Python3 as Python. This is not done to confuse you, in the mind of the author, Python is Python3.

Once you have installed Python3 – you may have more than one version of Python on your system. That’s fine, DO NOT remove Python2, especially if you’re on a Macintosh or Linux system. Python2 may be required by your operating system. In addition, it is not recommended to modify, update or extend your system’s Python2 library in any way. Your system can take care of that itself, and it is dangeous to do so yourself. Another option to avoid this problem is to develop Python in a virtual environment. As with most things, there are many strategies – with pros and cons for each.

Python Axioms & Terminology

The Zen of Python

In [0]:
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!

Pythonista

The Jedi Masters of Python. There is simply no better way to put it. By the end of this guide I hope you will embrace the idea of being a Pythonista, and always use Python for good.

Pythonic

Beautiful Python. The belief that Python can be beautiful is more than just the personal opinion of this author. It’s a widespread ideal within the greater Python community. An aspiring Pythonista may strive for aesthetically pleasing code. A true Pythonista will not need to try.

Python is a Consenting Adult Language

This may sound odd at first. It means that Python trusts the Pythonista implicitly. Not many programming languages are like this, and it can sometimes cause trouble. If you would like to re-define the built-in string class, Python will gladly let you – with a smile.

All of these are ugly in any scope, and dangerous at global scope!

# Don't do this!
str = "some string"
int = 42
list = [1, 2, 3]
tuple = (4, 5, 6)
...

The code above may seem innocent enough, but it causes several delayed consequences that break Python. Much of Python is implemented in Python… let that sink in. At the global scope the name str is a reference to the string object – if you overwrite it, Python will be fine with it… that is, until you need to use that class later in your code. The expression str(42) should produce the string “42” from the int 42, but it can’t now that the name str was overwritten. This general idea is true for all built-in names at the global scope. Don’t overwrite Python’s guts by accident, learn the built-in names and respect them.

The following code example is totally insane, it can only be the work of a devious Sith Lord. However, one can’t help but appreciate the femme fatale beauty of it…

# Don't do this!
int, str = str, int

Everything in Python is an Object

While this is true, Python is not strictly an object oriented language. Python is a multi-paradigm language and supports many programming styles.

Everything in Python is a Reference

This means – everything in Python is a Reference to an Object. This will be fundamentally important on the path to becoming a true Pythonista.

Python is Dynamically Typed

This refers to the ability in Python, to reassign a variable to different value types throughout the lifetime of the variable with no warnings, errors or special syntax. Python is Python because Python is dynamic. This is a feature, not a bug. However, the resulting code is not concidered Pythonic.

In [0]:
my_var = 42
print(my_var)

my_var = "When is a string nothing like a thread?"
print(my_var)

my_var = 4 + 2j
print(my_var)
42
When is a string nothing like a thread?
(4+2j)

While reusing a variable is technically fine, the example above does so in an ugly way. The objects that my_var points to are totally unrelated and nothing is gained by assigning them to the same variable. If it actually made logical scense to reuse the variable, then it wouldn’t be so ugly.