Masthead

Introduction to Python in VSC

1. Introduction

This tutorial will get you started programming in Python really quickly. We'll use the Visual Studio Code (VSC) for an Integrated Development Environment (IDE). This is similar to the Wing IDE that GSP 318 used in the past.

2. Installing Software

This section shows you how to get started with Python and VSC if you are not using one of the CPH computers that already has them installed.

2.1 Getting Ready

  1. First, make sure you have the latest update to your virus software on all the computers you'll be using
  2. Create a folder called "GSP 318" on your desktop.
  3. Remember to back up the folder at least once a week so you don't lose your work!

2.2 Making Sure You Have Python

If you have installed ArcGIS, then you should already have Python installed on your computer. However, this copy only works with ArcGIS so you'll want to install your own copy.

  1. Go to the Python web site at "Python.org"
  2. Click on "Download"
  3. You'll see that there are a couple of versions of Python and installers available for a wide variety of operating systems. This website refers almost entirely to Python 3.x as it has almost comletely replaced older versions of Python. Where possible, I use Python syntax and functions that are common to both versions.
  4. Click on the link for the latest released version of Python 3x and download the installer that is appropriate for your computer. If you have a 64-bit computer, it is recommended to install the 64-bit version of Python.
  5. The installation instructions for Python will vary with your operating system but they are pretty easy to follow and you should be able to just "click through" using the defaults.
  6. Python will typically be installed in a folder in your "root" drive ("C:/Program Files/Python3X" on MS-Windows).

Note: ArcGIS installs a custom version of Python that has some compatibility problems with Python libraries. We'll talk more about this later.

Installing VSC

If needed, install VSC from Microsoft.

  1. You'll want to already have a copy of Python running on your computer.
  2. Search for VSC on your computer or your browser and follow the installation instructions from Microsoft.
  3. Then, install the extension for Python in VSC. VSC allows development in a variety of languages and needs this extension to compile and debug Python scripts.

3. Using VSC with Phython

Getting Started

  1. From the File menu, select "New File...". VSC may also ask you to create a project folder at this point.
  2. Give the new file a good name with the ".py" extension and save it in to the project folder (e.g. Learning.py).
  3. Below is a screen shot of VSC and the key elements of the interface we'll be using this week.

Image of the VSC Gui showing the run button, left panel options, file explorer, current script and where python outputs text.

Figure 1. The VSC application interface with the first elements that you'll be using identified.

Since you can have a number of different versions of Python on your computer, it is recommended that you set the path to the version you want to be using. Otherwise, Wing may select one you do not want it to use!

  1. Select "View -> Command Palette".
  2. Enter "Python: Select Interpreter"
  3. This will show you the current path to the program (interpreter) that will run (execute) your Python scripts. You can also change the interpreter here. This is not important if you only have one Python interpreter installed (not including the one that comes with ArcGIS Pro). We will go back to this setting when we work in ArcGIS Pro and need to select its interpreter.

3. Your first project

We'll get started by creating a new file to print out information.

  1. Type the following line of code exactly and type "Enter" at the end:
 print("Hello World")
  1. In the upper left of the window you should see an arrow that looks like one that might appear on a DVD or CD player. This is the "play" or "run" button and will run your program. Click on the arrow now.
  2. You should see "hello world" appear in the "Terminal" panel at the bottom of the screen.
Warning: Computers are just a machines and will do exactly what you tell them. Unfortunately, this means you have to be very precise in what you tell them or they will do something you don't want them to do. You will get used to this quickly but for now you'll want to move slowly and carefully - don't jump ahead or you'll start having programming errors and will get frustrated.

 

You just used the "print()" function to write out a "string" of text to the Terminal panel. This is a very powerful way to see what your programs are doing and you'll use print() a great deal. For now, you can type anything you want between the quotes inside the parenthesis and see it typed out. Try some of the examples below and then make up at least 5 of your own. Change the text in the print function and press the "run" button and verify that you see your output in the "Terminal" panel.

print("Computers are just machines")
print("I have to make sure each statement is correct")
print("Fortunately, the IDE will help me do this!")
Programming can be challenging at times and frustrating at others. Remember to celebrate the little accomplishments you make and have some fun with programming when you can.

4. More Data Types

You've now printed out some "strings". Strings are sequences of "characters" which are coming from the keyboard and into the computer as you type them. Strings are used to input text into programs and output text from programs. Python itself is a "text-based" language that the Python interpreter (another program) converts into a language the computer understands (pretty cool eh?). There are a number of other data types and also more complex structures that combine these data types.

4.1 Integers and Arithmetic

Type the following and press run:

print(2+2)

You should see "4" appear in the Debug I/O panel. Python allows us to perform a wide variety of mathematical operators just by typing them. The plus ("+") and minus ("-") symbols are for additional and subtraction. The asterisk ("*") is used for multiplication and the back slash ("/") is used for division. Try the following and then try some of your own:

print(10*13)
print(20/2)

These numbers are all integer values or values that do not have anything after the decimal.

4.2 Floating point numbers

Floating point numbers allow us to have fractional values. Try the following and then try some of your own.

print(2.3+1.2)
print(2.3*2)
print(2.1*3+2-0.2)

4.3 Using the Run-Time Debugger

VSC includes a powerful tool to help you debug your programs and it makes a great tool to learn programming.

On the left side of the script panel, you'll see an arrow when you run your program. Click in the arrow and you should see a red dot appear. This is called a "break point" and VSC will stop at this point when we run the debugger.

Click on the drop down arrow next to the run (arrow) button and select "Python Debugger: Debug Python File".

You should see something like the following:

This indicates that the IDE has "stopped execution" at the line where you placed the breakpoint. You now have "variables" appearing on the left panel and the arrow is selected to the left of that. Note that when you want to return to your script, just click on the first icon in the far left panel (looks like two pages). Remove the breakpoint for now and we'll talke more about breakpoints and debugging in the future.

4.4 Variables

Variables are how we store values within a program. Python is an "un-typed" language which means it is very easy to create variables. For example, try:

x=10 
print(x)

Now that we can declare variables, we can perform operations on them and then print out the results. Try:

x=10
y=3
z=x*y
print(z)

We'll be using variables in almost all of our scripts so spend some time creating different variables and doing simple arithmetic. The standard symbols work including; +, -, *, /, and ^.

Variables names must start with a character (a-z or A-Z) and can not contain punctuation other than underscores.

4.5 Watching Variables Being Created and Changed

  1. Enter a couple of lines of arithmetic and set a breakpoint at the first line.
  2. Select "Python Debugger: Debug Python File" and then click the "Step Over" button a couple of times and then take a look at the left panel. You should see "Variables -> Locals -> special variables" and then the variables you have created.

You can use the "Step Over" button to step line by line through your code and watch it create variables and compute values. This is makes it relatively easy to debug your programs.

Important: In just a few more chapters, you'll be writing Python very quickly. However, your programs will then have problems and you'll need to quickly stop, set some breakpoints, and see what is wrong. Without having the ability to set breakpoints and examine variables as they change on each line of code, you can spend days trying to find even simple programming errors.

Write some more lines of code at this point and use breakpoints and the "step-over" button to see how they are created and changed in your programs.

5. Errors

As we mentioned earlier, computers are just machines and if we make a mistake in the syntax of our scripts the IDE will let us know. Type the following into your script and run it:

print("Hi"

In the "Terminal" panel you should see an error. Notice that other than giving the line number where the error occurred, the error messages are not very helpful. This is pretty typical of errors from interpreters in all languages. Add the missing parenthesis to the end of the "print" function and run the program again. The exception should go away but watch for them in the future and fix them before moving on.

6. Comments

Just as important as the scripting we create is the documentation that goes with the scripts. If you don't add documentation you'll quickly find yourself wondering what the scripts do! Imaging what would happen if you didn't comment your code and you don't come back to it for a year or more. You'd probably have to rewrite the script from the start.

Comments allow us to add text into our programs that the Python interpreter will just ignore. Just type a pound sign ("#") and then your comment like the following:

 # this is a comment

Additional Resources

An Informal Introduction to Python up to section 3.1.2

Beginner’s Guide to Python

Python for Non Programmers

Python in Visual Studio Code

© Copyright 2018 HSU - All rights reserved.