Main Content

Python or Lua - which should I use / learn?

Archive - Originally posted on "The Horse's Mouth" - 2011-12-21 07:42:39 - Graham Ellis

"Should I learn and use Python or Lua" - a question to me. The questioner was perceptive, knowing that he shouldn't try to learn lots of languages at the same time, and that he should always limit the "basket" of languages used within his applications - in fact, he was wondering if he could select just a single language as his route forward.

All programming languages are different. BUT they all start from the same basic tennents ...
• A series of executable statements performed one after another
• Comment statements to allow the author to provide documentation for others coming back to the code later on
• Variables used to store information between statements
• Conditional statements or groups of statements performed only if a particular expression is true when the group (block) is reached
• Loops - blocks of statements which may be performed multiple times while a particular expression is true
• Loops and conditionals within loops and conditionals to deal with more complex cases
• Collection variables, in which you can gather a group of pieces of information under a single name.
• The ability to name blocks of code so that you can run them multiple times (with different inputs to the block too).
• Libraries of commonly used blocks of code which can be shared between programs. Many of these libraries provided with the language itself so you can easily call up common tasks such as reading keyboard or file inputs, and writing outputs.
• Storage of all of the above into disc files so you can write it once and run it may times.

The differences between the languages come in HOW they achieve the above - and how they achieve much more too. And those differences will effect what language is best for any particular task. You also need to consider:
• Licensing Issues and cost
• Knowledge of your existing team, and existing codebases
• The future availability and support of the language.

Let's look at the specifics of Lua v Python.

Lua is a small language - it has a tiny footprint which allows it to be used on almost any size of computer without grabbing a significant proportion of resources. That's fantastic news for applications such as computer gaming, where every byte and cycle can cost over thousands and (perhaps) millions of systems. But there's a sting in the tail - it's smaller because it doesn't include as many facilities and (especially) standard libraries, so that the programmer will often have to do a lot more work in writing code that a user of a larger language.

Python is a much larger language in comparison. I've been careful so say "in comparison" because it does not include the bloat that you'll find in languages like Java and Perl, leading them towards applications with yet different metrics. So in Python, you don't have to do all the low level level "roll you own" coding that you sometimes need in Lua, but that's at the price of a bigger memory footprint and somewhat slower operation ... which doesn't really matter if you're running the code on a bigger computer such as a web server, or if you're not looking for the high speed dynamic graphics expected of a game.

Both Lua and Python are open source, with good licenses that allow you to use the language free at the point of distribution. Lua has an especially open license - you can distribute it as a part of your product (the license must be included) without charge, and without making your code conform to the same open source license (i.e. it is not a "Viral" license that transfers to all code written in Lua). So - again this places Lua ideally for inclusion in games, and in other commonly and heavily distributed bits of code such as virus scanners.

When you decide to invest - and it's often a heavy investment - in writing a large code base, you'll also want to know that it will have a long life. New hardware products seem to come out every year (like new car models!), but underlying them is the same programming - or at least the same libraries. You get to realise just how important this is, and how long the life of code is, when you look at Perl, PHP and Python and their major releases - how comparability, or at least a route forward to save redevelopment, is paramount in their plans, including ongoing support for facilities that are last-millennium, pre-web. Good news - both Lua and Python have strong, and ongoing, support and because they're open source they're not as susceptible to being withdrawn as a commercial product - you aren't going to suddenly hit a wall where you can do nothing, or find yourself having to buy an expensive contract for continued support and use. I'm afraid I can't say that "Open source = never an ongoing support problem" - there are several open source languages around at the moment where new versions and facilities are commercial / much more commercial, and I see the free-to-use approach fading onto older, more limited and more difficult to use versions. I can't say "never" for Lua or Python, but I'm not worried about them - whereas I am worried about MySQL and Java.

So - back to the original question - "Should I learn Lua or Python". The questioner's involved in the writing and production of computer games, sold and supported via a web service / site. His team are already writing in Lua (for the game) and Python (using a Django framework) on the web server. Good news - from what I've been told of the game and web site, the use of two different languages seems a sensible decision in this case, and both languages are well suited to the particular use / application to which they'll be put, as described to me.

With regards to learning, I'm going to answer a question with a series of question.
"Do you want to work on the game end or the web end of the business?"
"Are you a good learner who revels in getting deep into projects and detail"
"Might you do better actually be better placed getting an overview so that you can better support your existing team of programmers?"
These are very business-specific and personal questions, and it's up to the personal answers given by the questioner to find the best answer to the original question.




I have added two comparative examples, showing a program to tackle the same simple application if Lua and Python.

[Here] is a program in Lua which reads from the keyboard, and calculates on a loop outputting the results. And [here] is the same thing programmed in Python. You'll note that even in a short program like this, the prompt-read is much shorter and cleaner in Python, but the Lua runs perfectly well.

[Here] is a program that opens and reads a file in Python, sorts the lines and outputs the sorted results. Again, it's longer in Lua - see [Here]. I was very tempted to write a more complex data analysis - in Python that would have been trivial. In Lua it would have been very practical, but I would need to spend a lot more time on a course explaining the algorithms behind it. For completeness, the data file that both these programs use may be found [Here].

We run public Python and Lua courses. Here are some links ...
Public (open) Lua classes
Public (open) Python classes
These courses run at our Melksham, Wilthshire, UK training centre where we have residential accommodation available for delegates to stay overnight during courses.

We also run private (single company) classes in both Python and Lua - either at our training centre, or on site at your facilities. You can find further details ...
Private Lua or Python courses at your own offices
Private Lua or Python courses at our training centre in Melksham
Private course can be tailored to meet you exact needs.