Menu
CoddyTech
Course

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.

  1. 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.

    Python
    1print("Hi" + ", Ada")
    Lua
    1print("Hi" .. ", Ada")
  2. 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.

    Python
    1d = {"x": None}2print("x" in d)
    Lua
    1local d = {x = nil}2print(d.x == nil)
  3. 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.

    Python
    1print(sum(range(1, 4)))
    Lua
    1local total = 02for i = 1, 3 do total = total + i end3print(total)
  4. 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.

    Python
    1def twice(x):2    return x * 23print(twice(4))
    Lua
    1local function twice(x) return x * 2 end2print(twice(4))
  5. 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.

    Python
    1a = ["a", "b", "c"]2a.pop(1)3print(",".join(a))
    Lua
    1local a = {"a", "b", "c"}2table.remove(a, 2)3print(table.concat(a, ","))
  6. 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.

    Python
    1a = [3, 1, 2]2b = sorted(a)3print(a[0])
    Lua
    1local a={3,1,2}2table.sort(a)3print(a[1])

Syllabus

10 chapters48 lessons1 project48 challenges99 quiz questions

More courses for Python developers

Learn Lua from another language

Frequently 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.

Coddy programming languages illustration

Start Lua for Python Developers for free

Get started