Lesson 1 - Introduction

Avram Lubkin
Rockhopper Technologies

Why Python?

Why Python?

  • Python is consistently ranked among the top programming languages
    • Currently second on GitHub (active projects) behind JavaScript
      • Overtook Java in 2017
    • Heavily used in data science and scientific computing
  • Extensive ecosystem
    • PyPI contains over 151,000 packages (September 2018)
  • Simple
    • Or as complicated as you need
  • Integrates with C libraries
  • In demand
    • 67% increase in Python jobs on Indeed.com between 2012 and 2016

History

  • Created by Guido van Rossum
    • From the Netherlands
    • At Dropbox since December 2012
    • Formally at Google, NIST, CNRI
    • Resigned as BDFL (Benevolent Dictator For Life) in July 2018
  • Started in December 1989
    • Guido was trying to stay busy while his office was closed for Christmas

    • Named after Monty Python’s Flying Circus
      • Python documentation has a lot of references to Monty Python.
        • “eggs” and “spam” is commonly used instead of “foo” and “bar”

        • Example from Input and Output in the official Python tutorial

          >>> print('We are the {} who say "{}!"'.format('knights', 'Ni'))
          We are the knights who say "Ni!"
          

Interpreters & Compilers

  • CPython
    • Most popular
    • Reference Implementation
  • PyPy
    • Fast version of Python written in RPython
      • statically-typed subset of CPython
  • Jython
    • Python that runs on a JVM
  • IronPython
    • Python for .NET
  • Cython
    • Compiles Python code to C
    • Not to be confused with CPython
  • Several others

Versions

  • The current version is 3.7 (3.8 scheduled for October 2019)
    • Python 3 is not backwards compatible with Python 2
    • Compatible code can be written, easier with the six module
  • Python 2.7 was released in July 2010
    • Slated for retirement in 2020
    • Only receives bugfixes and backports to assist in transition to Python 3
    • A lot of code is still Python 2, so we’ll see it for a long time
    • Tauthon is a project to create an unofficial Python 2.8

Versions in Enterprise Linux

Coding Style

  • PEP 8 is the official style Guide
    • Covers naming, indentation, spacing, etc
    • Some areas are up for interpretation
    • The goal is consistency and readability, not strict adherence
  • Summary
    • Indent with 4 spaces, no tabs
    • Constants in UPPERCASE
    • Class names in CapWords
    • Almost everything else in lowercase_with_underscores

Tools - Text Editors

Tools - IDEs

  • VSCode with Python extension
    • Written in Typescript
    • By Microsoft (Open source)
  • Spyder
    • Written in Python
    • Targeted at the scientific community
  • PyCharm
    • Written in Java
    • Commercial and community versions
  • IDLE
    • Written in Python
    • Basic IDE include with Python

Tools - Linters

Tools - Other

  • Debuggers
    • pdb
      • Built in debugger
    • Most IDEs have graphical debuggers
  • Documentation
    • Sphinx
      • Tool for creating documentation from reStructuredText
      • Used (with Hieroglyph) to create these slides
      • Easy API documentation with autodoc
  • Shells
    • REPL (Read–Eval–Print Loop)
      • Built-in console
    • IPython
      • Feature-rich console

Resources

Our First Script

  • See which version of Python you’re running

    $ python -V
    Python 2.7.12
    
  • Python 2:

    #!/usr/bin/env python
    
    # This is a comment
    print "Hello, world"
    
  • Python 3:

    #!/usr/bin/env python
    
    # This is a comment
    print("Hello, world")
    

Our First Script

  • Execute script

    $ chmod +x first_script.py
    $ ./first_script.py
    Hello, world
    $ python first_script.py
    Hello, world
    

Shebang

  • Tells the shell where to find the Python interpreter
  • #!/usr/bin/env python
    • Uses first python in $PATH
  • #!/usr/bin/python
    • Uses the system default python
  • #!/usr/bin/python3.6
    • Uses a specific python
  • All options are valid, just different

Using the Python Console

  • Enter Console

    $ python
    Python 2.7.12 (default, Aug  9 2016, 15:48:18)
    [GCC 6.1.1 20160621 (Red Hat 6.1.1-3)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
  • Exit Console
    • exit() or ctrl-d
    >>> exit()
    $
    
  • Enter console after running script
    • Useful for troubleshooting
    $ python -i first_script.py
    

Using the Python Console

  • Hello, world

    • Python 2:

      >>> print "Hello, world"
      Hello, world
      
    • Python 3:

      >>> print("Hello, world")
      Hello, world
      

Print: Statement vs Function

  • In Python 2 print is a statement
    • No parentheses
  • In Python 3, print() is a function
    • Requires parentheses
  • To use the print() in Python 2, hop in a Delorean

    >>> from __future__ import print_function
    >>> print("Hello, world")
    Hello, world
    
  • Bonus Question:
    • Why does print("Hello, world") produce the same output in Python 2 and 3?
    • Hint: Try print(1, 2)

White Space

  • In Python, white space matters
    • Standard indent is 4 space

    • No curly braces for code blocks

    • No semicolons at the end of statements

      • Still used sometimes for one-liners
    • Indent to start a code block

    • Dedent to end a code block

    • Indents must match!

White Space

  • Notice the second set of dots? The interpreter is waiting for a dedent.

    >>> for n in [1, 2, 3]:
    ...     print n
    ...
    1
    2
    3
    

Comments and Docstrings

  • Comments are preceded by a pound sign #

  • Comments can occur on separate lines or at the end of a line

    # This is a comment
    print('something')  # This is an inline comment
    
  • Docstrings appear at the beginning of a file or object definition
    • Generally in triple-double quotes
    #!/usr/bin/env python
    """
    This is where you would describe the script
    It is also a good place to include contact and copyright information
    """
    

Line Continuation

  • Appending a backslash \ to the end of a line will cause it to continue to the next line

  • Strings are automatically concatenated

    >>> myString = "This is the string the never ends. " \
    ...     "It just goes on and on, my friend."
    >>> myString
    "This is the string the never ends. It just goes on and on, my friend."
    
  • Open parentheses, braces and brackets imply a line continuation

    >>> print(
    ...       'something')
    something
    

Imports

  • Additional functions, classes, and objects can be imported from modules
  • Modules are simply a Python file
  • Packages are collections of modules or subpackages
  • A lot of very useful modules and packages are shipped with Python
  • Many other modules and packages can be found online
  • Imports are managed with the import statement

Imports

  • Import a module

    >>> import sys
    >>> sys.version_info
    sys.version_info(major=3, minor=5, micro=1, releaselevel='final', serial=0)
    
  • Import module with a different name

    >>> import sys as system
    >>> system.version_info
    sys.version_info(major=3, minor=5, micro=1, releaselevel='final', serial=0)
    
  • Import objects from a module
    • Notice the module name is not given when calling version_info
    >>> from sys import version, version_info
    >>> version_info
    sys.version_info(major=3, minor=5, micro=1, releaselevel='final', serial=0)
    

Imports

  • Sometime you will see a star in the import line
    >>> from sys import *
    
    • Don’t do this!
    • This means import “everything” from a module
      • What “everything” includes is configurable with a module
    • Wildcard imports can lead to unexpected behavior

Imports - Module Search Path

  • The module search path is installation dependent

  • The current path can be displayed with sys.path

    >>> sys.path
    ['', '/usr/lib64/python35.zip', '/usr/lib64/python3.5',
    '/usr/lib64/python3.5/plat-linux', '/usr/lib64/python3.5/lib-dynload',
    '/usr/lib64/python3.5/site-packages', '/usr/lib/python3.5/site-packages']
    
  • When running a script, the directory the script is in is the first in the search path
    • This is not the same as the current working directory
  • The path can be prepended with the PYTHONPATH environment variable

    $ PYTHONPATH=/usr/share/superpython:/usr/share/superduperpython python
    

Standard Library

  • A collection of packages and modules shipped with Python
  • Provides standard solutions for common problems
  • Provides standard interfaces for low level or OS-specific operations
  • Refer to the documentation for a complete list
  • Highlights
    • sys — System-specific parameters and functions
    • os — Miscellaneous operating system interfaces
    • glob — Unix style pathname pattern expansion
    • shutil — High-level file operations
    • re — Regular expression operations
    • random — Generate pseudo-random numbers
    • time — Time access and conversions (lower level)
    • datetime — Basic date and time types (higher level)

Online Help

  • The help() function, allows help access directly from the Python console
    • If the argument is a string, the topic will be searched for

    • Any other object will bring up help for that object type or class
      >>> help('tuple')
      >>> help(tuple)
      >>> help(myTuple)
      
  • Help is also available from the command line with the pydoc command
    $ pydoc tuple
    
  • Objects can also be inspected, using the dir() function
    • Implementation can vary by object type
    • Generally lists attributes and methods associated with an object