Installing Python & Hello World

Published on July 29, 2017 by

Welcome to this tutorial on Python! Whether you are a developer who just haven’t learned Python yet, or if you are completely new to programming, this tutorial is for you! We start at the absolute beginning, so if you are already familiar with programming, then you will find that this Python tutorial starts out pretty soft. Anyways, let’s begin learning Python!

The first thing we need to get started, is to download the latest version of Python 3. If you are on a Mac (and probably Linux, too), then you already have Python installed, but as of today, the pre-installed version of Python is an old one. So what you will want to do, is to install Python 3. Regardless of which operating system you use, you can do that by heading over to the downloads page at python.org. Alternatively, if you are on a Mac, you can use Homebrew to install Python by running the following command within the Terminal application.

brew install python3

Now you should be able to write python3 within your terminal (or Command Prompt on Windows). What this will do, is that it will open up a Python shell where you can write Python code directly.

You can press CTRL + D (or CMD + D on Macs) to exit at any time.

We will be using a program (referred to as an IDE – Integrated Development Environment) named PyCharm in this tutorial. There are many other IDEs out there, so you can choose any that you like. If you used the installer downloaded from python.org, then that also ships with a very simple Python shell named IDLE, which is great for doing simple things. Either way, you should find some Python editor to work with. In the beginning, you can just write code directly within a shell, but that quickly becomes inconvenient, at which point you will want to work with files.

Anyways, let’s write our very first line of Python! Simply enter the following into the terminal and hit enter.

print("Hello World!")

The result will look something like this:

If you are following along, then you have just written your first line of Python! Pretty easy, right?

What happens behind the scenes, is that the Python interpreter reads the lines of codes that you write and converts them into the appropriate machine code. That’s because the code that we write is just text, and computers don’t understand text. After having converted the code into machine code, the operating system (Linux/macOS/Windows) is then instructed to run it, which produces the message “Hello World!” as we instructed the program to print to the screen.

Perhaps you can imagine that writing code this way is not going to be very convenient or effective when writing more code than just simple print statements. Therefore you can easily add code to a file and then run the code within that file. Try to create a file named app.py with the following content.

print("Hello!")
print("Are you excited to learn Python?")
print("You should be, because Python is really awesome!")

Note that the .py file extension does not have any special purpose here, other than signalling that we are dealing with a file containing Python code. It’s a convention to use this file extension, but everything would still work if we used the .txt file extension, for instance.

Notice how each statement is added on a new line? That’s how we do with Python. Whitespaces and newlines are important and “behave” a bit differently than in other programming languages, but you will see that a bit later.

To run the code within the file, simply enter the following within your terminal or Command Prompt.

python3 app.py

Note that your currently working directory needs to be the directory in which the file is stored. You can navigate to the directory as follows before running the above command.

# Linux/macOS
cd /path/to/directory

# Windows
cd C:\path\to\directory

Running the code within the file will produce the following output.

Great! So now you know the very basics of running Python code, both directly within a shell, but also by typing code into a file first and then instructing the Python interpreter to run the code within that file.

You are already off to a great start, so let’s continue on our mission to learning Python!

Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

Leave a Reply

Your e-mail address will not be published.