Python 101 – Writing and Executing Your First Code

Learn how to write, save and execute your first piece of Python code. If you have followed our previous guide, Getting Started with Python Coding, you’ve already written your first piece of code with the ‘print(“Hello everyone!”)’ function. Here we expand on that and look at entering your code, playing around with some other Python examples, and then saving and executing your code.

Step 1 – Open the Python 3 IDLE. If you’ve closed Python 3 IDLE, re-open it as you did in the previous guide. In the Shell, enter the following command:


Step 2 – Just as predicted, the word Hello appears in the Shell as blue text, indicating output from a string. It’s fairly straightforward and doesn’t require too much explanation. Now try:


Step 3 – You can see that instead of the number 4, the output is the 2+2 you asked to be printed to the screen. The quotation marks are defining what’s being outputted to the IDLE Shell; to print the total of 2+2 you need to remove the quotes:


Step 4 – You can continue as such, printing 2+2, 464+2343 and so on to the Shell. An easier way is to use a variable, which is something we will cover in more depth later. For now, enter:


Step 5 – What you have done here is assign the letters a and b two values: 2 and 2. These are now variables, which can be called upon by Python to output, add, subtract, divide and so on for as long as their numbers stay the same. Try this:


Step 6 – The output of the last step displays the current values of both a and b individually, as you’ve asked them to be printed separately. If you want to add them up, you can use the following:


This code simply takes the values of a and b, adds them together and outputs the result.

Step 7 – You can play around with different kinds of variables and the Print function. For example, you could assign variables for someone’s name:


Step 8 – Now let’s add a surname:


You now have two variables containing a first name and a surname and you can print them independently.

Step 9 – If we were to apply the same routine as before, using the + symbol, the name wouldn’t appear correctly in the output in the Shell. Try it:


You need a space between the two, defining them as two separate values and not something you mathematically play around with.

Step 10 – In Python 3 you can separate the two variables with a space using a comma:

print(name, surname)

Alternatively, you can add the space yourself:

print(name+” “+surname)

The use of the comma is much neater, as you can see. Congratulations, you’ve just taken your first steps into the wide world of Python.

Saving and Executing Your Code

While working in the IDLE Shell is perfectly fine for small code snippets, it’s not designed for entering longer program listings. You will eventually reach a point where you have to move on from inputting single lines of code into the Shell. Instead, the IDLE Editor will allow you to save and execute your Python code. In this section you’re going to be introduced to the IDLE Editor, where you will be working from now on.

First, open the Python IDLE Shell and when it’s up, click on File > New File. This will open a new window with Untitled as its name. This is the Python IDLE Editor and within it you can enter the code needed to create your future programs.

The IDLE Editor is, for all intents and purposes, a simple text editor with Python features, colour coding and so on; much in the same vein as Sublime. You enter code as you would within the Shell, so taking an example from the previous tutorial, enter:

print(“Hello everyone!”)

You can see that the same colour coding is in place in the IDLE Editor as it is in the Shell, enabling you to better understand what’s going on with your code. However, to execute the code you need to first save it. Press F5 and you get a Save… Check box open.

Click on the OK button in the Save box and select a destination where you’ll save all your Python code. The destination can be a dedicated folder called Python or you can just dump it wherever you like. It’s better to keep a tidy drive though, to help you out in the future.

Enter a name for your code, ‘print hello’ for example, and click on the Save button. Once the Python code is saved it’s executed and the output will be detailed in the IDLE Shell. In this case, the words ‘Hello everyone!’.

This is how the vast majority of your Python code will be conducted. Enter it into the Editor, hit F5, save the code and look at the output in the Shell. Sometimes things will differ, depending on whether you’ve requested a separate window, but essentially that’s the process. It’s the process we use throughout our guides, unless otherwise stated.

If you open the file location of the saved Python code, you can see that it ends in a .py extension. This is the default Python file name. Any code you create will be and any code downloaded from the many Internet Python resource sites will be .py. Just ensure that the code is written for Python 3.

Let’s extend the code and enter a few examples from the previous tutorial:

print(name, surname)
print (a+b)

If you press F5 now you’ll be asked to save the file, again, as it’s been modified from before.

If you click the OK button, the file will be overwritten with the new code entries, and executed, with the output in the Shell. It’s not a problem with just these few lines but if you were to edit a larger file, overwriting can become an issue. Instead, use File > Save As from within the Editor to create a backup.

Now create a new file. Close the Editor, and open a new instance (File > New File from the Shell).
Enter the following:

print(a, b, c)

and save it as

Executing Code from the Command Line

Although we’ve been working from the GUI IDLE so far, it’s worth taking a look at Python’s command line handling. You may already know there’s a command line version of Python, but might not know it’s also used to execute code.

Using the code we created in the previous tutorial, the one we named, let’s see how you can run code that was made in the GUI at the command line level.

When you first installed Python, the installation routine automatically included all the necessary components to allow the execution of code outside of the GUI IDLE; in other words, the command line. To begin with, click on the Windows Start Button, and type: cmd.

As you did when launching the Python IDLE, click on the returned result from the search, the Command Prompt App. This will launch a new window, with a black background and white text. This is the command line, also called a Terminal in macOS, Linux, and Raspberry Pi operating systems.

Now you’re at the command line, we can start Python using the command python and pressing the Enter key. This will put you into the command line version of the Shell, with the familiar, three right-facing arrows as the cursor (>>>).

From here you’re able to enter the code you’ve looked at previously, such as:


You can see that it works exactly the same.

Now enter exit() to leave the command line Python session, and return back to the command prompt. Enter the folder where you saved the code from the previous tutorial, and list the available files within; you should see the file.

From within the same folder as the code you’re going to run, enter the following into the command line:


From within the same folder as the code you’re going to run, enter the following into the command line:
python This will execute the code we created, which to remind you is:

print(a, b, c)

Working With Different Versions of Python

If you’ve previously used Python 3 on a Mac or Linux, and subsequently the Raspberry Pi, you may be a little confused as to why the Windows version of Python uses the command line: python, instead of python3.

The reason behind this is that UNIX-like systems, such as macOS and Linux, already have Python libraries pre-installed. These older libraries are present because some of the macOS and Linux system utilities rely on Python 2, and therefore installing a newer version of Python, and thus altering the executable name, could have dire consequences to the system.

As a result, developers decided that the best approach for macOS and Linux systems would be to leave the command line ‘python’ as exclusive Python 2 use, and newer versions of user-installed Python would be ‘python3’.

This isn’t an issue with Windows, as it doesn’t use any Python libraries other than the ones installed by the user themselves when actually installing Python. When a Windows user installs Python, the installation wizard will auto-include the command line instance to the core Windows PATH variable, which you can view by entering: path into the command line. This points to the python. exe file required to execute Python code from the command line.

We don’t recommend you install both Python 2 and Python 3 within Windows 10; naturally, you can if you want, but realistically, although Python 2 still has a foothold in the coding world, Python 3 is the newest version. However, if you do, then you will need to rename one of the Python versions names; as they will be installed in different folders and both use python.exe as the command line executable. It’s a little long-winded, so unless there’s a dire need to have both versions of Python installed, it’s best to stick to Python 3.

Next, check out how to begin working with Numbers and Expressions in Python.

Find more guides like this in…

David Hayward

David has spent most of his life tinkering with technology, from the ZX Spectrum, getting his hands on a Fujitsu VPP5000/100 supercomputer, and coding on an overheating Raspberry Pi. He's written for the likes of Micro Mart, Den of Geek, and countless retro sites and publications, covering reviews, creating code and bench testing the latest tech. He also has a huge collection of cables.

Related Articles

Back to top button