I want to code, where do I start? (Mac Edition)
So you want to enter the mysterious and wonderful world of coding. You want to enjoy the ability to create anything you can think of with the click of a few buttons. Maybe hack your way into any source of information to gain insight on what the government isn’t telling you. You could even create program that makes millions off the stock market. Well I am here to tell you that it is nothing like that. The beginning steps to coding are small, simple, and may be confusing. That is why I am providing this prologue to your coding experience. Hopefully get you to start with coding and not with a headache.
What is a coding language and do I need to practice my Spanish?
Learning a coding language is like learning French, Spanish, or any other spoken language except for the fact that they are all in English. This is a bit confusing at first but the main idea here is you need to learn the keywords that are used most often and the the syntax that allows you to make them work. To give an example of an essential part of coding along with its syntax, I am going show how to display text as the output of your code. The keyword to do so is print, and the syntax of how that is actually used is below.
As you can see we have the keyword print followed by the text that we want to be outputted (printed) by the print statement. One thing to clarify is we are not printing a piece of paper with the text on it. We are simply displaying this text as the output of the code.
This is how all of coding languages work and you should get familiar with this process. You need to perform a task, you find the keyword that performs that task, and then you follow the syntax to be able to use it in your code. Depending on the language that you choose the syntax will be very different, so be weary because some are much more verbose than others.
How to choose your language
Unlike Neo, you have more choices than just red or blue. There are tens if not hundreds of languages that all have their pros and cons, but how do you choose? If you already have one in mind, that’s great, but you should still take the following into consideration before you are hard set on that. There are many different applications of coding like website development, data science, game development, network administration, software development, database administrator, and so many more. To help with the decision look at this infographic or this article to help you pair your ideas with a compatible language. One thing to note here is that most well know languages can perform the tasks that you want, its just some are better than others for certain applications. Once you have found your language you are ready to start coding… well not yet, just a couple more things. For this tutorial I am going to use Python because it’s one of the easiest to read, easiest to start coding, and has so many applications (its also great for jobs). Another great perk of using Python on the Mac operating system is that it already comes preinstalled and you won’t have to deal with any wonky downloads.
Once you have identified your language you have to identify how you are going to get pen to paper (rather fingers to keyboard), and start writing some code. This may seem like a simple thing but with almost everything in coding, there are more ways to do that than you could imagine. The easiest way to start writing some code and seeing the output is at a website like this. That is an online coding environment so you won’t be able to save any of your work or combine files to together in a larger project. That website is good because you can get familiar with the most basic of coding like printing, assigning values to variables, and basic if-else statements. As you can see the holy grail of starter code, the hello world example, is already provided.
The magic of the command line
If you are looking to code on your computer, the command line (also named the terminal) might be for you. Go into your applications folder and click on the black box called ‘Terminal’. This may seem very scary at first but I am going to walk you through it, and after about 5 minutes you should have a good understanding of what terminal is.
Python on the mac terminal is very easy to use and you can start coding by typing python3 (Python3 is just the newest version of Python).
This method of coding is very simple and good for the very basics. You cannot really write anything longer than a few lines because if you make any mistakes you have to rewrite the whole thing. Moving past that you are going to need a better idea of how to navigate your computer using the command line. If you open up Finder and look at the left sidebar, you should see applications, documents, desktop, and a couple other things. Now go into your terminal and type “ls”, and you should see a similar list of items as to what you saw before in Finder. This is no coincidence, they are the same thing! The reason that terminal is important for coding is it allows you to open and manipulate files and folders a lot faster than what you could do going through Finder. Rather than clicking through 5 folders and then opening a file manually you can go right to the location of the file with one line of command line code. Here are the only things that you will need for terminal to get you started.
Typing “ls” (stands for list files) will list all items in the directory (folder) that you currently in.
Typing “cd” stands for change directory, and it will change the directory (folder) that you are in to the one that you put after cd.
Typing ‘touch’ will create a new file with the name of the text you provide after touch.
Now that you have a python file created (in this case example.py) on the desktop you can edit it in many different ways. Vi, Vim, Nano, Emacs, and many more editors can be launched straight from command line. They do require knowledge of the keystroke commands that allow you to access most functionality but once you have the basics down they can be very useful. I would not recommend these for now as I believe text editors discussed in the next section are much more intuitive for beginners.
Text editors == Microsoft Word (for coding)
If you do not want to deal with terminal but still want to write some code on your computer you can dive into the world of text editors or integrated development environments (IDE). A text editor is like the Microsoft Word of coding in that it is a place where you write your code down and save it to your computer. They are pretty bare bones in that they do not add any major functionality or tools but it does allow you see your entire code file and also save and edit. The one difference between text editors and terminal coding that might not seem helpful at first is the ability to differentiate parts of the code with the color of the text. In the print example you saw when choosing a language you can see that the text is a different color from the print, and this is done to be able to look at code and understand it faster and easier. Text editors that I would recommend are Atom (designed specifically for mac), Sublime Text, and Notepad++.
So once you write some code in a file using the text editor, save that file somewhere on your computer. You go to your terminal, navigate to the location where your file is, and then type python3 name_of_file.py and it should run that code (replace name_of_file.py with the name of your file).
IDE’s are where you are going to get into the super user territory of coding. This is a much more advanced tool but these will allow build automation, debugging, testing, and many more tools to get a better understanding of what is happening in your code. You should look into these after you have gotten the basics of coding mastered and you start building bigger and bigger projects.
So now that you have all that down you should be able to code to your hearts content. Just to recap here are the steps that you should follow:
Step-by-Step guide to start coding
- Choose your language (make sure it’s downloaded to your machine)
- Create a file with the correct file type (according to your language)
- Write some code
- Run that file from command line
BOOM, you’re a coder. You have also made it past one of the hardest parts of coding. Now that you can write code you should explore the wondrous world of Python and see all that is possible. Here are a few links to some great resources for learning about Python.
- This link is a massive source of information about everything python. It is really comprehensive and can find almost anything Python.
- This is another great source of information coming straight from the people behind Python.
- This is a great resource to get your feet off the ground and practice beginner Python.