Concept: Naming and variables
Concept: Naming and variables#
In the first chapter you have seen statements of the following kind:
board = TiledBoard()
The = sign has a different function here than in mathematics.
First the right side of the statement is executed. In this case a TiledBoard() object is created.
In the second step, the created object is saved with the name ‘board’. You can always access the created object later using this
So names have a very special meaning in a programming language, they serve you as a storage for objects and data. By giving objects a name, you can access them again later. Such names are also called variables, because the object that can be accessed via the name can usually be changed in various ways.
In the simple case this can look like this:
a = 3 b = 2 c = a + b
By storing values under the names a and b, you can access them again later. In the last line,
a + b is first calculated and the result is stored in c. The result is 5.
Variables can store new values at any time - the old value is lost:
a = 3 a = 2 c = a + a
Results e.g. in 4, because the value 3 is overwritten in the second line and is lost.
The board is an object and brings along different attributes and methods,
that you can access, e.g.
Attributes are accessed with the syntax
object name.attribute name.
board.rows = 4
This code stores the value 4 in
board.rows - so the board object has 4 rows afterwards.
Methods are commands that an object can execute, e.g.
You access methods with the syntax
object name.method name(). Sometimes there are variables in the parenthesis