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

  1. VSC is installed on the lab computers and you can have ITS install it on other computers on campus.  If you are installing it on a personal computer, you should be able to install it from: https://code.visualstudio.com/
    1. Note that this is new and I’ve only installed it from a Microsoft website in the past.
  2. As we get started in VSC, you’ll need to know the ames of some of the panels and buttons in the VSC window but these won’t appear right away so you can use the figure below to find the names of the key elements to get started.

  1. 3. After you install and run VSC, you should see this screen:

I do recommend clicking on “Choose your theme” and select a theme you like.  Remember to get one that is high contrast so you can see everything.  Then, you can just close this panel by clicking on the “x” in the right side of the panels tab (it is labeled “walkthrough Setup VS Code”.

  1. I start by clicking on the “Explorer” icon which is the just below the VSC icon in the upper left of the window.
    1. Then, click on “Open a Folder”
    2. Select your “GSP 318” folder on your desktop or create a new folder.
    3. Check “Trust the authors…” and click “Yes, I trust the authors”.
    4. Close the Welcome panel because these only appear once and you’re going to need to learn VSC for lots of files.
  2. Click on “File” and select “New File”
  3. Type “Python” in the field that appears in the top middle of the window and click on “File” to the right of the field.
  4. Name the file “HelloWorld.py” and save it into your GSP 318 folder.
  5. If a message appears in the lower right asking if you want to install the “Python” extension, click “install”. 
    1. Close the python welcome panel if it appears and click on the “Explore” icon so we can see our files.
  6. Ignore the text in the panel and type:
    1. print(“Hello world”)
  7. Click the “Run Python File” which looks like a arrow pointing to the right and is in the upper right of the window.
  8. You should see “hello world” printed in the “TERMINAL” panel at the bottom of the screen. 
  9. Your VSC window should now look something like the one below.
  10. Image of the VSC Gui showing the run button, left panel options, file explorer, current script and where python outputs text.

 

There are a lot of features in VSC but we are only going to need a few for this class.  One really important one is the debugger.

  1. Go to the “Run” menu and select “Start Debugging”.  If you can’t see the “Run” menu, it may be that the VSC window is small enough that it has hidden some of the menus in the “…” menu item.
  2. Click on “Python Debugger”
  3. Click on “Python File”
  4. Click on “Run and Debug”
  5. The program should have run just as it did below.
  6. Add several more lines of code printing what ever you would like.
  7. Click just to the left of your first line of code and you should see a red dot appear.  This is called a breakpoint because your code will stop (break) at this point when you run it with Debugging.
  8. Click “Run and Debug” again and you should see a highlight on the line of code with the breakpoint.  This line of code has not yet been executed but the lines before it have. 
  9. Also, you’ll see a bunch of new stuff appear in the VSC window.  Below is a diagram of the things that were added during debugging that are going to help us debug our code much faster (and with less frustration).
    1. Variables: shows your variables that are defined and what values they are set to
    2. Breakpoint: Here you can add and remove breakpoints on any line of code in your files.
    3. Continue: Continues running the code from the current line.
    4. Step Over: Executes one line of code at a time.
    5. Stop Debugging: Stops the debugger and returns to regular editing.

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.