Running your first Python program

Tony Staunton
Python Code Academy
6 min readJan 12, 2022
Photo by Clément Hélardot on Unsplash

Python is a popular, high-level programming language built by Guido van Rossum in the late 1980s. Rossum’s goal was to create a programming language without complicated syntax, and to make the code of this language as human readable as possible. Since it was created in 1989, Python has grown from a simple scripting language into a fully fledged object orientated programming language. Today, accroding to a recent GitHub survey, Python has become the second most popular programming language in the world after JavaScript. If you are a beginning programmer, who is looking to learn a programming language that is powerful, human readable, and used in companies such as NASA, Instagram, YouTube, and Google then Python is the right choice. Not only that but as you grow as a developer, Python will grow with you, helping to protect your learning investment and future proof your skillset.

Source

Before you dive headfirst into learning Python it’s better to first ask ‘why Python?’. Python is a tool used by developers to perform tasks, and like all tasks each one can require a different tool. So is Python right for you and your tasks?

Why Learn Python?

  • Python is versatile: Few programming languages are as versatile as Python. It can be used to develop applications for machine learning, web development, data analysis, game development, cyber security, software testing, embedded systems, and much more. It is an excellent alternative to using relatively complex tools when building applications.
  • Python comes with a vast array of prebuilt libraries which you can use in your application. These libraries include NumPy for scientific computing, Pandas for data analysis and manipulation, Keras for deep learning, and many more.
  • Python is easy to learn: You do not need to learn overly complicated syntaxes. Python programmes are written, and read very much like plain English. Even with this level of simplicity you can create programmes to perform any type of complicated task.
  • Compared to other programming languages the learning curve with Pyton is relatively small. You can quickly get up and running, and master the basics in no time. In a recent survey by WPengine, programmers were asked what programming language they thought was the easiest to learn. Python ranked second after HTML.
  • Python is open-source with a vibrant, and helpful community: As a developer, you will at some stage need support from other Python developers. The Python community has never let me down. Also, there are lots of local meetups, conferences, and online forums where you can connect with fellow programmers and share ideas.

Now, let’s see how to get up and running with Python.

How to Install Python on your Machine

Installing Python on your machine is pretty straightforward. But just before we get to the installation process, it is important to understand that there are two versions of Python.

Python 2 and Python 3

Python is regularly updated with improvements and fixes. Most of the time, these updates are nothing to worry about, and your code will run irrespective of the version you have installed on your PC. However, there are incompatibility issues between the 2.x series and the 3.x series due to changes in the Python syntax. Today to get the most from Python it’s important to have Python 3installed on your machine as Python 2 has become deprecated.

Depending on your machine, mostly Apple devices to run some older programs, you may have Python already installed. This version of Python is most likely Python 2. So make sure to follow the step below to install Python 3 on your specific OS.

Installing Python on Windows.

  1. Visit the Python official website, https://www.python.org/downloads/, and download the latest version. You can also scroll down to download older versions.
  2. Once the download is complete, open the exe file
  3. Make sure to check the “Add Python 3.x to PATH” box, then click on ‘Install Now’.
  4. Simply click on next untill the setup is complete.

Installing Python on Mac

  1. Install XCode from the Mac App store
  2. Install the command line tools with the following command: xcode-select — — install (yes, that’s two hyphens, with no space, before install)
  3. Use Homebrew to install Python 3 since Homebrew automatically adds Python to you file path. Go to, https://brew.sh/ to install it.
  4. Once you install Homebrew, run the following command on brew:

brew update

brew install python3

Installing Python on Linux.

You can install Python using apt in Deian or Ubuntu. Simply enter the following command:

sudo apt install python3

Running your first Python Code.

Now, that you have Python installed, let’s write, and run your first Python program. Open Python by typing python on your Command Line or Terminal.

If all is working correctly, you will see the version of Python installed on your machine followed by >>>, telling you that you can start writing your Python code.

Note: If you don’t see Python3 at the version type python3 to explicity tell your machine to run Python3.

Now type the following command:

print(‘Hello world’)

Let’s break down what you’ve just typed:

  • print() is a default Function that displays something on your computer. Functions are prebuilt pieces of Python that you can reuse to accomplish common tasks such as displaying text to screen. There are a countless number of functions which can be used to perform pre-defined tasks such as adding two numbers, receiving input from the user, and so on.
  • When you call a Function in Python, you typically use open and close parentheses, as in (). Inside the parenthesis, Python receives the ‘raw materials’ required to perform what the function was programmed to do. For example, if a function is designed to add two numbers, the ‘raw material’ for the function is the two numbers it needs to add. Technically speaking, these ‘raw materials’ are called arguments. In the example code above, the raw material is ‘Hello World!’ which is the text we want to display on screen.
  • Also, in the code example above the use of single quotation marks tell Python that you are passing in text as an argument. Double quotes can also be used but you cannot mix single and double quotes. Choose only one type for this example.

If you run your line of code by pressing enter, you will have the text ‘Hello world’ displayed on your console.

And there you have it. Congratulations on writing your first Python code.

This article has one simple goal, to help you install Python and run your first program in as few steps as possible. There are other ways to install and run Python programs such as using the Anaconda data science platform which we’ll discuss in a later article.

If you would like a free guide which shows you other ways to get up and running with Python please click here.

I hope you found this article useful.

Tony.

--

--