Python 101 - Introducing Python

ENV 859 - Geospatial Data Analytics   |   Fall 2023   |   Instructor: John Fay  

Introduction

Our journey to learn Python starts here, with no prior experience in coding necessary. We begin with a quick intro to Python, explaining why it’s so popular and why its worth the investment to learn it. Then we dive in. Coding languages are best learned by doing, and that’s the path we take: I will lead us through various demonstrations and exercises, noting important concepts and patterns that help understand the structure and use of Python. And I will also note aspects that are best just “accepting” as is, i.e., concepts I’ve seen in other Python lessons that tend to be more confusing than helpful to understand. Of course, if ever you are curious about something, feel free to ask! (On of the benefits to taking this as a real-person class vs a purely on-line web-course!)

The concepts covered here begin with the very basics of Python: variables, data types, data structures, and key elements of Python syntax. In later lessons (Python 101b), we will continue on to the steps in constructing workflows from these elements by writing Python scripts, steps including iteration (“loops”), conditional processing (“if” statements), and defining functions. Other useful Python techniques are peppered in along the way, and in the end you’ll have your basic Python toolbox which is surprisingly powerful.

Topics & Learning Objectives

Topic On completion, you should be able to…
A. Diving into Python Run a simple Python command
• Appreciate the power and elegance of Python
B. What is Python? Why Python? • Explain what makes Python an especially popular coding language
• Describe how Python relates to other coding languages, like R
C. Python and ArcGIS • Explain the advantages to learning Python for geospatial analysis
• Run a Python command from ArcGIS Pro command window
• Run a Python command from ArcGIS Pro notebook
• Run a Python command from an ArcGIS Online notebook
• Run a Python command from a Python Command prompt
• Appreciate that ESRI is not the only game in town once you know Python

A. Diving into Python

I’m a strong believer in learning by doing, so let’s dive in.

  1. Open your browser (Chrome or Firefox) to: https://cmgr.oit.duke.edu/containers and login.
    :point_right:Note: The log in process now looks a bit different than in the video…

  2. Find the link to create your ENVIRON859 environment: https://cmgr.oit.duke.edu/containers/ENVIRON859

  3. In the page that appears, find the New:▼ menu and select Python 3.

  4. In the new browser window that appears, click in the gray area next to In [ ]: and type:

    print ("hello world")
    
  5. Click the |► Runbutton.

Below the Python command you just typed in, you should now see the results of your command. Not too exciting perhaps, but you have to begin somewhere. To spice things up, try this:

  1. In the new empty gray box, type the following commands:

    from arcgis import GIS
    gis = GIS()
    gis.map("Duke University",mode="3D",zoomlevel=1)
    
  2. Run the commands by clicking the run button again.

  3. Finally, type and run this Python command:

    import this
    

These few lines of code packs a lot in, no? Of course there’s lots going on behind the scenes, and that’s the part we’ll cover in the next several sessions. Let’s start by with a chat about what exactly Python is and where it fits into our analytical toolkit.


B. What is Python? Why Python?

:point_right: A short slide deck from Jake VanderPlas introducing Python: Link

What is Python?

Not terribly long ago, most GIS aficionados probably first imagined a snake when they heard “python”. Nowadays, most anyone in a tech-related field envisions code when they hear the word. A quick web search will reveal Python to be one of the most popular coding languages in the world. Noted for its simple syntax and code readability, Python’s relative ease of use has made complex processes like machine learning and image processing accessible to non-computer scientists.

Ok, even if Python is so wonderful, why would we want invest in learning it if we can do so much with software like ArcGIS Pro? I offer a few reasons:

  • First, adding Python to your geospatial toolkit expands your analytical capabilities tremendously. ESRI has a wonderful legion of programmers who add new tools to its ArcGIS suite each year, but what happens if ArcGIS does not have that one tool you need? Python gives you the space to write additional tools yourself: tools that do something geospatial or that tie your geospatial data with tools that do entirely different analyses. [Recall how awkward it was to iterate through datasets using ArcGIS Pro’s model builder; Python makes that process much easier…]

    Furthermore, since Python is open source, it has a much larger army of coders than ESRIs, and these coders are busy producing new tools that can boost your productivity tremendously. So, by combining ArcGIS Pro with Python, your abilities become virtually unbounded.

  • Second, while you may find comfort in using ArcGIS pro’s graphic user interface (GUI), at some point you may find that typing commands is actually more efficient than hunting items in menus and clicking them. This can be especially evident in repetitive tasks where just one or a few inputs change. Knowing how to craft a Python script can save you gobs of time as we’ll see later in this course.

  • And third, developing geoprocessing workflows in Python makes for much more reproducible research. ESRI’s model builder is nice, but it still buries some key information that makes analyses hard to reproduce. Python scripts are fully transparent and allow others, even though that aren’t too familiar with GIS, gain some understanding of the key steps in your workflows.

Why Python?

A number of coding languages do spatial analysis, so why Python? In fact, the debate between whether to use R or Python is a lively debate here in the halls of the Nicholas School. (Spoiler: most eventually agree its best to just learn both…). I will not belittle R’s ability to do geospatial analysis, but here Python wins for one big reasons: ESRI has invested heavily in Python, making Python its default coding language. Not only is this a big vote of confidence in the language, but it of course makes it much easier to transfer our knowledge of ESRI-based GIS to the Python world: we get access to everything ArcGIS Pro can do, and more, via ESRI’s Python tools, which we’ll discuss next…

:point_right: Just Google “Python vs R” and you’ll see gobs of interesting comparisons between the two. I like the one here


C. Python and GIS

Python and ArcGIS

ESRI’s first GIS software , Arc/INFO, predates graphic based operating systems and was a command line program. Yes, all analyses were done by typing commands – even making maps. (I don’t miss that!) Back then, ESRI developed a language called Arc Macro Language, or “AML”, that allowed you to string these commands together, more or less automating processes. AML scripts were useful, but they were limited to working with Arc/INFO. Meanwhile, other more generalized programming languages allowed cross-application programming (e.g. integrating ArcGIS with MS Office). ESRI responded and started supporting other, less exclusive, languages for those still preferred coding than point and click GIS. These included Visual Basic, Java, C++, and yes, Python.

Python, because of its relative ease of use and widespread popularity among non-programmers, bubbled to the top, and while you can still leverage ArcGIS using Visual Basic, C++, Java, etc., you’ll have to go hunting for that documentation while instruction on how to use Python with ArcGIS is integrated in its help documentation:
https://pro.arcgis.com/en/pro-app/arcpy/main/arcgis-pro-arcpy-reference.htm

Python is integrated directly into ArcGIS Pro application via the Python command window (found when the “Analysis” tab is active. Furthermore, with the latest release (v.3.0) ArcGIS Pro, a Python coding platform called “Jupyter notebooks” is fully integrated with the application. All geoprocessing tools and more are made accessible to Python coding environments via the arcpy package, which we will dig into later in this course. Additional GIS capability is enabled via ESRI’s ArcGIS API for Python, a separate Python package that is independent of ArcGIS Pro. We will examine this later as well. Python has been installed on all NSOE lab machines as part of installing ArcGIS Desktop and ArcGIS Pro. Python, however, is not a sub-component of of ArcGIS but rather is installed alongside ArcGIS. In fact, if you have both ArcGIS Desktop and ArcGIS Pro, you have not only two separate installations of Python, you have two different versions of Python (2.x and 3.x, respectively).

Ways to run Python on a machine with ArcGIS Pro Installed*

► Find and run the following in your Windows Start menu, under the ArcGIS sub-menu.

  • Python Command Prompt: Still need to type python to get to the Python prompt
  • Python Interactive Terminal: Version?
  • IPython shell: This is interactive Python, more than just a command line..
    • {DEMO} “tab completion”
  • IDLE (Python GUI): A graphical user interface for Python (meh…)
  • And we can access a Python command prompt from within ArcGIS Pro
  • We can also create a Jupyter Notebook from within ArcGIS Pro or outside of it.

* See box below for how Python is installed without ArcGIS Pro

So many ways to access Python! This can be confusing, especially at first. To simplify matters, we’ll limit ourselves at first to using Jupyter Notebooks, a platform that is a bit more user friendly. We’ll have a more formal introduction to Jupyter Notebooks in the next lesson.

Python and open source GIS

Learning Python has many benefits outside its link with ESRI’s geospatial projects. Among the thousands of open source packages available in the Python coding environment are a number of quite powerful, freely-available GIS packages. These include Shapely, GeoPandas, PySAL, and Fiona, which we will address later (time permitting), as well as packages to interface with Google Earth Engine and some widely used machine learning and image processing packages (Scikit-Learn and Scikit-Image).

Python without ArcGIS Pro

Of course, ArcGIS Pro is not required to install and run Python. in fact, Macintosh computers come with it pre-installed. For reasons I’ll explain later, I recommend installing the Anaconda or Miniconda distributions of Python. (Anaconda has a nice GUI with MiniConda being more lightweight…) But don't install Anaconda or Miniconda on a machine with ArcGIS Pro! First of all, you don’t need to install another instance of Python, but more importantly, your computer will get quite confused as to which Python to use!


So yes, if you plan on sticking with GIS in your career, learning Python will pay great dividends!