News

Python explained

Python is the popular programming language used on the Raspberry Pi. What makes it so special?

Python

Python

There’s a lot of focus on Python for programming on the Raspberry Pi. Is this because it’s the only way to program the Raspberry Pi?

No, not at all. As we’re generally using Linux on the Raspberry Pi, just about every major programming language can be used. C, C++, Ruby, Perl and more are all completely compatible with the Raspberry Pi. You’re not really programming the Raspberry Pi with them either – you’re creating programs that will run on the Raspberry Pi.

That’s good to know, then. If that’s the case, why concentrate on Python as much as you guys and the Raspberry Pi folk do?

It really all comes back to the core mission of the Raspberry Pi Foundation: to help educate school children in computer science. This involves coding, and Python is considered an easy language to learn. It’s a language a lot of people know anyway. Plus, we have a bit more expertise in Python on the magazine, and we’re also very happy to promote the Raspberry Pi Foundation’s objectives.

What makes it so easy to use over the other languages?

Python is ‘readable’. Core variables and functions are named plainly, the structure is designed to handle a lot of white space and everything is written a lot more straightforwardly than other programming languages. What it all means is you can quickly scan some Python code and make more sense of it than other languages, which can sometimes look like a cat took a walk across a keyboard.

So readable means anyone can just look at it and understand what’s going on?

That’s not quite what it means; you would still need a basic understanding of coding and Python to recognise what specific parts mean. These can include the difference between a tuple or a list or a class and a function. These can easily be learned, though, and are generally easier to pick up than with the other languages.

It’s easy enough to teach to children?

Absolutely. It’s very similar to maths in that you’re teaching rules and methods that you can immediately produce an answer or output from. With the correct lessons, different concepts can be introduced and built upon just like anything taught in school. In fact, the incoming changes to the UK curriculum will cover coding in this exact way.

If Python’s such a simple language, does this mean it’s not very useful in the real world of programming?

That’s not the case at all – Python is used by companies around the world in ‘real programming’. It’s not the only language, though, and some companies won’t even use Python – at the very least, it sets people up to learn the intricacies of programming languages and makes learning others a little
bit easier.

Can Python interact with other programming languages?

It largely depends on the language but the quick answer is not really. There are very few, if any, projects where you’ll need to use different languages, unless they handle completely different aspects of the software. A database could be created and maintained using SQL but that’s something Python can’t handle itself anyway.

How do I create a Python program?

Like a lot of programming, you need to create a script: a file that contains all your code and tells a Python interpreter what to do when the code is run properly. You can create them in a plain text editor like gedit or even nano on the terminal and save them as .py files before testing them. The best way is to create it in an IDE though.

What is an IDE? How do I get one on the Raspberry Pi?

An integrated development environment is software that lets you create and test scripts in specific languages. You usually have a few more ways to debug your files with them as well. In the case of Python on the Raspberry Pi, you can use IDLE which is already installed and available from
the desktop.

Wait… IDLE? Python? Is that an intentional reference?

Yep, IDLE is named after Monty Python alumni Eric Idle because everyone in tech is a nerd.

I see on the Raspberry Pi that there are multiple IDLEs. One is just called IDLE and the other is called IDLE 3. What does this mean?

The standard IDLE uses Python 2, while IDLE 3 uses Python 3. They’re both two slightly different versions of Python, with 3 having a few more and different functions. Due to the popularity of Python 2, though, it’s still prevalent in tutorials and projects and included on the Raspberry Pi.

Which one should I be using?

It’s best to stick to Python 2 and therefore the normal version of IDLE. Going from 2 to 3 is not a huge deal so when the big switchover finally occurs you’ll be in a good position to learn the changes as you go.

Will the code I create on the Raspberry Pi work elsewhere?

The code is not platform-specific, so as long as you have the same modules and files on the PC you want to transfer it to it will work just fine. Some modules may be slightly different between different types of PC, depending on what’s compatible on the Pi and PC, but 99 per cent of the time you shouldn’t have
a problem.

×