Learning to code is hugely popular at the moment, and Python is a great coding language to learn. Luckily for us, the Mac is a great coding platform, and Python makes it easy to learn how to code on a Mac.
In this feature, we’re going to look at setting up Python in macOS, then learning to code on this platform. The learning curve is very manageable; Python may even be installed on your Mac already. (If not, don’t worry: we’ll cover installation too.)
We focus on Python here, but for a broad overview of Mac coding and the various languages you can choose from, read our complete guide to coding and programming on a Mac.
The benefits of Python
Python is a high-level programming language that was developed in 1991. Over the past few years, with the influx of coding into schools, it has ballooned in popularity, especially in England. Why is that? What benefits does it have over other languages?
Simplicity. One of the first benefits that Python has over other languages is how simple it is. The syntax is very easy to read. You won’t spend a lot of time having to memorise lines of syntax, which allows you to focus on the basic programming concepts, and it becomes a good starting point for learning other languages.
Power. Despite being simple, Python is very powerful. It is fully extensible with the various extensions that it has available. You can achieve almost anything with Python, from applications and games to machine learning.
The community. Python also has a huge community and masses of learning material is available. No matter what problem you’re having, there will be someone out there who can help you.
Low costs. Python is free and open source, which means you don’t have to pay a penny to start using it. There are hordes of other programmers out there continually trying to improve Python, and the various editors and compilers that are available for it.
It’s good for your career. Finally, Python is one of the most in-demand programming languages (alongside JavaScript and Java). This is a great starting point if you’re looking to get into a programming position.
Install Python
Python may be installed on your Mac already: you can test this out by opening Terminal and entering python –version.
If it’s not, you should make your way to the Python website and click the download button. Don’t be put off if the version number is different to the screenshot below – it may have been updated. The version at the time of writing is 3.7.0.
Once you’ve clicked the download button, a .pkg file will begin to download. When it’s finished, navigate to the downloads folder and double-click on this file to open the installer. Follow the steps onscreen to complete the installation.
Best Python text editors
By default, Python files (ending in .py) will open in TextEdit, and this won’t do. It has a nasty habit of a writing files in non-ASCII format, which messes things up. It also lacks any decent formatting options.
So you’ll need to install a great text editor. Fortunately, there are a few available.
The first text editor that you may come across in Python is IDLE. This can be found bundled with Python when you first install it, and you will find it located in the Applications folder.
IDLE is an integrated development environment that allows you to both edit your code and see what the output will be after running it. It is the easiest editor, and the most recommended when you first get up and running. Once you’ve gained some experience you may want to move on to something more advanced.
PyCharm is possibly the most popular Python IDE out there at the moment, offering both a free and paid-for version: the latter has a wider range of features, but the freebie offers more than enough for the beginner. Features include access to plugins and web development support, as well as the usual editor offerings such as syntax highlighting.
Eclipse is an IDE that’s been around for quite a while now, offering support for a variety of different languages. To use it with Python, install Eclipse IDE for JavaScript and Web Developers, based on Eclipse Neon 4.6, then add the PyDev plugin. PyDev and Eclipse work together to create an excellent IDE for Python which is updated regularly by the hardworking Python community.
TextWrangler is a free download from the App Store. It’s a lightweight version of BBEdit but has all the functionality you need. One of the best choices for starting out.
Geddit is a popular choice amongst Linux fans, so it’s good for programmers to learn. Not as slick as other options, but very functional.
Sublime Text is a personal favourite. You can download and use it for free, although it’ll occasionally nag you into paying for it. A slick interface that’s easy on the eyes with great formatting control.
First steps and the basics
Now you have enough information about Python and what to use, it’s time to start some actual coding. With Python the majority of the commands are based around contextual words in the English language. So whereas C# would require you to type in Console.WriteLine in order to print some writing to the screen, Python just requires the simple command print. We’re going to look at 3 simple tasks that form part of the building blocks of coding. Creating an output, making a calculation, and using an if statement.
For our first task we’re going to use IDLE. It is easy to use and comes packaged with Python when you install it, so it makes sense to start out using it. Open up IDLE, by going to your Applications folder and double clicking it.
Once we have IDLE open it will open up a window called the shell. This is where all of our outputs will appear, but we aren’t going to do our coding in there. To do that we need to create a new file. We can do this by clicking File > New File in the top menu. This will open up a new code editor.
Now that you have two windows open, feel free to lay them out whichever way you feel most comfortable with. We have set ours out side by side as shown below.
Once you’ve set up your layout, click on the code editor window that opened up, and then click on File > Save As in the menu. Then save it as helloworld.py. As is customary in programming, the first program that you write will output “Hello World” to the screen.
We’re going to go to our code editor window and type in instructions that we would like our shell to carry out. The command we are going to use is the print command. This command looks like this.
print( )
Inside the brackets is where you put what you would like to be printed. So we’re going to use print to create an output to the shell, like this:
print(“Hello World”)
Once you have typed that, click File > Save from the menu to save the updated program, and then click Run > Run Module from the menu. Your output will appear then appear in the shell window. It should look something like this.
It is important to put the speech marks around any words that you would like to be printed to the shell, the reasons why are for another time, but for now just remember that words need speech marks around them just like talking in a book would. Have a practice printing different phrases to the screen to get used to it.
Our second task is to use Python to do a calculation for us. So we’re going to open up the shell again and open up a new file, just like we did before. This time we’re going to name our file Calculation.py.
This time instead of printing words, we’re going to print a calculation. We are going to add 9 and 8 together, so we need to type into our new file our new print command, which looks like this.
print(9+8)
Once we have done this we need to save and then run the program by clicking Run > Run Module. You will notice now that the shell that it prints the answer, as you can see below.
Try different calculations to get used to using the skill, remember that numbers don’t need the speech marks around them. If you are not already familiar with programming, you can use * to multiple and / to divide.
Finally, we will create one more basic program that will use something called an if statement. This allows us to do something if it meets a certain condition. Let’s open up a new file again, and write out the following syntax:
myNumber = 100 if myNumber > 50: print(“That is a high number”) else: print(“That is a low number”)
Here we are setting a variable of myNumber to 100, then we will run an if statement to check if myNumber is over 50. If it is, we print “This is a high number”, otherwise we will print “That is a low number”. Don’t forget to save and then run the program, as you did with the previous examples.
You will notice that the program prints out “This is a high number”, because our number is over 50. Feel free to change the number, and see what output you get.
Best Python training courses & learning resources
Now that you’ve got your feet wet, you might want to learn more about Python. Below, we have listed some of the best resources to get you on your way.
Udemy’s Python Bootcamp will teach you Python from the ground up. You will learn more about the Python syntax, build your own applications and games. A good way to go from beginner to professional.
Code Academy offer a fantastic range of coding courses, not just for Python but for most languages. The Python course will walk you through the basic syntax, functions, loops and even advanced topics.
Lynda offer a wide variety of tutorials and courses that will take your Python skills to the next level. Everything from Python basics, all the way to neural networks.
TutorialsPoint contains a large library of information on the Python syntax from beginner to advanced.
Learn Python the Hard Way. Don’t be intimidated by the title. This course teaches you the nuts and bolts of programming.
Think Python. This free book takes you through different aspects of the language.