LinMao's Blog

LUA Basics

A quick note of LUA for later reference.

Lua is a powerful and intuitive general-purpose programming language developed in 1993 by a team of scrappy professors at Pontifical Catholic University of Rio de Janeiro, in Brazil.

Much of the inspiration for Lua came from the languages SOL (Simple Object Language) and DEL (data-entry language). In Portuguese, Sol means “sun” and Lua means “moon”.

Data Types

Basic Data Types

Data Types Definition Syntax Example of Use
Number A numeric value including positive values, negative values, and decimal values. 103.5-4 To store how many followers you have on Instagram.
String A sequence of individual characters inside quotations. It can be letters, spaces, numbers, or symbols. "This is a string"
'I have 5 cats!'
To store your username on a website.
A value that only has two possible values: true or false. true
To indicate whether you have dark mode on or not.
Nil A representation for the absence of a value. If there is no value, it is nil. nil To indicate an empty box on a fillable form.

Note: There are also an additional four complex data types: Tables, Functions, Userdata, and Threads.

type(): get the data type of a variable.

Dynamic data type: Since variables in Lua do not have a type — it is just a container for data — you can reassign a value of any data type to a variable.

Type Convert

  • tostring() : converts a variable to string
  • tonumber(): converts a variable to number


Arithmetic Operators

Operator Name Description Syntax Example Result
Addition Adds two numbers x + y 5 + 2 7
Subtraction Subtracts two numbers x – y 5 – 2 3
Multiplication Multiplies two numbers x * y 5 * 2 10
Division Divides two numbers x / y 5 / 2 2.5
Exponential Takes the exponent of two numbers x ^ y 5 ^ 2 25
(also known as modulo or modulus)
Gives us the remaining leftover number after we’ve divided two numbers. x % y 5 % 2 1
Negation Reverses the sign value of the number. -x -(3+2) -5

Logical Operators

Operator Syntax Description
and A and B true if values A and B are true, false otherwise.
or A or B true if at least one of A or B are true, false otherwise.
not not A true if A is false and false if A is true.

Order of Operations

The list below shows what operators are evaluated first from top to bottom:

  • ^
  • not
  • *, /
  • +, -
  • <, >, <=, >=, ~=, ==
  • and
  • or

Like other programming language, LUA can use parentheses () to change the order of operations.

String Concatenation

  • concatenation operator (..).



  • single line comment:

  • Code block comment:


If statement

  • Comparison Operatoers, others are the same as Python or C/C++ except for: A ~= B: A doesn’t Equal B.

If-else statement

If-elseif statement


Declare functions

  • Declare a function by using function keyword

  • Declare a function with parameters and call the function with arguments (parameter is in the definition while argument is real value)

  • Declare a function with return

Built-in functions

Run Lua on ubuntu terminal

Install LUA

Run LUA script

Some thoughs

LUA is quite similar to Python, I can get a baisc command of LUA with one hour because of the experience of Python programming. Although there're some small differences: 1, comment syntax; 2, not equal operator (~=); 3, type convert and string concatenation, 4, don't distinguish int and float; 5, no need to import built-in module, etc. Other than those small differences, I guess a Python programmer can easily have a good command of LUA within a short time, some Python codes are even able to directly run with LUA without any change.


LUA Getting started

Lua 5.4 Reference Manual

赞(0) 打赏
转载请注明出处:LinMao's Blog(林茂的博客) » LUA Basics

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

LinMao's Blog(林茂的博客)