Lua for Python Developers
Transfer Python skills to Lua through real multi-file programs: truth rules, tables, functions, modules, errors and metatable objects. Build a stock ledger and port two Python reference programs.
Python and Lua side by side
The same program in Python and in Lua, from the lessons of this course.
Print and Comment
Python and Lua both run statements at the top level. Lua comments start with --. Strings use either quote style. Lua joins strings with ..; print separates multiple values with tabs, unlike Python spaces.
Python1print("Hi" + ", Ada")Lua1print("Hi" .. ", Ada")None and Missing Fields
Lua nil represents absence, much like Python None. But assigning nil to a table field removes that key. Assigning false keeps a key. Read a missing field to get nil, rather than a Python dictionary KeyError.
Python1d = {"x": None}2print("x" in d)Lua1local d = {x = nil}2print(d.x == nil)Ranges to Numeric for
Lua numeric for i = start, stop, step do includes the stop if reached. Python range excludes it. The default Lua step is one. Use a negative step for descending traversal.
Python1print(sum(range(1, 4)))Lua1local total = 02for i = 1, 3 do total = total + i end3print(total)Functions and Returns
Lua defines functions with function ... end. Functions are values and can be local. Parameters are local names; rebinding a numeric parameter does not change the caller. Tables passed as arguments still refer to shared objects.
Python1def twice(x):2 return x * 23print(twice(4))Lua1local function twice(x) return x * 2 end2print(twice(4))Lists as Dense Tables
Lua tables serve as both sequences and maps. Keep a sequence dense from key 1 through its length. table.insert appends by default; table.remove(t, i) shifts later elements down. Use table.concat to join strings.
Python1a = ["a", "b", "c"]2a.pop(1)3print(",".join(a))Lua1local a = {"a", "b", "c"}2table.remove(a, 2)3print(table.concat(a, ","))Sorting and Comparators
Python sorted creates a new list. Lua table.sort rearranges its input and returns no result. A custom Lua comparator returns true when its first argument must precede its second. Comparators must be strict; use a tie-breaker when output order matters.
Python1a = [3, 1, 2]2b = sorted(a)3print(a[0])Lua1local a={3,1,2}2table.sort(a)3print(a[1])
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsFirst Lua Programs
6 lessonsValues and Decisions
5 lessonsIteration and Flow
5 lessonsFunctions and Closures
5 lessonsTables as Collections
5 lessonsOrdering, Modules and Errors
5 lessonsObjects and Owned State
5 lessonsComposition and Object Lookup
6 lessonsProject: Stock Ledger
4 lessonsFinal Python Ports
2 lessonsFrequently asked questions
Who is Lua for Python Developers for?
For people who already write Python and want to learn Lua. Each lesson starts from Python you know and shows how Lua does the same thing, so you skip the beginner basics.
Do I need to know Lua already?
No. The course starts with your first Lua program. You need Python basics: variables, conditions, loops, functions and collections.
How many lessons does Lua for Python Developers have?
Lua for Python Developers contains 48 interactive lessons you can complete at your own pace.
What does Lua for Python Developers cover?
The course is organized into chapters covering: First Lua Programs, Values and Decisions, Iteration and Flow, Functions and Closures, and more.
Is Lua for Python Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.