Introduction: Writing Your First Computer Program

Why Programming?

Computer programming or “coding” seems very intimidating. You might not think that you don’t know enough about computers and dread the idea of troubleshooting problems that pop up on your own personal laptop. If you believe that your stilted relationship with computers disqualifies you from learning computer programming skills you are wrong. You might think that you have to “be good at computers” but quite a few programmers also struggle with simple tasks like figuring out why your computer won’t seem to print a document. The truth is, you don’t have to be an expert to be good at computer programming.

Computer programming is much easier than it seems and can lead to a rewarding and high paying career. in this CNBC article by Courtney Connley entitled “The 20 best jobs in America in 2020” five of the top ten jobs were programming jobs. Here we’ll get you started with your very first computer program.

Supplies

  • Computer
  • An internet connection

Step 1: Pick a Programming Language

Computer programs are simply a set of instructions given to a computer one at a time. To the computer these instructions ultimately are just a bunch of ones and zeros or binary. Since humans aren’t good at speaking binary, programmers use a variety of human friendly computer languages to write these instructions. These languages have names like C (pronounced like the letter ‘C’), C++ (pronounced as-see plus plus), Java, JavaScript (no relation to Java), Go, Rust and Python. Each of these languages brings its own advantages and once you start feeling comfortable writing programs it becomes easier to learn a new one.

In this example we will be using Python. It is easy to use, easy to learn and is in high demand.

Step 2: Download Python

In order for you to run the Python program you will need to have Python installed on your system. Python is free and can be downloaded at https://www.python.org/downloads/ . On that site, click on the yellow “Download Python 3.8.3” button to download .

NOTE: The number 3.8.3 may be different as this button will download the most current version.

Step 3: Install Python

Run the downloaded file.

On the first screen of the installer, make sure the box next to “Add Python 3.8 to PATH” has a checkmark in it, if not click the box so one will appear then click the top “Install Now” option.

As Python installs on your system, a progress bar will appear. Wait patiently, it should only take a few moments to install.

When finished you’ll see a screen stating the setup was successful. Click close and the installation is complete.

Step 4: Open Notepad

Programmers will often use an IDE (Integrated Development Environment) to write all of their programs. An IDE usually comes with tools that will highlight sections of the program and catch typos for the programmer as she writes her instructions. For complicated programs an IDE can really help. There are great free IDEs available but they aren’t required. Computer programs can be written in just about any text editor you can think of, in fact we will be writing our program in Notepad. Notepad is installed by default on all Windows operating systems and will do just fine.

Open notepad by clicking the Start Menu and by typing on your keyboard “notepad” and clicking on it once found by the system.

When open, you'll find a blank text file.

Step 5: Define a Function

It’s time for you to write your program! You can hardly call yourself a programmer without writing a “hello world” program. This is a coding tradition!

When Python executes your program it will read instructions, one line at a time from the start of the program to the end of the program. Your first order of business is to define a function. The most basic way to describe a function is as a named group of instructions which can be reused whenever we call it by name. We’ll call our function hello_world.

To define a function we need to use the “def” keyword, give it a name, a set of parentheses and end the line with a colon so your function will start like this:

def hello_world():

On the next line you’ll give this function its group of instructions. In this case your group will be very small, just one instruction. Python keeps track of what belongs to the function by checking for indentation. So to tell it this instruction is part of the function, we will press “Tab” on our keyboard then give it the instruction print(“Hello World!”)

def hello_world():
	print(“Hello World!”)

Step 6: Create an Entry Point for Your Program

At this point you have written a function but you haven’t told the computer to execute that function anywhere. You’ll do this in this step. To call our “hello_world” function on a new line you just call it by name. Type the following, without a leading tab:

hello_world()

No need to give the “def” keyword because you aren’t defining anything. There’s also no need to put a colon because you aren’t telling the computer what this function will do when it is called, you’ve already done that.

Your program now looks like this:

def hello_world():
	print(“Hello World!”)
hello_world()

It seems silly but it is worth repeating: The first two lines define the function, the last line calls that function.

Step 7: Saving the File

That's it, you’ve written a whole program! Give yourself a pat on the back. You can say to anyone who will listen “Computer programming isn’t that hard! I’ve written a program before.” You will be completely right! But you aren’t done yet. Now that you have written instructions for the computer it’s time to watch the computer run those instructions.

In order to do that, you need to save the program you just wrote. Click the “File” menu and select save. When the prompt appears, select your Desktop folder as the location to save the file. In the “Save as type” field, select “All Files (*.*)” and name the file hello.py.

Saving your file here will make it easier to locate when we are trying to run the program.

Step 8: Running the File

The way this program is written, it needs to be executed in the command prompt. Open it up by clicking the Windows Start Menu and typing on your keyboard “cmd” and pressing enter.

Now navigate to the location you saved your program, the Desktop folder, by typing “cd Desktop” and press enter.
Then tell the computer to use python to run your program by typing “py” followed by a space and the name of your program.

Now press enter and you have successfully executed your program!

See what it did? It executed your program, called your function and printed the text “Hello World!” on its own line.

Step 9: Go a Little Further

At this point you're a programmer (or coder, whatever you’d like to call yourself!) Now take it one step further. Maybe add a few more print(“”) instructions in your function, just make sure to give it the same indentation as your other one and put whatever text you want inside the quotation marks. Maybe call the function a few extra times by typing the hello_world() statements on their own lines below where you typed the last one. Just make sure you save the file before running it again!

Step 10: Where to Go From Here

Great! You’ve written a program. Hopefully you see how fun and easy it can be. Now what? There are so many cool things that you can instruct your computer to do that we haven’t covered here. You can make the computer do something if some condition is met by using “if” statements. You can make the computer do something over and over again by using “loop” statements. You can combine the two in an infinite number of ways. You can store data in variables to be used at a later time. Each of these concepts are easy to pick up. There are a million free resources to learn from, including Instructables. When I first started I learned from a site called www.codecademy.com which offers free coding courses in many programming languages including Python and I would highly recommend them.