Mini-Lab: Getting Started with VSCode and Python

 


Introduction

The objective of this mini-lab is to become familiar with the programming environment VSCode and the language Python, which will both be used in COMP107.



Becoming familiar with VSCode

  1. Create a new folder on your machine where you will store your work for this class. You might call it something like CS107Labs.
  2. Start up VSCode. From the Start window, choose to open a folder and select your CS107Labs folder, to start working. (Alternatively, you can close the Start window, and then go to File -> Open folder to select your CS107 Labs folder.)
  3. Close the "Get Started" window if you haven't already. Go to File -> New file to create a new file. That will open a blank file in the Editor pane.
  4. Immediately go to the File -> Save As.... Give your file a meaningful name, such as GettingStartedML.py. Be sure to add the .py ending so that your file is identified as a Python file!
  5. We are now ready to write some code in your file. Type the following commands into your file:
    print(3 + 4)
  6. Now let's see what this does. In the upper right corner of the Editor pane, there is a little triangle. Click on it to run your program i.e., execute your code. You should see the number 7 get printed out in the Terminal pane (below the Editor pane). If this does not happen for you, ask an instructor to see what happened.
  7. We can now add more commands to our program. Type in the commands from the Exercise on page 2 of the reading: Using VSCode To Program with Python and Jes4py.
  8. Run your program. Do you get the results that you expected? If not, please ask an instructor or TA for an explanation.
  9. Run these commands.
    print(3 - 4)
    print(3 * 4)
    print(3 ** 4)
    print(3 / 4)
    print(3 // 4)
    print(3 + 4)
    print(3 + 4)
    Include the results in your submission. Detail whether the operations were expected. For those which were not, provide an explanation for what is occuring.

Working with picture files

Some of the following exercises will have questions to be answered and turned in at the end of the mini-lab. You should type your answers up in a Word document to be submitted at the end.
  1. In order to work with pictures, we will need to use the Jes4py library. We will tell our program to use this library by adding the following statement at the very beginning of your program:
    from jes4py import *
  2. In the same file that you were working with above, hit 'Enter' to add a blank line to your file after your previous statements, then type the following command (make sure upper/lower cases are exactly as given here):
    myFile = pickAFile()
    A file selector will open. Choose a JPEG (.jpg) file from a folder on your hard drive. (Or, if you prefer a wider selection of images, copy several images from the MediaSources directory to a folder on your own hard drive.)
  3. Click on the triangle to run your program again. What is this function pickAFile() doing? Did you see anything new happen when you ran the program? Write down your answer in a complete sentence in a Word document or in the markdown file provided above.
  4. Now add the line
    print(myFile)
    below your previous statement and run the program again. What is printed? Using sentences, write down the result and explain what this command is doing.
  5. Now add the statement myPict = makePicture(myFile) at the end of your program and run the program. What did you see happen? Write down what iyou think this function is doing. If you're not sure, ask an instructor or a TA.
  6. Add the statement print(myPict) to the end of your program. What is printed? Write down your result.
  7. At this point, you have not actually seen the picture you've chosen to work with. Add the statement show(myPict) to the end of your program and run your program. A new window should appear with your picture displayed, but you probably didn't see it happen.
  8. There are two ways we can fix this. First, at the top of you program, after the statement where you imported Jes4py, add the statement:
    from time import *
    and then add the line sleep(5) after your show statement. Now when you run your program your picture should be displayed for 5 seconds.
  9. Another way we can see the pictures is from the terminal. Mouse over the path of the filename and do a Command-Click to open the file in the Editor pane.
  10. Finally, let's practice saving the picture. There are two options for achieving this.
  11. Turn in your answers to the questions in these exercises on Kit.

Working with sound files

Complete the following exercises if you have time. There is nothing to write up from these exercises.
  1. We can also use the pickAFile function to select a sound file. Add the following line of code to the end of your program.
    soundFile = pickAFile()
    When the file chooser window opens, select a sound file. (Note: sound files have a .wav extension.)
  2. Now add the statement sound = makeSound(soundFile) to your program.
  3. Finally, to play the sound, we will use the blockingPlay function. Add this statement to the end of your program: blockingPlay(sound). If nothing happens, make sure that the volume isn't turned all the way down on your computer.