Python 101 - Session 1

ENV 859 - Advanced GIS John Fay, Fall 2017

Prep

  • Ensure ArcGIS Desktop and ArcGIS Pro are installed on your VCM image
  • See ArcGIS Desktop Post-Install and ArcGIS Pro post-install docs
  • Fork WhirlwindTourOfPython and clone to your machine using GitHub desktop
    • Doc on how to fork repositories: link

Introduction

Source

  • Dive in: Python command line in ArcMap

    • Hello world
  • What is scripting? Link

  • Python vs other languages

    • The Zen of Python; "Pythonic"

Set-up: Where does Python live and how to run Python code

Source and here

  • ### Where does Python live?

    • Python 2.7 with ArcGIS Desktop C:\Python27\ArcGIS10.5\python.exe
    • Python 3.5 with ArcGIS Pro: C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe
    • Also: Anaconda Python* - https://anaconda.org/anaconda/python
  • ### Running Python code: Hello World

    • Python Command shell
      • C:\Python27\ArcGIS10.5\python.exe
    • IPython shell
      • C:\Python27\ArcGIS10.5\scripts\ipython.exe
    • Python scripts...
    • Jupyter notebooks...
    • IDEs: PythonWin
  • ### Quick Tour of Jupyter notebooks

    Source and here

    • How it runs: Server, content (notebooks), browser, JavaScript
    • Open notebooks (.ipynb) via main page
    • Markdown vs code
    • Executing code
    • Create a new notebook
    • Add a cell
    • Write code
    • Execute code
    • Closing a notebook
    • Closing the Jupyter server

Basic Python Syntax

Source

  • Reading Python code

  • Comments

  • Whitespace

  • End of lines

Basic Python Semantics: Variables and Objects

Source

  • ### Python variables

    • Variable assignment with =
    • Mutable vs immutable objects
    • Two variable names can point to the same mutable object
      • Change one and the other changes
  • ### Objects and data types

    • In Python, the variable's value assigns its data type
    • type() function
    • help() function

Basic Python Semantics: Operators

Source

  • Python as a calculator
    • Addition, subtraction, multiplication, division (true and floor)
    • Modulus
    • Exponentiation (powers)
  • Bitwise operations (not covered...)
  • Assignment operations
    • Assigning and re-assigning values with =
    • Augmented assignment values: +=, -=, *=, */,...
  • Comparison operations
    • returns a Boolean
    • is equal to, is greater than...: '==, >,>-,!='
  • Boolean operations
    • and, or, not
    • Boolean vs bitwise
  • Identity and membership operators
    • is infers object identity, not equality; two variables can be equal, but not the same object
      • If x is y is true, then changing one will change the other
    • in checks for membership

Built-In Types: Simple Values/Scalars

Source

  • Integers
    • "fixed" vs "variable" precision (short, long)
      • a = 2; type(a) -> 'int'
      • b = 2 ** 250; type(b) --> 'long'
    • Integer division, v2 vs v3...
  • Floating-point
    • standard vs decimal notation
    • floating point precision gotcha
  • Complex - not covered...
  • Strings
    • Quotes
    • string functions (Jupyter's tab completion)
    • Concatenation
    • String indexing and slicing (to be continued...)
  • None
  • Booleans
    • Deriving vs creating
    • True and False keywords
    • 0 is false, all other numbers are true: bool(0) vs bool(-23)
    • "" is false, all other string is true: bool("") vs bool('false')