Learn Programming with Python — An Introduction

Posted on April 29, 2020 in Learn Python

Learn Programming with Python — An Introduction

Have you never written any computer software at all, but would like to start? Join me in this series to learn the basics.

Photo by Christine Morillo https://www.pexels.com/@divinetechygirl

Learn Programming with Python — An Introduction

Have you never written any computer software at all, but would like to start? Join me in this series to learn the basics of computer programming.

This course is for you if you want to learn how computer programs work and write software to control your computer. When you’ve finished this course you’ll hopefully leave with a sense of the joy of writing computer software in Python.

But wait, what is a programming language?

Today’s laptop and desktop computers are powered by number-crunching chips known as the Central Processing Unit (CPU). These chips accept instructions and execute them. With billions of electronic circuits on each chip, almost everything your computer does for you is controlled by the CPU.

The CPU, as you may know, can only deal with two discrete states, known as On/Off, Zero/One, Present/Absent. These zeroes and ones are impossible for people to use to express themselves. Instead, we use “programming languages” which use words, phrases, syntax and rules very similar to our natural human languages. The thoughts we express as lines of computer software are then translated to a form which the CPU can execute directly.

Why Learn Python?

It is possible to use any of a wide number of “languages” to write computer software. First published in the early 1970s, the venerable Granddaddy of many of today’s popular languages is called “C”. Today, “C’ is still popular, as well as its children and grandchildren: “C++” (pronounced: C plus plus), “C#” (pronounced: ‘C-Sharp’), Java, Javascript, Python, Ruby and PHP.

Klint Finley said it best in his 2020 article for Wired Magazine: “Python Is More Popular Than Ever”. Python is a very versatile language allowing a range of programming styles and concepts to be expressed. Python is used widely at large companies such as Google and Netflix, as well as in academia in areas such as Machine Learning, Bioinformatics and Computational Linguistics. Python is also widely used in hobbyist projects, for example in home automation projects.

Getting Started with the Basics

In order to get started, you’ll need the following basics:

  • A computer! Windows, MacOS or Linux are all fine for writing and running Python code.
  • A python runtime! You can get yours and install it from https://www.python.org/downloads/ for your operating system. Make sure you get the latest version, at the time of writing it was Python 3.8.2.
  • A code editor! Python comes with an “integrated development environment”, but for a better beginner’s experience you should can install a code editor, such as Microsoft’s Visual Studio Code or JetBrain’s PyCharm Community Edition. There are many more editors available, if you’re curious.

I’ll be using VS Code on MacOS. Once you’ve got your pre-requisites downloaded and installed, let’s continue!

Hello, World!

It is a bit of a nerd tradition for your very first program to print out the text “Hello, World!” into a terminal (Windows: Command Line). Nothing could be simpler! If your “Hello, World!” program doesn’t succeed, keep trying at this point until it works.

Open a new file and immediately save this file as “hello_world.py”.

VS Code recognises that this is a Python file because of the .py file extension, and asks to install the Python extension.

Install the Python extension in Visual Studio Code.

You’ll also be asked to install the “linter” — which is used to make sure that the code you’ve written makes sense to the computer.

Now you might see the following warning in your VS Code terminal window:

That’s Ok. Type the following (or copy paste) into your terminal window to update pip — Python’s built-in updater.

pip install --upgrade pip

If this fails for you, try:

pip3 install --upgrade pip —- user and from now on use pip3 instead of pip to keep Python updated. The 3 in the name refers to the Python3 version of pip. Python had an earlier version -2- which is not compatible with version 3.

Enter the following text as the only text in your hello_world.py file. We’ll discuss what it does in a moment.

print (“Hello, World!")

Now click the green “play” symbol in the top right of the VS Code window. You should see something similar to the following:

VS Code showing “hello_world.py” execution output.

Congratulations, you’ve just run a python program called “hello_world” which output the text “Hello, World!” to your terminal!

Try to Modify It

  • Change the text and output your own customised greeting. Hint: make sure that the “” and () symbols aren’t removed — doing this would introduce your first bug!
  • Add the special characters \n to the end of your message, or go wild and add two: \n\n . This inserts one or two “newline” characters to your message. Go even wilder and add newline characters to the beginning of your message.

Try to Break It!

I was able to break hello_world.py and get the following range of errors produced in the terminal:

  • SyntaxError: EOL while scanning string literal
    This bug is caused by omitting the final " enclosing the argument. Python can’t figure out where thestring literal (in our case this is “Hello, World!”) ends. Instead, it found an end of line (EOL).
Syntax Error EOL
  • SyntaxError: unexpected EOF while parsing
    This bug is caused by omitting the final, closing bracket in our program. Python found an unexpected end of file (EOF) — it was expecting to find that the print() function would be closed with a final bracket.
Syntax Error EOF
  • SyntaxError: invalid syntax
    This bug is because we omitted the opening bracket ( after the print statement. Because of this error, Python can’t even begin to understand what we’re trying to say. It looks like a simple error, but to Python we now have total junk in this text!
Invalid syntax error!
  • NameError: name pront is not defined
    This bug is because we misspelled print and instead wrote pront . Now Python is trying to execute a function which it can’t find!
NameError

Explanation of Hello, World!

Let’s take a look at the different parts of our super-simple, one-line program.

  • print() this is a function which is built-in in Python. When we use this function, we instruct Python to print to the terminal (also known as standard-out) whatever appears in brackets.
  • Programming languages are often full of functions. A function usually takes at least one argument to work on. We are not limited to built-in functions, we can write our own!
  • “Hello, World!” is the argument we gave to the print() function.

Extending the Program by Accepting Input

So far, so good. But let’s make our Hello, World! program more interesting by accepting user input. Our goal is to ask the user for their name, and then say “Hello, <name>”.

We can do this by using a further in-built function, provided by Python: input(). Remove your existing code, and instead enter:

username = input("Please enter your name: ")
print(f"Hello, {username}!")

When I execute this program and type my name at the prompt in the terminal, I get the following output:

Greeting the user!

Line 1 does something really important. We use the input() function to get input from the user, and we supply a prompt: Please enter your name: so that the user knows what to do. The important thing is that we store the user’s input into a variable named username. That’s important because we need this value later!

In line 2 we use a Python feature called F-Strings. F-Strings are a recent addition to Python, and enable us to format a text on the fly, without using complex logic to build a text. F-Strings are indicated by the construct f"" — the preceding f indicates that an F-String follows.

In our F-String we use curly braces to indicate the position at which a replacement should occur: {username} . The curly braces are removed from the output, instead the value stored in the variable username replaces it.

The line:

print(f"Hello, {username}!")

Combines all of the above points! We reuse the print() function from earlier. We use the F-String capability to build the output, and hey presto, it works!

Extending Hello, World to request the user’s name.

What Have We Achieved?

Congratulations, you’ve reached the end of the introduction article “Learn Programming with Python”. Here’s what we’ve done so far:

  • Discussed why Python is a good choice
  • Installed Python and Visual Studio Code
  • Written our first program and executed it
  • Played around with the program a little and learned about bugs we can introduce
  • Learned how to receive user input from the terminal
  • Learned about F-Strings

That’s quite a lot for an introduction! In the next instalment we will learn how to create our own functions to make more complex programs.

Articles in this series so far: