Module 0.0 - Your First Program

0.0.0 - Hello, World

Let’s take a look at a simple program:

print('Hello World!')

This program is written in the Python programming language, possibly one of the smallest programs one can write, and a traditional starting point for learning programming.

We will very soon write and run this program ourselves.

For now, let’s point out a few things:

  • What we see above is the text of the program.
  • The text of the code represents instructions for the computer to run.
  • The text follows specific rules for writing in the Python programming language.
  • The entire program will appear inside of an editor when we write it.

Here is an example of code displayed in the Spyder editor:

Code displayed in the Spyder editor

  • The editor displays the text using colors. These help us read and write the program, but they don’t affect how it runs. They are for display only.
  • Writing the program and running the program are different.

Let’s have another look at the program. This time we will run it:

print('Hello World!')
Hello World!

In Python, the print function will display output in a window called a terminal. In computing, terminals are where program output (and input) are displayed as text. Here, we will show the terminal output below code blocks, when appropriate.

The print function has nothing to do with mechanical devices that apply ink to paper.

This is what it looks like in the Spyder code editor: Code output in the Sypder editor

If you look at the both the program and the terminal output, you will see:

  • The text Hello world! appears in quotation marks in the program.
  • The text Hello world! is displayed as the output.

The print function displays text at the terminal output, and we instruct the computer what text to display by putting that text in quotation marks.

Exercise 0.0.1

Exercise 0.0.1

Follow these instructions to install an editor. To check that you’ve finished, write up the program above and print out Hello World! on the terminal (console of your code editor) on your computer.

0.0.1 - The Recipe Analogy

Consider an analogy of a program as a recipe- but a recipe that you will write, and someone else will cook. The computer is the cook, and the results of the program are the meal.

Recipe Analogy

  • Before writing a program (or a recipe) we have intentions in mind for what we want the program to do (or how we want the meal to turn out).
  • You must be careful that your written instructions match your intentions.
  • Writing down the instructions alone does nothing: someone must also execute them.
  • Just as a cook needs pans, utensils, ingredients, and heat to prepare a meal a computer needs certain resources to execute a program.
  • Unlike a cook, a computer will not try to interpret your intentions. The computer will do exactly what you tell it to do.

0.0.2 - Computer Fundamentals

There are computer fundamentals that you will need to understand before learning to program. Most of these concepts are very straightforward, and you are probably familiar with many of them already.

Examples are:

  • Navigating directory structures
  • Locating, copying, and moving files
  • Using a text editor
  • Uploading and downloading files
  • Handling the quirks of an operating system

Exercise 0.0.2

Exercise 0.0.2

Review computer fundamentals by following these instructions. There is nothing to write for this particular exercise; it is purely for review and building proficiency.

0.0.3 - Variations on a Theme

We will now modify the initial program slightly, and discuss how the changes to the program result in changes to the output.

Consider this program:

print('Hello World!')
print('My name is Carl')

Exercise 0.0.3

Exercise 0.0.3

Type up the above program, replacing Carl’s name with your name. Save the file as myname.py. It is good practice to always save programs when you write them.

0.0.4 - When Things Go Wrong

We will now deliberately introduce errors to see what happens. In the following program, the closing parenthesis is missing:

print('Hello World'

When we try to run this program, the result is:

In the following program, the quotation marks are missing:

print(Hello World)

The error is similar, but not the same:

Both programs yield a syntax error, which is Python’s way of telling us, the programmer, that the way we have written our code doesn’t follow the programming language’s rules.

These error messages can help us to fix the problems with our code. As we learn more about how to correctly write Python code (the “syntax”), we will also learn more about how to read these error messages.

Exercise 0.0.4

Exercise 0.0.4

Confirm that you get the errors above by typing up these programs as error_1.py and error_2.py.

Webster (1913) defines “syntax” as:

That part of grammar which treats of the construction of sentences; the due arrangement of words in sentences in their necessary relations, according to established usage in any language.

Natural languages (such as English) and programming languages both have syntax. Just as putting words in the wrong order in English can change the meaning of a sentence, incorrect syntax in Python can change what your program does.

0.0.5 - Computer Science: Beyond Programming

Computer science and programming overlap, but there are many parts of computer science that are not programming at all!

A computer science degree typically features areas such as:

  • Programming embedded devices
  • Understanding the hardware underlying computing
  • Algorithms (the mathematics of computer science)
  • Fundamental limitations of computing
  • A deeper understanding of how computer systems work together

There are also electives (some of which can be taken by non-Computer Science majors) including, but not limited to:

  • Machine learning, artificial intelligence, robotics, computer vision, natural language processing
  • Design of interfaces
  • Graphics, animations, visualizations
  • Digital humanities, computing for arts and social sciences
  • Scientific computing
  • Computational biology, chemistry, physics, economics…
  • Educational tools, health data
  • Medical devices
  • Control of mechanical systems
  • Cyber security

0.0.6 - How To Succeed In This Course

Programming may be challenging.

  • If you haven’t programmed before, the paradigm will take getting used to.
  • The skills involved require a lot of practice.
  • You may need to brush up on computer fundamentals
  • It’s easy to overwhelmed: you’re learning a new language!
  • It’s often easy to read a concept and “understand” an example that is much harder to implement by yourself from scratch.

How to succeed:

  • Make progress every day. Everything in this course builds on previous lessons. Don’t lose “muscle memory”
  • Attempt every exercise and work through the problems yourself.
  • Ask questions early and often. Come to office hours. The course staff are here to help.

End-Of-Module Problems

Full credit is 100 pts (complete at least two problems). There is no extra credit.

Problem 0.0.1 (50 pts)

Problem 0.0.1 (50 pts)

Write a program that prints the following to the console:

Hello, world.

Submit as simple_hello.py.

Problem 0.0.2 (50 pts)

Problem 0.0.2 (50 pts)

Write a program that prints the following to the console:

Hello, Washington DC!

Submit as hello_dc.py.

Problem 0.0.3 (50 pts)

Problem 0.0.3 (50 pts)

Write a program that prints the following to the console:

Hello, Washington DC!
My name is Otis.

(Replace “Otis” with your name.)

Submit as hello_name.py.