EX00 - Hello, world


Welcome to COMP110 - Introduction to Programming at UNC Chapel Hill!

0. Complete the Prerequisites

Before continuing on, be sure you’ve completed each of the following steps to prepare your computer for the course’s programming assignments:

  1. Update your Operating System
  2. Install Python and VSCode)
  3. Set-Up Workspace

1. Write Your First Program!

Now for the main attraction! Let’s write a classic “hello world” program.

In Visual Studio Code, be sure your workspace is open. (Reminder: File > Open Recent > comp110-workspace is a quick way to reopen it!)

Open the Explorer pane (click the icon with two sheets of paper or to to View > Explorer) and expand the Workspace directory.

Right click on the exercises directory and select “New File”. Enter the following filename, being careful to match capitalization and punctuation:

  • ex00_hello_world.py

An empty file opens. The .py file extension is standard for Python programs.

For the past 50 or so years it’s a tradition in the programming world for your first program to print the words “Hello, world.” We won’t break it.

Write the line of code below in your file:

Next, save your program (shortcuts on Windows: Control+S, Mac: Command+S).

To run your program, first open a terminal: Terminal > New Terminal

Then, in the Terminal, type out the following command and press enter:

python -m exercises.ex00_hello_world

If it works, you should see the text “Hello, world.” printed in your terminal! If it doesn’t work, it’s likely a minor issue somewhere. Programming is very precise, go back through and try each instruction again checking for exact punctuation, capitalization, and spelling matches. Even a minor discrepency will cause it to go awry. If you are stuck, come see us in office hours on Monday by following the instructions posted under Sakai’s Resources page and we’ll be able to work it out with you! As you know, there were a lot of steps to get to this point, so it’s not uncommon for something super minor to be off somewhere. The good news is, once your setup is working you’ll be in good shape moving forward.

Congratulations, you’ve written and run your first Python program! Try running it again by pressing the up arrow on your keyboard, noticing the same command you ran last is filled in, and pressing enter again.

What is the command you wrote doing? The first word python is the program responsible for interpretting your code. The second symbol, -m, is what we call an “option” that is short for “run as module”. You’ll learn all about modules soon. Lastly, exercises.ex00_hello_world is referring to the file you created! Notice you created the file ex00_hello_world.py in the exercises directory, or “folder”. This is the module you are asking python to interpret!

Try changing the text inside of the double quotes (but be sure to keep the words hello and world somewhere), saving again, and rerunning your program.

What is this line of code doing? You are calling a function named print. The print function is built-in to the Python programming language and results in data being “printed” out somewhere. By default, that somewhere is on your screen in the Terminal. The parentheses following the word print indicate extra information being given to the print function. In this example, you are giving a piece of textual information to print which is called a “string” and denoted by the double quotes surrounding the textual data. Don’t worry, we will break down all of this into more tangible detail soon!

Your first program is almost complete! However, before submitting it there are a couple more style and documentation steps to complete.

First, you should add a special kind of string value, called a docstring short for documentation string, to the top of your program file, which is a Python module. Then, you should add a line with a special variable named __author__ assigned to your name and e-mail address. Add the following lines above the line of code that calls the print function. Fill in your name and e-mail in the __author__ string.

Save your program again and re-run it. You should still only see your printed output message. What must that mean about the two lines of code you just added? They’re for documentation purposes and must not impact the printed output of the program.

Congratulations!

There were a lot of steps and new concepts thrown at you in this initial exercise. Gearing up is half the battle! The amount of setup involved in a modern development environment can be a little overwhelming to a first-time programmer. You are not expected to understand the intricacies of all the processes and software you just followed and setup. That will come with time. For now, focus on the big win that is having written a Python program in a professional code editor, run it, and backed it up using git! Programming gets way more fun, and way more creatively rewarding, as we make our way up the mountain from here.