Table of Contents
Welcome to another article in our Intro to Programming series. Now that we installed Python, it's time to start writing some computer code, and as we do that, I want to show you several ways you can write and run Python code.
At a very high level, programming has two parts. One is writing a program, and the second is running it. I'll show you several ways you can write code and several ways you can run code. In the end, you can choose whichever one you like best. In the real world, all of these different ways are used, in different contexts, so while it's perfectly okay right now to select one way and stick with it, it is worth getting familiar with all of them.
When it comes to Python programming, we have the following types of "environments" for writing code:
- text editors, such as Sublime, Atom and Visual Studio Code
- the Python command line
- so-called "notebooks" (e.g. Jupyter, Google Colab)
We will cover these in more detail in just a moment. Before we do that though, let's talk briefly about the options we have for running code. I'm going to talk about the following ways: scripts, the Python command line and Jupyter notebooks.
The first thing to remember here is that how you run code depends on how you wrote it. For example, if you write code in an editor, the way you typically run it is by using the command line. The Python command line and Jupyter notebooks allow us to write and run code in the same place.
Let's first talk about text editors. There are a lot of text editors out there, but the ones we're talking about are specifically designed for writing computer code and have some nice built-in features that make writing code a lot easier such as the Python code comments. We won't go into too much detail about these features, since they can differ a bit from one to the next but, at a minimum, you can expect the following:
- syntax highlighting
- keyword search
- project exploration
Another great feature of modern code editors is that they have built-in support for a variety of programming languages. This is great because, if you want to learn a different programming language, you won't have to also learn how to use a different editor.
In terms of editors, I would recommend Sublime, Atom and Visual Studio Code. I use mostly Emacs but I would not recommend it for a beginner, as its learning curve is fairly steep. Generally, all good code editors can be customized with plug-ins to match your work requirements and programming style.
You may want to spend some time exploring a variety of text editors before you choose one to make your own. As a programmer, you will spend most of your time in your editor of choice, so it is important that you know it very well and that you customize it (over time) to match your workflows. Constantly switching editors can be counter-productive in the long run.
Though I use Emacs for my own programming, for this article I will be using Atom as my editor, as it is easier to understand. Please feel free to follow along in your own editor.
Note: One very important thing to always remember about Python programs is that the files where you write your code should be saved with the .py extension. Another good practice when naming files that contain computer code is to avoid using spaces. This is just so that it is easy to type them on the command line. This might not make a lot of sense right now, but trust me - it's best to avoid using spaces when naming a file that has computer code. If you need to separate words in the name, it is best to use underscores. So, for example, a good file name would be hello_world.py.
Our first program
Finally, in order to show you how to write and run a program, we need to pick a program to write. Now, believe it or not, the program that is often used to introduce beginners to a programming language is a program that simply outputs the words "hello world". According to Wikipedia, the tradition of using this phrase dates back to an influential book that was published in the 1970s - the book was about another programming language, called C.
We will abide by this tradition and we'll go ahead and write the Python code for a program that would print the phrase "hello world". Now, in Python, as you can see here, we achieve this task by writing:
Ok, so let's write some code. We'll start by opening our editor. I'm using Atom, but again, you can use Sublime or any other editor that is suitable for writing computer code. When it opens, Atom shows us this window with a number of options and a welcome message. You can close these windows by moving your mouse over the tab at the top of the window panes and clicking the blue x button.
Now, let's click on the File menu and select New File from it. We should see a new window open and this is where we can write our code. So let's just type our program:
When we are finished, we can save the file by going to File > Save. I'll save this in my Projects/i2p folder. Now, you might not have an i2p folder or directory. I strongly recommend that you keep all your code organized by creating different folders for each project. In this case, I went ahead and created a Projects folder in my home directory, and inside it, an i2p folder - which is short for introduction to programming. It might be easier to follow along if you create similar folders on your computer, but it's not necessary. You can save your code wherever you want, as long as you can find it easily.
I'm going to save this file as hello_world.py. An interesting thing happened - we have colors now!
Definitely looks a lot better than before. These colors are hints about what each word represents. This feature is called syntax highlighting. For example, the word print, as we'll learn later, is a function, so in my Atom editor it's blue. Having these words highlighted makes it easier to read and understand code, so this syntax highlighting feature is one of the features that make text editors specialized for writing code so much nicer to use when programming.
NOTE: Many code editors use the extension at the end of the file name to determine what programming language you are using. This is why, for Python, it is important to append the .py extension at the end of the file name. Without it, Atom won't automatically know that you're using Python, so you won't get the syntax highlighting.
Great, so now we wrote our first program and we saved it on our computer and it's time to run it. By the way, this hello world program is our first script. You'll often hear certain programs being called scripts. Don't let that confuse you - a script is just a program. A script (or a program for that matter), simply instructs the computer what to do. So why do we have two names for this, program and script? Some of it is for historical reasons, but in general, we use the word 'script' for simpler programs and 'program' for more complicated programs.
When you run your program, it is read and executed by the Python Interpreter, which is a piece of software on your computer that is built to understand Python code. This software is called an interpreter because it basically interprets (or translates) code for the computer. Your computer does not natively understand the Python language. It only understands a much older and more complicated language which is actually very hard for humans to write. So that's why we need an interpreter. The analogy with human languages applies very well here. If you don't speak German and you need to talk to someone who speaks German, but they don't speak English, you'll need an interpreter. We don't speak the computer's language, because it's hard, and our computer doesn't speak Python because it's actually too complex, too close to English, for it to understand it. So we need a Python interpreter to translate between the two.
So, let's run our very first program. To do so, we need to open the terminal, a.k.a the command line. If you're not familiar with the command line, make sure to read my blog posts (how to use the command line - part 1 and how to use the command line - part 2) that introduce it.
Now, remember, when it starts, the command line (or terminal as I prefer to call it) takes you to your home directory, but you may have saved your program in a different directory. This is a good time to practice our command line navigation skills. We can use cd to move to the directory. On my computer, I saved my program in Projects/i2p, so we can just type cd Projects/i2p .
Now, let's run the ls command - and yes, we can see the hello_world.py script that we saved earlier.
So now that I've convinced myself that I'm in the right directory, we can just run this script. To do so, simply type python hello_world.py and then hit enter.
What you see is that the Python Interpreter read the code in our file, executed it and output the 'hello world' phrase, as it was instructed to do. In the terminal, python is just a command, just like cd or ls . The python command is the command we use to run the Python Interpreter. Remember, the interpreter is just a piece of software that sits on your computer, reads files containing python code, and executes the code inside them. The python command, in this case, is followed by the name of the file that contains the code that we want to run, which is hello_world.py. If earlier you gave your program file a different name, then you should use that instead of hello_world.py.
That's all there is to running a script. You basically write the code in an editor of your choice, save the file with a .py extension (and no spaces in the name, remember), then open up a terminal window and use the python command to tell the Python Interpreter to read your program and execute it.
The Python command line
It turns out that the Python interpreter can do more than just run code that we wrote and saved as a Python script. In fact, it also allows us to write code and immediately execute it. Let's see that in action.
Let's go back to the terminal and play with the python command a bit. As we saw previously, we know that, if we follow it with the name of a file that contains Python code, it's simply going to read the file and execute the code inside it. But what if we don't enter the name of a file at all and just type python and then hit enter? Let's try that.
This right here is the command line mode of the Python Interpreter. When it starts, you can see that it prints out a bunch of useful information, like the Python version. Earlier, I mentioned that the Python Interpreter is a piece of software that can read and run Python programs. It can do so in two ways: either by reading and running a file, as we saw above, or reading and running commands that we type directly in this Python command line.
In the Python command line we can't run commands like cd or ls because those are not valid Python code - those are commands that your computer's operating system understands. However, here we can type and run any valid Python code. In the beginning you might confuse the two - the Python command line and the regular command line, but here's a trick. If you see that three-arrow sign at the beginning of each line, you're in the Python command line and you can type and run Python code. If you don't see it, then you're in the regular command line and you can't type and run Python code, but you can type and run commands like cd and ls .
So, perhaps you're wondering, if we're in the Python command line, can we run the code we wrote in our script file? Of course we can. Let's try it. I'm going to type out print('hello world') and hit enter. And as soon as I hit enter, I can see the Python Interpreter read the command I entered and executed it. That's pretty cool, right?
Why do we have two ways of running code with the Python Interpreter? Well, the command line mode is always useful if you want to quickly try something out, because you can write code and immediately run it. But if you have a more complicated program that is more than a few lines of code long, it's very cumbersome to use the command line. Plus, the Python command line doesn't have the nice syntax highlighting that comes built-in with the editors we saw earlier. Also, the Python command line doesn't allow you to save your code. It also doesn't allow you to search through your code, and it doesn't have line numbers, which make for a nicer user experience when writing code.
Now we have two ways of writing and running code. We can write it in a script and run that script on the command line, or we can use the Python command line to write and execute code immediately. The file method is nice because we get syntax highlighting, searching in files etc., but it can also be a bit annoying because we have to switch to the command line to run whatever code we write. The second method, the Python command line, makes it very easy to write Python code and run it immediately, but it doesn't have syntax highlighting or other useful features, and it becomes very hard to use when writing longer programs.
As a beginner, you're probably wondering how to combine the benefits of the file method with the benefits of the Python terminal method. Is that possible? Yes it is! Enter Jupyter notebooks. Before we go into more details about this third way of writing and running code, let's close out of the Python command line - you can do so by typing quit(). Don't forget the brackets. So once I type that and hit enter, you can see that I'm back in my regular command line - the three angled arrows disappeared.
To learn more about this very powerful programming environment, please read our detailed guide on how to use Jupyter notebooks. Throughout this series, we'll be primarily using the Jupyter environment, so it is well worth the time to study it and get familiar with it.
That's it for now. As usual, the key to learning is to practice. Since printing is the only thing we've covered so far, just take a few minutes and write code that prints different phrases. It may look silly, but the idea here is to get you familiar with the Jupyter notebook environment, which we'll be using throughout this series. And worry not, soon enough you'll be able to write much more advanced code. There's quite a bit in this article if you're a complete beginner, so definitely read it several times if you need to and follow along on your computer until you are comfortable doing everything I showed you so far.