print('Hello World!')
Hello World!
Let’s take a look at a simple program:
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:
Here is an example of code displayed in the Spyder editor:
Let’s have another look at the program. This time we will run it:
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:
If you look at the both the program and the terminal output, you will see:
Hello world!
appears in quotation marks in the program.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.
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.
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.
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:
Review computer fundamentals by following these instructions. There is nothing to write for this particular exercise; it is purely for review and building proficiency.
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:
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.
We will now deliberately introduce errors to see what happens. In the following program, the closing parenthesis is missing:
When we try to run this program, the result is:
In the following program, the quotation marks are missing:
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.
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.
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:
There are also electives (some of which can be taken by non-Computer Science majors) including, but not limited to:
Programming may be challenging.
How to succeed:
Full credit is 100 pts (complete at least two problems). There is no extra credit.
Write a program that prints the following to the console:
Hello, world.
Submit as simple_hello.py
.
Write a program that prints the following to the console:
Hello, Washington DC!
Submit as hello_dc.py
.
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
.