1 03-IntroPython


Previous: 02-GitLinuxBash.html

1.1 Screencasts

This is the python logo:
03-IntroPython/python.jpg
Do you see the eyes?

1.2 Intro to Python3

https://en.wikipedia.org/wiki/Python_(programming_language)
Python is an open-source community developed programming language that grew in popularity and use organically, because people actually liked it!

1.2.1 Want to copy python code from the internet and preserve scope?

Hint: copying code from the internet can often be cheating, but if you do copy it, at least understand it fully before use!

1.2.1.1 You can, and Python supports brackets and semicolons! See how below:

Appendix of the Python Tutorial...

                        Appendix XXX

        Python Block Delimited Notation Parsing Explained

Python incorporates a sophisticated parser and advanced notation
for recognizing block delimiters from almost any computer
language.  The foreign language notations of C, Ada Pascal, TCL,
and Perl will work in most situations.  The Python parser only
requires two minor modifications to the block notation rules of
the foreign language's grammar rules in order to be Python
compliant.

The first change is the addition of a rule which states that
indentation of code is not simply a stylistic suggestion like it
is in other languages.  It is mandatory in Python.  The use of
indentation is considered good coding practice in all these
languages and Python takes this a step further by making it
required by the language grammar.  In Python the meaning of a code
block which is not properly indent is not defined.  Experienced
Python programmers find this rule to be very helpful in making
everyone's code more readable.  An added benefit is that language
sensitive editors, such as Xemacs, can assist in writing code
since they are able to automate the indentation of code blocks.

The second change to to the grammar rules of foreign languages is
that all symbols used to indicate the beginning or end of a block
must be prefixed with the '#' character.  If you are a former
Pascal or Ada programmer this will change your usual notation to:

        if x: #BEGIN
                x = x + 1
        #END

If you are more familiar with C and C++ then you will be
comfortable with either:

        if x:
        #{
                x = x + 1
        #}

or (if you like this questionable bracket style...):

        if x: #{
                x = x + 1
        #}

or:

        if x:
                x = x + 1

or even:

        if x: x = x + 1

C programmers will be happy to hear that the Python parser will do
the right thing even if curly braces are not included when two
trailing statements are present:

        if x:
                x = x + 1
                y = 3 + x

This last feature will fix a common source of bugs in C, C++, and
Java programs.

Python's parser is also sophisticated enough to recognize mixed
notations, and it will even catch missing beginning or end
delimiters and correct the program for the user.  This allows the
following to be recognized as legal Python:

        if x: #BEGIN
                x = x + 1
        #}

or even:

        if x: #{
                x = x + 1
    #}

Now as you can see from this series of examples, Python has
advanced the state of the art of parser technology and code
recognition capabilities well beyond that of the legacy languages.
It has done this in a manner which carefully balances good coding
style with the need for older programmers to feel comfortable with
look of the language syntax.

The above was actually a deadpan sarcastic joke, but it was quite instructive about the first three things you need to know in python:
1) tabbing
2) spacing
3) comments

Explanation if it wasn’t funny to you yet…
Copying python code from the internet often introduces critical bugs due to tabbing not being preserved and no brackets, while C++ does fine, having explicit brackets.
It’s also a sore point with college students learning to code, who are completing hard programming assignments when versions of those projects exist on the internet…

1.2.2 Why Python?

https://en.wikipedia.org/wiki/Comparison_of_programming_languages#Expressiveness

Language Statements ratio Lines ratio
C 1 1
C++ 2.5 1
Fortran 2 0.8
Java 2.5 1.5
Perl 6 6
Smalltalk 6 6.25
Python 6 6.5

When human time is more valuable than computer time.

This is not always true, for example
* real time systems or stock trading, where you want C, C++, Rust, and/or assembly
* cryptography where you want speed and security, and you may want C or Rust
* low level control for writing drivers, operating systems, etc., in C, asm

03-IntroPython/python_weapon.jpg

03-IntroPython/which.jpg
This is just in fun – in reality, C/C++ are not memory safe, nor conducive to good programming because of bad human factors and an unsafe model of data ownernship and mutability, and Java is not that bad, just kind of mediocre all-around, which can be a good thing in some contexts. Both are legacy, antiquated, and sticking around because they were used so much in the past.

If you had to pick only 4 languages to learn well, then Python (typed), Bash, C/C++, and JavaScript (Typescript) would probably be best, learned in that order.
That being said, picking up new languages is fun!
Don’t stop with python or any of those, keep going afterwards.
You too, can be snooty and judgmental about language preferences AFTER you know 7-10+ well ;)

A poem about python:
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!
-Tim Peters

1.2.3 Style guide

https://www.python.org/dev/peps/pep-0008/
https://docs.python-guide.org/writing/style/

Watch: https://www.youtube.com/watch?v=esZLCuWs_2Y (Uniformity trumps perfection!)

1.2.3.1 Auto-code formatting

#!/bin/bash

sudo dnf install python3-black python3-yapf python3-autopep8
#sudo zypper install python3-black python3-yapf python3-autopep8
#sudo apt install python3-black python3-yapf python3-autopep8
# pip3 install --upgrade black --user

# Do this to automatically have these programs format your code:

# The one I prefer:
black yourcode.py

# Or another option:
python3-autopep8 --in-place yourcode.py

# Or another option:
yapf --in-place yourcode.py

Some articles about the above three python auto-formatters:
* https://blog.frank-mich.com/python-code-formatters-comparison-black-autopep8-and-yapf/
* https://medium.com/3yourmind/auto-formatters-for-python-8925065f9505
* https://www.kevinpeters.net/auto-formatters-for-python

Note: Actually use one of these (I require python3-black)!

+++++++++++++++++++++++++
Cahoot-03.1
https://mst.instructure.com/courses/58101/quizzes/55249

1.3 Basic Python3 tools

Interpreters, IDEs, debuggers

1.3.1 Interpreters

Optional: prettier, heavier, local python terminal
* https://ipython.org/
* A more colorful nice terminal, compared to the standard one.
* It is a bit more overhead, though not much; it does not noticeable impact run-times.
* Some differences to watch out for:
* Some are better than standard cpython:
* it handles imports differently (better),
* Some are worse than standard cpython:
* prints some extra junk characters (worse)
* may mangle argparse help (worse)
* Install
* $ sudo dnf/apt/zypper install python3-ipython
* $ pip3 install --upgrade ipython --user

Optional faster python interpreter
* https://www.pypy.org/
* https://realpython.com/pypy-faster-python/
* $ sudo dnf/apt/zypper install pypy3
* Despite goals, not always compatible with everything (i.e., I’ve seen this break pip3)

1.3.2 IDEs and debuggers

Real local IDEs and debuggers for interactive exploration and prototyping are what you should be using!

1.3.2.1 GUI-based

https://www.spyder-ide.org/
* Good at almost everything. The only things it does not do well are Vim-shortcuts… and speed)
* Very full-featured IDE, awesome debugger (best out there).

Install
* System repos:
* $ sudo dnf/apt/zypper install python3-spyder spyder3

1.3.2.2 Command-line

https://github.com/inducer/pudb
* Nice minimal vi-like command line debugger for easy tracing)
* Super-fast, efficient, focused, and great for tracing code to learn it.

Install
* System repos:
* $ sudo dnf/apt/zypper install python3-pudb

1.3.2.3 Online debuggers and interpreters

Don’t bother using these!
* https://www.onlinegdb.com/online_python_debugger (for the newbies who know how to click but not type… yet)
* https://repl.it/ (like the one above, but it has a nice interactive interpreter and the ability to import Git repos. The project has been innovative and supports a whole host of features, including installing packages.)

1.3.3 Pretty publication of code

For scientists, mathematicians, or physicists, wanting to publis and write up up lab reports, data analysis, papers, keep notebooks, etc., see:
../../Bioinformatics/Content/02-PlatformTools.html

+++++++++++++++++++++++++
Cahoot-03.2
https://mst.instructure.com/courses/58101/quizzes/55250

1.4 Code

To be stepped through in the python3-spyder IDE and/or python3-pudb debugger:
* 03-IntroPython/00_basics.py
* 03-IntroPython/01_variables.py
* 03-IntroPython/02_preview.py
* 03-IntroPython/03_std_io.py
* 03-IntroPython/04_errors.py
In-class: Show these in both command-line and GUI debuggers.

Interactive only

#!/usr/bin/python3
4
_
# _ is the last thing returned to the interpreter, but not saved

+++++++++++++++++++++++++
Cahoot-03.3
https://mst.instructure.com/courses/58101/quizzes/55251

Next: 04-ExpressionsTypes.html