After working through this lesson, you'll be able to:
Understand and use different data types in Lua.
Print values and variables.
In Math class, you learned that a variable is a symbol for a number that you don't know yet. In Computer Science, a variable is information that is stored in a program.
A variable name refers to a particular object.
A variable is a location in memory where a programmer can store a value.
Variables are created when first assigned.
Lua is a dynamically-typed language. This means that variables do not have types, only values do.
By default, all variables will point to nil until they are assigned a value or initialized.
Variables are case sensitive which means num, NUM, Num are different variables.
Variables start with a letter or underscore.
Variable names cannot be one of Lua's reserved words or keywords.
Examples: and, break, do, else, elseif, end, false, for, function, if, in, local, nil, not, or, repeat, return, then, true, until and while.
Generally, keep your variable names short, sweet, to the point and easy to remember because you're the one who has to use them.
And someone else (your instructor) will be looking at your code.
positive or negative numbers
a set of characters (letters or numbers) enclosed by single or double quotations
values are either true or false
non-value, to represent the absence of a useful value
allows arbitrary C data to be stored in Lua variables
represents a method that is written in Lua or C
can be used to represent arrays, sets, etc.
represents independent threads of execution and used to implement coroutines
The type function enables you to know the type of the variable.
username = "profpy"print(type(username))robux = 30000print(type(robux))
-- This is a comment
Can used on its own line or at the end of another line.
-- This is a commentname = "profpy" -- so is this
Start with two hyphens, followed by open brackets, text and closed brackets.
-- [[ Theselines are commentedout]]
The = in Lua means assigning the variable on the left to the value on the right. It does not mean equality.
robux = 100000friend = "Bernie"
In Lua, you can simultaneously assign different or the same values to different variables.
a, b, c, d, e, f = 1, 2, 3, 4, 5, 6
The print function outputs to the console. To print a string, enclose a pair of single or double quotes.
print("Hello World")print('Hello World')
You do not need quotes to print the value of a variable
x = 1print(x)
Strings are a sequence of characters that can be used to represent text.
They can be contained in a pair of single or double quotes.
Escape sequences consist of an escape character, which will always be a backslash in Lua and an identifier.
A new line
A horizontal tab
A double quote
A single quote
###is a number from 0 to 255 corresponding to an ASCII character
Concatenation is the operation joining strings end to end.
String concatenation in Lua is two dots ..
print("hello" .. "world")
Coercion is the conversion of a value of one data type to a value of another data type.
Lua provides automatic coercion between string and number values.
print("99" + 1) -- This result in 100
print(5e2) -- 5 times 10 to the 2nd powerprint(5e-2)
print(0x1) -- 1print(0x10) -- 16