Tutorial: Your first steps
in programming!

To code could seem intimidating, but the concept is actually very basic: to tell your computer what to do, using its language!

With this simple tutorial, you’ll write your first script with the language Python and you’ll run it! No need for preliminary knowledge. The goal is simply to show you the amazing potential of programming!

There are many programming languages. Python is just one of them, which was created in the late 80’s by the Dutch programmer Guido Van Rossum, who worked for Google and Dropbox, among others.

If you have a Mac, Python is already installed. If you have a PC, it’s possible that you need to install Python yourself. I used Python 2.7 for this tutorial.

To know if Python is on your system or not, open the Terminal application (on a Mac) or the Command Prompt (with Windows). Type python and then press Enter. Here the result on my computer.

python version

By the way, it’s this window that we will open to run our script at the end of this tutorial. But close it for the moment.

#1 Text Editor

A script is just a text file. Therefore, if you want, you could code your program with TextEdit (if you have a Mac) or the NotePad (if you have a PC). However, as I wrote it in a previous post, I suggest you use Sublime Text 2. This small software knows Python and will color your functions and elements in your code. It’ll be clearer and easier to read.

Once the software installed, we are ready! Open a new file. In the bottom right corner of the window, it’s probably written “Plain Text”. Click on it and change it for Python.


Here is the program we will code today. The computer will ask several questions to the user. He will also play a little game with him. The complete script is below.

And here is the script running. Don’t hesitate to press pause to be able to notice each step! I will explain everything in detail below.

#II Libraries

Libraries are a special set of functions and you need to load them before using them. Since your computer will read your script line after line, from top to bottom, you have to start your script with the libraries needed.

Here, we will use two libraries. The first one will allow us to work with hours and years. The second will let us create random numbers. You will understand their purpose later. For the moment, copy these two lines in your text editor.

#III Interactions with users

In Python, as for many languages, there is a specific function to display text on the screen. As written below, our program starts by saying hello to the user.

We can also ask the user to type something and store this information in a variable. It’s exactly what we do on ligne 7. We ask the user his name and we save it under the variable name.

When we have variables with data in them, we can work with them! On line 9, our script takes the variable name, which contains the user’s name as it was typed, and puts it into a sentence!

Have you copied all the lines so far? Good! On to the next step!

#IV Mathematical operations

Nothing’s easier for your computer than playing with numbers!

On line 12, we start by asking the user his birth year. We put the information into the variable year.

On line 14, we use the function datetime.now().year to get the current year (that’s why we need the datetime library at the beginning!). We subtract the current year by the year given by the user, which gives us his age! The result is stored into the variable age. (By the way, we pass year into int() to transform the text into a number because, when the user gave us his birth year, it was some text. But you need numbers to do mathematical operations!)

On line 16, we take the variable age. It’s a number, so we pass it through str() to convert it into text, then we use it into a sentence that will be displayed on the screen.

Be sure to copy the code exactly as shown. Programming is a very precise art!

#V Conditional statements

With a script, you can ask your computer to act differently depending on conditions imposed by yourself.

From line 18 to 23, our script will act differently depending on the user’s age.

If variable age is greater than 25 (condition indicated on line 18), the computer will act as told on line 19 and will ignore the four following lines.

However, if variable age is smaller than 25 (condition indicated on line 20), the computer will act as told on line 21 and will ignore the two following lines.

If the variable age is not greater or smaller than 25, it means that it’s equal to 25! In this case, the program will run what we ask of him on line 23!

(You probably noticed the tabulations at the beginning of specific lines. For some elements, like conditional statements, you need them. It’s very important! Otherwise your script won’t run!)

#VI Loops

Loops are extremely important for programmers. They allow them to repeat the same action a great number of times, with few code lines.

From line 26 to 36, the computer will play a small game with the user.

We start by explaining the rules to the user. Then we start a five time loop, on line 28.

Then, we create the variable python_number, in which we store a random integer between 0 and 10, with the function random.randint(0, 10) (Do you remember the random library at the beginning? It’s for this function that we need it!).

Next, we ask the user to choose a number as well and we keep the number under the variable your_number.

The four last lines are very simple: we compare the variable python_number with the variable your_number. If they are identical, the user has guessed the number chosen by the computer! If they are different, he looses.

Since lines 30 to 36 are inside a loop that will repeat itself five times, the computer will read them five times consecutively. All the informations stored into the variables will be over-written by new values each time.

#VII Run the script!

Voilà! Our small program is done. We can now say goodbye to the user. We use the function datetime.now().hour and datetime.now().minute to have a good excuse to leave. It will display the current time.

Do you remember the variable name, on line 7? It still exists! And the user’s name is still stored in it! We use it again here.

When arrived at the last line of your script, the computer will automatically end your program.

Now, save your script and check that it’s identical to mine. Be careful! Each character is essential!

Then, open your Terminal (Mac) or your Prompt Command (Windows). Type python to tell your computer which language you’re using, then indicate the path to your script (you also can drop your script directly in the window).

It should look similar to this (but with a different path for your file of course!):

Terminal exemple

Now press Enter! Here we go! Your script is running! Interact with you program! Repeat the experiment several times to test different answers to the questions.

(If you have error messages, it’s probably because there’re typos in your code. Go back to the beginning, copy-paste my whole script and try again.)

#VIII Conclusion

Congratulations! You coded your first script!

Of course, it’s a very simple (and not very useful) program. But imagine the potential for a reporter!

You could write a script to search a specific name into hundreds of pdf files. No need to open them one by one. No need to read them. The script would do it for you in few seconds!

You could also write your own script to automatically receive emails when the police in your area sends a press release with specific keywords.

The possibilities are almost infinite!

If you want to learn more, follow me on Facebook, Twitter or LinkedIn. You can also subscribe to my website and receive an email when a new post is published. (To subscribe, go to the top and click on the menu symbol)

You can also learn the basis of several programming languages with the excellent Codecademy website. I followed their Python, HTML and CSS classes. I will start the Javascript ones soon too. It’s really well done, and you can do them at your own pace!

3 thoughts on “Tutorial: Your first steps
in programming!

  1. Pingback: How to code a robot reporter

  2. J Cothron

    This looks like a clear, simple tutorial–just my speed–and useful for beginners in computer programming. I would appreciate it if you could let me know when you have a new post. Thanks very much.


Leave a Reply

Your email address will not be published. Required fields are marked *