Welcome!

Welcome to the wonderful world of Python.

Python was Made in Holland, then enhanced by a global team. BDFL emeritus: Guido van Rossum. The language continues to evolve.

Guido named his language after Monty Python, the BBC comedy troupe that also became popular in America. However, there's no ignoring that a python is a snake, important in Greek mythology.

What you are looking at now is a Jupyter Notebook file transformed into HTML, which in turn instructs your browser in how to render the page.

In [1]:
import sys
sys.version
Out[1]:
'3.7.9 (default, Aug 31 2020, 07:22:35) \n[Clang 10.0.0 ]'

Do you want to run multiple versions of Python on the same computer? There are ways... Python comes with a "virtual environment system" for example. The Standard Library includes venv. The Anaconda distribution of Python includes its own virtual environment solution.

Let's exercise a built-in, something Python comes with, built in, minus any need to use the keyword import.

In [2]:
copyright
Out[2]:
Copyright (c) 2001-2020 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.

Course Framework

The main tools we'll be using:

  • Python
  • an IDE (at least one)
  • Github / Git
  • JupyterLab / Jupyter Notebook

python_course_workflow

Python

PSF Approved

Python was not designed for beginners, but it turned out simple enough to introduce the Object Oriented Paradigm (OOP). Python has functional aspects too, meaning functions outside of classes make perfect sense, unlike in Java.

IDE

Python IDEs

An Interactive Development Environment is about making your life as a coder a lot easier. The interactive shell or REPL, text editor, debugger, linter, spellchecker, all come together in a pleasant dashboard.

VS code

Try a few of them. Some IDEs are language specific, such as Spyder and IDLE, whereas others give you optional plug-ins or add-ons (extensions) for working with multiple languages (vscode is of this type).

Don't feel like you always have to use the same IDE.

Github / Git

Complex Topic

Github Logo

Here's where Youtube or another source of videos might come in handy, to explain the world of Version Control, both in broad brush strokes and up close.

Git arose from the GNU Linux Project. Linus Torvalds needed a way to keep the foundations under Linux, its version control system, collaboratively used from around the world, free and clear of future "right to use" obstructions.

In other words, the version control system used for Linux was too important a dependency to leave to others.

Picture many people working on a project and wanting to take it in various directions, provisionally. You have a main trunk with experimental branches, with the opportunity to merge successful threads back into the main action. That's one pattern for using Git.

The main point of version control is you're able to roll back through time and use snapshots of how the project looked in the past. Drafts get saved. Changes stay undoable, at least in theory.

Heads Up

As of this writing, Github is changing the workflow such that a different session token is needed each time.

JupyterLab

jupyter_window

The Jupyter Project arose from the I-Python Notebook Project, which in turn arose from the IPython Project.

Topics:

Immutability versus Mutability

The tuple of tuples below is read only.

In [3]:
zoo = (("Ape", 4), ("Zebra", 2), ("Penguin", 1))

The tuple of lists below looks like it might be immutable but is it?

In [4]:
L = ([] , [])
In [5]:
L  # is L immutable?
Out[5]:
([], [])
In [6]:
L[1].append("dog")
L  # no!
Out[6]:
([], ['dog'])

Callability

This topic is worth a whole page to itself.

Sorting Sequences

How might you sort zoo tuples by either animal or age?

Here's a way I would not expect a beginner to find right away:

In [7]:
from operator import itemgetter
In [8]:
sorted(zoo, key=itemgetter(1))
Out[8]:
[('Penguin', 1), ('Zebra', 2), ('Ape', 4)]
In [9]:
sorted(zoo, key=itemgetter(0))
Out[9]:
[('Ape', 4), ('Penguin', 1), ('Zebra', 2)]

Randomness

In [10]:
from random import choice, shuffle
In [11]:
zoo
Out[11]:
(('Ape', 4), ('Zebra', 2), ('Penguin', 1))

shuffle mutates its argument, and since zoo is immutable, it's not a legal argument to shuffle. However, we may replace the outer tuple with a mutable list and then apply randomness.

In [12]:
shake_up = list(zoo)
shuffle(shake_up)
shake_up
Out[12]:
[('Zebra', 2), ('Ape', 4), ('Penguin', 1)]
In [13]:
shuffle(shake_up)
shake_up
Out[13]:
[('Ape', 4), ('Zebra', 2), ('Penguin', 1)]
In [14]:
choice(shake_up) # pick an element at random
Out[14]:
('Ape', 4)
In [15]:
def get_token(n=25):
    chars = "abcdefghijklmnopqrstuvwxyz1234567890"
    return "".join([choice(chars) for _ in range(n)])

get_token()
Out[15]:
'7eovprsbdrxod9lfmrls876c2'

Slicing and Dicing Sequences

Sequences (like lists, tuples, ranges) have left-to-right (right-to-left) order, are sortable and reversible.

Mappings (dicts, sets), in contrast, are more general, in that the same content, in arbitrary order, connotes equality.

In [16]:
{"a":"b", "c":"d"} == {"c":"d", "a":"b"}
Out[16]:
True
In [17]:
["a", "b", "c", "d"] == ["c", "b", "a", "d"]
Out[17]:
False
In [18]:
set(["a", "b", "c", "d"]) == set(["c", "b", "a", "d"])
Out[18]:
True

Recent Pythons implement PEP 448, expanding the powers of the single- and double-star when used as prefixing unary packing / unpacking operators.

In [19]:
(*zoo, *zoo)
Out[19]:
(('Ape', 4),
 ('Zebra', 2),
 ('Penguin', 1),
 ('Ape', 4),
 ('Zebra', 2),
 ('Penguin', 1))
In [20]:
a_slice = slice(0,None,2)
(*zoo, *zoo)[a_slice]
Out[20]:
(('Ape', 4), ('Penguin', 1), ('Zebra', 2))
In [21]:
to_ship_tupl = (*zoo, *zoo)[a_slice]
to_ship_dict = dict(to_ship_tupl)
to_ship_dict 
Out[21]:
{'Ape': 4, 'Penguin': 1, 'Zebra': 2}

When a dict cries out to be converted into named arguments, to any callable, the double-star unary operator, prefixed, comes to our aid.

Likewise, as we learn when studying callability, the double-star operator, in front of a parameter name, will match any remaining named arguments to a dict, by that name.

In [22]:
dict(**{"Llama":2}, **to_ship_dict)
Out[22]:
{'Llama': 2, 'Ape': 4, 'Penguin': 1, 'Zebra': 2}

Unicode

As we discussed in our first meetup, Unicode is the successor to ASCII both in the sense of superceding, and in the sense of swallowing and including.

Python libraries will let programmers continue working in pure ASCII, or in any of several codecs whereby plain text is decoded and encoded.

UTF-8 is the most common flavor of Unicode and is Python's default. The format commits varying numbers of bytes to the Unicode codepoints, with information up front (front byte) regarding how many bytes follow (picture a tiny train of one to four cars, car = byte).

In [23]:
import fooding
In [24]:
fooding.foods
Out[24]:
['🍗', '🍩', '🎂', '🍟', '🍞']
In [25]:
fooding.fruits
Out[25]:
['🍇', '🍈', '🍉', '🍊', '🍋', '🍌', '🍍', '🍎', '🍏', '🍐', '🍑', '🍒']

Explorations with Unicode:

The script below is designed to run at the command line, and therefore has to read arguments from the command line.

unicode_fun.py

Proposed project:

Add another language section to the unicode_fun.py menu and to the output.

Additional Resources:

Proposed Exercises

Youtube Gallery (curated)

Curriculum Using Python + Jupyter Notebooks

GeoPython by the Department of Geosciences and Geography at the University of Helsinki

Python Data Science Handbook by Jake VanderPlas

School of Tomorrow by Kirby Urner

For Further Reading

History of Python (my Wiki article)

Generating the FCC computer graphics stuff

Intro to Python Flickr Album used in courses

Answering Questions on Quora (doing my civic duty)

PyCamp: Another Repo for Teens by me

Python Release Schedule (3.9, 3.10, 3.11...)

In the Beginning Was the Command Line by Neal Stephenson, a classic

Revolution OS movie documentary

A Monty Python Story in the New Yorker (1976)

In [ ]: