Documentation > Python
Hello World!
Let's start with the traditional 'Hello World!' example.
What will you learn?
You will write your first Python program and learn how to run it nControl.
You will understand the basic structure of a statement in Python.
What will your program do?
It will customize the behavior of a tile.
It will print the text Hello World! every time you press it.
What will you need?
nControl 2018.0 or higher.
Writing the Program
nControl is designed to control train layouts using on screen tiles. The behavior of each tile can be fully customized with Python scripts. Let's start by programming a generic tile to print the text Hello World! on our screen.
Start the nControl software. Then go to Configuration Mode, create a new generic tile and open the code editor.


In the editor, type the text self.print('Hello World!').


Close the editor and the Edit Generic Tile Properties dialog box. You should now see a generic tile in the tile layout.
Running the Program
To run the program, activate Simulation Mode and click on the tile Generic 1 to execute the hello world program. If all goes well, the console window should appear. Each time you click the tile it will add a line with the text Hello World!


Understanding the Code
Congratulations, you just ran your first Python program! But what does the code of your program really mean?
Python is an 'object oriented' language. In object oriented programming the source code is organized using 'objects'; for example, in nControl the generic tile is programmed as an object. Objects can store data and perform operations and they allow you to quickly build new thing using what you already have, rather than having to start from scratch.
In this example, we wanted to print Hello World! in the console window. The generic tile has a function to do that; the print function. The print function has one argument, being the text that has to be shown in the console window. In Python, the function arguments are written between parenthesis just behind the function name. In our case the argument was a text; you specify that by putting it between quotes. So that explains the print('Hello World!') part of the code.
But why did we put self. in front? When you call a function, you have to specify which object you want to use. In Python the self keyword1 specifies the object that you are currently working in. As we were working in the (code editor of the) generic tile, we can use self to refer to the generic tile. The '.' after self specifies that what follows is a property or function of that object.
So in plain English self.print('Hello World!') means: ask the generic tile (= self) to process the text Hello World! with it's print function.
What should you remember?
The most important thing to remember from this example is the basic structure of the code we wrote:
   self.print('Hello World!')
or in an abstract way:
   object.function(data)
The three components of this statement are:
  • object: specifies who has to do something: in this example it was the tile we were programming in represented by self.
  • function: specifies what task has to be performed; it this example is was printing text on the screen with the print function.
  • data: specifies the information that has to be used to execute the requested task; in this example it was the text Hello World!
Whenever you want to get something done by your program, you have to specify who has to do it (= object) and what has to be done (= function). In most case you also have to provide additional information about the job that has to be done (= data). It's very similar to asking a question in real life; for example you ask someone (= object) to pick you up (= function) at a particular time and place (= data).
It's important to understand this structure because you'll need it to formulate all your requests in Python code.

1Technically speaking self is not a reserved keyword in Python; it's a convention. You can use any name to point to the instance of the class, however, it's strongly advised to use self for that and not for anything else. nControl has been implemented using self, so in nControl you have to use self to point to the object you're working in.
Documentation > Python > Lesson 2