Variables Assignments Datatypes

From PZwiki
Jump to navigation Jump to search
PZwiki:Language policy Language: English

Welcome to your first tutorial!

For SciTE users: To run code, choose 'save as' from the file menu and save a file with the extension '.lua'. The play 'Run Program' button should then become available to use. Any output from your code will be displayed in the box at the bottom (if this is missing, press F8 to bring it up).

Hello World

In Lua this is easily done:

 print("Hello World!")

print is a built-in Lua function. Functions can be identified by the parentheses, these things: (). Anything between the parentheses are called arguments. In this case, we passed it the string 'Hello World!' which it took and printed in the console. Note that Lua is case-sensitive, so print() is not same as Print()
Also note, that unlike many other languages, we do not need a semicolon at the end of each line (although you can still use semicolons with no ill effect!).

Variables and Assignment

Variables are basicly named storage containers. You can put any value inside a variable, then refer to it easily by using the variables name. Lets have a look at the Hello World example to illustrate this:

a = "Hello World!"

Here, we placed the string 'Hello World' inside the variable a, then printed a. This should have produced the same results as before. Before we put the string into the variable a, it was empty (contained nil). By assigning it value, we intialised it. Note we used the single equals sign, =, to assign the string to the variable. This is basically a command to make the item on the left to be equal to that on the right. We can place practically anything inside a variable:

a = print
a("Hello World!")

b = "This works too."

We can also assign multiple variables at once:

a, b, c = "This is called", "multiple", "assignment!"
print(a, b, c)

Note if we have more values that variables, the extra variables are discarded. Liekwise, any extra variables remain unchanged (i.e. containing nil in the example below).

a, b, c = "Data ", "missing"
print(a, b, c)

Multiple assignment is particuary useful for swapping variables around with having to use a third variable to hold data on the transition:

a, b = 1, 2
print(a, b)
a, b = b, a
print(a, b)

Note there are some rules on what variable names can be. It cannot contain anything other than letters, underscores or digits and it must start with an underscore or letter (i.e. not a digit). It also cannot be any of Lua's keywords (such as and, or, function, local etc.), but we will go into these more later.


So far I've mentioned 'strings' and 'functions', these are both examples of different datatypes found in Lua. Datatypes are the classifications of a value. A useful function to discover the datatype of a vaue is type(). In order to see the result of this, we also need to use print(). Try some of these examples:


You probably guessed it, the main ones are:

  • numbers - any digit, including zero
  • nil - the absense of value
  • function - a function, like print() or type()
  • string - a sequence of characters
  • boolean - true or false
  • table - a list of values (covered in a later tutorial)

Other types include userdata and thread, but these won't be needed at this stage.

To determine a string we use quotes, either "double" or 'single'. As a special case we can also use double square brackets: [[ ]]

Characters inside square brackets are treated literally, so backslash escaping (more on this in another tutorial!) doesn't work, and a new line is determined by simply using an actual new line. See below for more clarification:

print("A string")
print("A 'string'")
print([["Square brackets" have
unique properties, that can
very useful later. You also cannot
use 'escaping': \n, \a \\]])

Using quotes will suffice for now when using strings, but it is worth remembering square brackets if you want to write a long string with separate lines, or your strings contain backslashes you just want to print.


  • Lua is case sensitive
  • print() can be used to print values to the console
  • type() can be used to discover the datatype of a value
  • Variables are containers which can be assigned pretty much any value
  • You can do multiple assignments on one line
  • Variables without assigned values are not initialised and contain nil
  • There are eight datatypes: nil, number, string, function, table, boolean, userdata and thread

Test Yourself

1) Swap the following variables around without using multiple assignment:

a = "A"
b = "B"

2) This code contains errors, identify and fix them:

Hello = a 
b = Print
c = type

3) Without running it, what would the outcome of this code?

a = type(type(1))