hello_world / Week 2 / due: 2018-01-18 23:59

Just print ‘Hello, world!’.

Requirements

This section details the exact requirements I expect from your homework folder, including the existence and correct spelling of filenames, to expected behavior when I run your code. You might as well assume that your code is being run by an automated process, a process that will fail upon even a typo.

In places where you see, SUNETID, e.g.

compciv-2018-SUNETID/week-00/foo_bar/

Replace it with your own SUnet ID. For example, if your SUnet ID is dun:

compciv-2018-dun/week-00/foo_bar/

Expected files

Your compciv repo is basically a folder of files, what’s on Github.com is basically the same as what’s on the repo as stored on your own computer. But for the sake of clarity, I explain what things should look like whether I’m visiting your repo on Github.com, or if I’ve cloned your repo onto my own computer.

When I visit your Github.com repo page

I expect your Github repo at compciv-2018-SUNETID repo to have the following subfolder:

compciv-2018-SUNETID/week-02/hello_world/

On this subfolder’s page, I would expect the file tree to look like this:

└── hello.py

When I clone your Github repo

If I were to clone your repo onto my own computer, e.g.

$ git clone https://github.com/GITHUBID/compciv-2018-SUNETID.git

I would expect your homework subfolder to look like this:

compciv-2018-SUNETID/
└── week-02/
    └── hello_world/
        └── hello.py

Expected behavior and results

After I’ve cloned your Github repo, assume that I will run the following shell commands and/or Python scripts, and expect the stated output and effects.

Test A. Running hello.py prints ‘Hello, world!’

If I run the following shell command:

python compciv-2018-SUNETID/week-02/hello_world/hello.py

I expect this exact output to my screen:

Hello, world!

Alternatively, the following sequence of shell commands should get the same result:

$ cd compciv-2018-SUNETID/week-02/hello_world
$ python hello.py

In other words, I use the cd command to “change” into the hello_world subdirectory. Then I run the Python interpreter on hello.py. It is something that has no effect on how you should write the program, I’m just showing another way that the location of files can be addressed, because certain situations require different setups…


Background and context

As we’ve touched on before, the “hello, world” program was devised by Brian Kernighan and Dennis Ritchie and is a cherished tradition by virtually every programmer at every level.

The program itself does something very simple and non-helpful. The actual challenge that you overcome is getting a program to run at all, on a specific language and computing machine.

As Kernighan elaborates in his artist’s statement/bio on Artsy.net:

This is the basic hurdle; to leap over it you have to be able to create the program text somewhere, compile it successfully, load it, run it, and find out where your output went. With these mechanical details mastered, everything else is comparatively easy.”

In hindsight, life is rather more complicated, but this is still a good first step, and “hello world” has taken on a life of its own as the standard first example for programming textbooks.

Walkthrough/Hints

I’ll spoil the answer for you. This is all the Python needed to complete this homework’s requirement:

print('Hello, world!')

Of course, there’s more to the assignment, like how your code should be in a file named hello.py in a specific subdirectory, but the Python part is done.

Run Python interactively

If Python was installed on your computer, then it (should have) come with a executable program – which I refer to as the Python interpreter – with the name of python.

The vast majority of times, we invoke the Python interpreter by “feeding” it a Python script, e.g.

$ python  hello_world.py

But sometimes it’s fun/ideal to try out Python interactively, line-by-line, before we commit to creating a new file and writing code etc etc.

If you invoke the Python interpreter without reference to a file name, it’ll go into “interactive mode” – you can read a bit about it in the official Python docs. Sometimes you’ll see it being referred to as a REPL, which is a term so fancy that I have to look it up on Wikipedia to remember the acronym.

The gist Python’s interactive mode is that every time you hit Enter, the Python interpreter reads the line (or lines) of Python written, executes the instructions, and shows you the result (if applicable).

This interactive interface is especially hand for the “hello world” assignment because the assignment requires literally just a line of code.

Assuming you’re at your system shell (note that this is a point of confusion for many, MANY people at first), i.e. MacOS’s Bash, and Windows’ PowerShell, you can enter interactive Python like this:

$ python

You are now in the python interactive environment/shell/command-line interface. To make it obvious that this is different than before, I’ve changed the symbol for the prompt from $ to >>>.

Try running the code for the “hello world” program at the Python interactive prompt. It should look like this:

>>> print('Hello, world!')
Hello, world!

And that’s all there’s to it to having your Python code interpreted and executed. Though again, the assignment requires that you write it as a Python script, i.e. a text file named hello.py that contains Python code.

To leave the Python interactive shell, type the following command (which is essentally a Python function named quit, not that it’s super relevant right now):

>>> quit()

python vs. Terminal vs. PowerShell

Did Python’s interactive mode seem very similar to whatever Terminal/Powershell usually is? For example, when you typed quit() in interactive Python, was it hard to tell what changed?

Here’s what my Terminal looks like, when invoking python interactively, doing “hello world”, then quit(), and then, for kicks, the Python “hello world” program again, which causes an error.

The Terminal screenshot below shows the actions – I’ve tried to highlight in pink the commands (Python and bash) that were actually run:

../../_images/bash-to-python-to-bash-to-error.png

Invoking the python interactive mode immediately sends you to another shell/command-line interface, but one that only understands Python – the Bash and PowerShell interfaces only understand, respectively the bash and PowerShell languages – which are not Python other than the fact that it’s all just text.

This will be confusing if you are new to the command-line. But if you know web browsers, think about Terminal/Powershell like you think about web browsers:

  • You see cool TV commerical for a website named “Bing”.
  • You want to check it out, so you launch Firefox, your favorite web browser.
  • You set Firefox to load www.google.com upon launch, because you use Google for finding things on the Internet.
  • You type “bing” into google.com’s search box and hit Enter.
  • This sends Firefox to https://www.google.com/search?q=bing, and Firefox loads the search results for “bing”
  • You click the top result for “bing”, which is the URL https://www.bing.com
  • Firefox leaves google.com, and loads up the website at www.bing.com, which looks and functions almost exactly like what you’ve used at www.google.com
  • You use Bing to search for something you’ve searched before on Google.
  • You are surprised at the results.
  • You restart Firefox to see if that’s the problem.

Using Firefox, you used Bing.com to get to Google.com. When you left one site to visit the other, your Firefox browser was still the same. And even though typing a query into Google.com looks and feels the same as using Bing.com, your query – i.e. commands/code in Terminal – are being read and interpreted by a completely different system.


In the end, it’s not a big deal if you don’t grok the difference between “interpreters”. We’re not learning the Bash/PowerShell languages in the same way we are learning Pythong, but it’s important to know that there is a difference.

Try iPython

One more detail about shells and command-lines – we will almost never use the interactive mode of the “plain” python executable. Instead, we use ipython, which should have been installed if you installed Anaconda.

The only thing worth caring about is that when you want to use Python interactively, don’t run the aforementioned command, i.e.

$ python

Instead, do this:

$ ipython

What’s the difference? The ipython interpeter i.e. “REPL” uses the same Python rules/language as python. It’s just a much nicer command-line interface, and that’s all that matters.

How to create (and run) hello.py like a proper Python script

Let’s just do (basically) what’s asked: write the Hello World program as a Python script named: hello.py.

Try this: open your code editor, whether it be VS Studio or whatever.

Create a New File (basically, a blank text file). Type our the line of Python code that you know works, because you confirmed it in interactive Python:

../../_images/vs-code-new-hello-world.png

Then save the text file, with the name of hello.py.

However, properly naming the file is easy. You need to know where on your computer it is saved. I’m almost sure that the default directory is not where you want it to be.

For now, I highly recommend saving it to your Desktop (unless it’s completely piled with files).

../../_images/vs-code-save-hello-py-desktop.png

Command-line readings here (more in class):

Introduction to the Command Line Interface