Our first day we're going to focus on getting used to Visual Studio Code to run Python programs. Visual Studio Code is an Integrated Development Environment (IDE). That means it's a program designed to help us write programs. Visual Studio Code is too long to keep typing out. From here on we'll refer to Visual Studio Code as VS Code.
First things first, open Visual Studio Code.
Let's get used to environment. VS Code offers various accessibility features, so you'll want to spend some time finding the ones that are important to you.
If you are using magnification or if you need to make the text bigger you can use the Ctrl + = shortcut to make make the text bigger. Ctrl + - will make the text smaller.
If you are looking for VS Code keyboard shortcuts you can try this page (which is the same as the shortcut button at the top of this page).
VS Code has put some thought into its accessibility support. The following links might be of use in getting comfortable in VS Code. The first hyperlink is a link to VS Code's accessibility page. The additional links are a non-exhaustive list that will jump you to specific topics on that page.
Let's open a simple Python program that is already on your computer. We'll do that using the Explorer panel in VS Code. This panel is located on the left-hand side of the VS Code. The Ctrl + Shift + E shortcut will take you the panel.
The Explorer panel contains a list of folders and files. Open the folder named "Lab 1" and select the file named "Hello.py". This should load the "Hello.py" Python program in the code editor in the main panel of VS Code. You can use the Ctrl + 1 shortcut to jump to the Editor panel.
Look over the Python program. There are only two kinds of lines in this program. The first line starts with a # character and is a comment. Any line that starts with # is just a comment for the reader and will not be executed by the computer.
The other lines are all print commands, which will print text to the terminal. Just by reading this program you should have a good idea of what it's going to do.
If you're using a mouse the run button is the top-right corner of the IDE and is a triangle that looks like a play button. If you're using keyboard shortcuts you can press Ctrl + F5 to run the program.
When you run the program a Terminal window should spawn at the bottom of the IDE. If you are using keyboard shortcuts you can press Ctrl + ` to jump to the terminal window.
Observe the output of the program.
In this step we're going to introduce an error into our program so we can see what a mistake looks like.
When a Python lab at camp shares a line of code it will include the line number at the start like this:
7 | print("Congratulations!")
This should match the last line of the Hello.py program. Change your program to match the following line:
7 | print("Congratulations!
Be sure not to copy the "7 | " in the line above to your program, that's only there to provide a line number so you can locate which line it belongs on.
This new line 7 basically just removes the last to characters from the original line to introduce a mistake. The IDE will detect the error and underline the mistake with a red squiggly like a misspelled word to call your attention to it.
Run the program that has a mistake. Press the run button in the upper-right corner or press Ctrl + F5 to run the program.
Look at the terminal to see the output of our incorrect program (Ctrl + `). The terminal now reports SyntaxError: unterminated string literal (detected at line 7)
You can also look at the "Problems" tab to see what errors the IDE has spotted. The keyboard shortcut Ctrl + Shift + M will jump you to the Problems tab. The Problem tab lists two problems it sees with our new program.
1. The first problem is that the "(" is not closed. This is because we deleted the ")" character.
2. The second problem is that the String literal is unterminated. This is because we deleted the quotation mark " at the end.
As you can see, Python want certain things like ( ) and " " to come in pairs.
Go ahead and fix your program so line 7 ends like the lines above it then run your program again to verify that it works.
Spend a bit of time editing the program and rerunning it to make it say different things. Especially do this if you are new to programming. There no magic here, programs are just instructions that you give a computer. Change the instructions and personalize this program for yourself.
Let's add a couple of more lines to your program so that it accepts input as well as produces output.
8 |
9 | username = input("What's your name?: ")
10 | print("Hello " + username)
Copy the lines above to your program. Remember you don't need to copy the line numbers, the IDE will list the line numbers for you. Line 8 is simply a blank line.
Try and run your improved program by clicking the Run button in the upper-right corner of the IDE or with the Crtl + F5 shortcut. If there are problems double check to make sure your program exactly matches the example lines of code.
If everything is working, you should see the output from your program plus a new prompt that says:
What's your name?:
Click the Terminal panel (or press Ctrl + `) then type your name. You should see it appear in the Terminal panel. After typing your name, press the Enter key and the program should add a new line that says hello to you.
Let's explain those new lines we added. Line 9 is doing two interesting things. It introduces a variable named username that is storing the name we typed in to the terminal. Up until now we've only see print commands. This line also introduces the input command that does.... well you probably guessed that it takes input. This command is the reason we can type our name into the terminal panel.
Line 10 is just a print command but it uses the variable we introduced in line 9 and stored our name in.
Let's rework the last couple of lines of your program to spice it up a bit.
Copy the following lines to your program. These will replace the lines 9 and 10 you added in the previous step. Remember you don't need to copy the line numbers, the IDE will list the line numbers for you. Line 8 is simply a blank line.
8 |
9 | first_name = input("What's your first name?: ")
10 | last_name = input("What's your last name? ")
11 | print("Hello " + first_name + " of House " + last_name)
Try and run your improved program by clicking the Run button in the upper-right corner of the IDE or with the Crtl + F5 shortcut. If there are problems double check to make sure your program exactly matches the example lines of code.
If everything is working, you should see the output from your program plus a new prompt that says:
What's your first name?:
Click the Terminal panel (or press Ctrl + `) then type your first name and press the Enter key. After entering your first name you'll see a second prompt asking for your last name. Type your last name and press the Enter key. Now your program will greet you more formally.
Let's explain these new lines. This time we've created two variables. Once called first_name and the other called last_name. Line 9 gets and stores a value in first_name. Line 10 gets and stores a value in last_name. Then line 11 prints both of the values out while interjecting some text in between them.
Try running the program and entering something other than your name. The computer doesn't care what you enter for each prompt. Whatever input you give the program will be stored and printed out.
Hopefully you've used this time to get comfortable working in VS Code. This lab demonstrated a couple of the most basic programming building blocks in Python: input and output. You should be able to load a python file, edit it, and run it. Then swap over to the terminal and interact with your program.
Lastly, we seen a couple of bugs and fixed them. Making mistakes and figuring out how to fix them is a big part of writing code.