Intro to Programming: How to Write and Run Code

Now that you installed Python, it's time to start writing some computer code!
By Ciprian Stratulat • Updated on May 2, 2023
blog image

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.

 

How to Write Code in Python Environments

When it comes to Python programming, you 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)

I will cover these in more detail in just a moment. Before I do that, let's talk briefly about the options you 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

 

What Are Text Editors and How to Use Them

Let's first talk about text editors. There are a lot of text editors out there, but the ones I'm 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.

 

I 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:

 

  • Autocomplete
  • 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.

Article continues below

 

How to Write Your First Program

Finally, in order to show you how to write and run a program, you 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 called The C Programming Language by Brian Kernighan.

I will abide by this tradition and I'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, I achieve this task by writing in Jupyter notebook:

print("hello world")

Ok, so let's write some code. You'll start by opening your 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.

Atom editor for writing code

Image source: Atom Editor, https://atom.io/

Now, let's click on the File menu and select New File from it. You should see a new window open and this is where you can write your code.

Go ahead and type your program:

print("hello world")

Image source: Atom Editor, https://atom.io/

When you are finished, you 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!

Atom editor for writing code

Image source: Atom Editor, https://atom.io/

It 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.

 

How to Run Your Code

Great, so you wrote your first program and you saved it on our computer, and now it's time to run it. By the way, this "hello world" program is your 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, 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 you 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. You don't speak the computer's language, because it's hard, and your computer doesn't speak Python because it's actually too complex and too close to English for it to understand it. So you need a Python interpreter to translate between the two.

So, let's run your very first program. To do so, you 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 that introduce it:

 

 

Image source: Atom Editor, https://atom.io/

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. You can use cd to move to the directory. On my computer, I saved my program in Projects/i2p, so you can just type: cd Projects/i2p

Image source: Atom Editor, https://atom.io/

Now, run the  ls command and you can see the hello_world.py script that you saved earlier.

Image source: Atom Editor, https://atom.io/

So now that you've convinced yourself that you are in the right directory, you can just run this script.

To do so, simply type python hello_world.py and then hit enter.

Image source: Atom Editor, https://atom.io/

What you see is that the Python Interpreter read the code in your 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 you 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 you 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 (with no spaces in the name), then open up a terminal window and use the python command to tell the Python Interpreter to read your program and execute it.

 

How to Write Code Using the Python Command Line

It turns out that the Python interpreter can do more than just run code that you wrote and saved as a Python script. In fact, it also allows you 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 you saw previously, you know that if you 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 you don't enter the name of a file at all and just type python and then hit enter? Let's try that and see what happens:

Atom editor for writing code

Image source: Atom Editor, https://atom.io/

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 you saw above, or reading and running commands that you type directly in this Python command line.

In the Python command line, you 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, in the command line, you can type and run any valid Python code.

 

  • Note: In the beginning you might confuse the Python command line with 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 you're in the Python command line, can you run the code you wrote in your script file? Of course you 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?

Atom editor

Image source: Atom Editor, https://atom.io/

Why are there 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 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.

 

How to Write and Run Your Script

Now that you have two ways of writing and running code, you can write it in a script and run that script on the command line, or you can use the Python command line to write and execute code immediately.

The file method is nice because you get syntax highlighting, searching in files etc., but it can also be a bit annoying because you have to switch to the command line to run whatever code you 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 I 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. It's a bit more advanced, but for reference, here are two  cheat sheets for Python: 

 

So once I type that and hit enter, you can see that I'm back in my regular command line because the three angled arrows (>>>) disappeared.

Atom text editor

Image source: Atom Editor, https://atom.io/

 

What Other Programming Environments Can You Use?

To learn more about Jupyter notebooks, a very powerful programming environment, please read our detailed guide on how to use Jupyter notebooks. Throughout this Intro to Programming series, you'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 on how to write and run for now. As usual, the key to learning is to practice. Since printing is the only thing I'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.

 

Ciprian Stratulat

CTO | Software Engineer

Ciprian Stratulat

Ciprian is a software engineer and the CTO of Edlitera. As an instructor, Ciprian is a big believer in first building an intuition about a new topic, and then mastering it through guided deliberate practice.

Before Edlitera, Ciprian worked as a Software Engineer in finance, biotech, genomics and e-book publishing. Ciprian holds a degree in Computer Science from Harvard University.