Programming 101 – learning Python

Posted by

Programming is an important and highly sought-after skill. It’s a great asset to have for any professional knowledge-worker in this information age. Aside from the career benefits, you can also learn programming to improve your logical thought processes, build the next awesome mobile app, or just  have fun with something new. We introduce you to programming in this tutorial using a programming language called Python.

Introduction to Programming Languages

There are various programming languages out there – Visual Basic, Java, C, Python and  PHP, amongst others. We use a programming language to write out a set of instructions that define how a computer program must function. Therefore, programming is the science and the art of telling the computer what to do. The language that a programmer may choose to use depends on a number of things; their past experience with programming, the type of problem they need to solve and the environment that the program will be running in. Different programming languages are designed for solving specific problems within a certain environment.

 

Why Python?

Since there so many programming languages around, designed for such diverse purposes, why should we choose to learn Python instead of something else? As the Python designers say on their website: “Python is a programming language that lets you work quickly and integrate systems more effectively.” As a new programmer, you want to start building interesting programs as soon as possible, and Python will not burden you with unnecessary complications that might quench your enthusiasm for learning this new skill. It’s an easy-to-use, and easy-to-learn language that will have you writing powerful code in no time! Let’s dive into it so you can experience it for yourself.

 

Getting Python

We’ll assume that you’ve got Python installed and ready to go before we begin. If you havem’t gotten around to installing Python yet or run into issues, read our handy guide to Installing Python first, then come back here and keep reading!

 

Getting Your Feet Wet!

The Python shell is an interactive environment where we write Python code and immediately see the results of the statements when we execute them. This is a great way to learn how to program because you receive immediate feedback on what you do. Now let’s introduce a few programming concepts using the interactive shell so that you can get acquainted with the language.

We’ll begin with some basic arithmetic.

  1. Type out the following on the shell and press the Enter key.

>>> 5 + 10

learning Python

Python calculates the result of the addition and displays it.

It’s all well and good that Python can compute mathematical results, but what happens if we want to save the output of our computations? Well, storage in Python is done via variables. A variable is an alphanumeric (letters and numbers) label that we give a storage space which keeps any values we put in it.

  1. To create a variable and store the first number in it type:

>>> numOne = 5

  1. Create a second variable called numTwo and store value 10 in it.
  2. To view the  contents of the variables we have created, we simply type out their labels and execute the statement by pressing Enter.

In the statement above, we retrieved the contents of the variable numOne and numTwo by writing them out in the line of code to be executed. This is called referencing a variable.

  1. Create a third variable called numThree and store the sum of numOne and numTwo in it.
  2. Display the contents of numThree to see it the answer is correct.learning Python
  3. Python performs all mathematical operations, we’re not limited to addition only.
  4. We can also store words in a variable. To create a variable with a word in it, execute the following line of code:

>>> string = “Hyperion Hub”

A word or collection of words written between quotation marks in Python is called a string. All programming languages use this term to refer to series of characters that are stored as a single complete unit. The terms is used because the letters are stored as a string of characters in the computer’s memory.

Note that we have to use quotation marks around the string we want to store in the variable. If we don’t add the quotation marks around the words, we get an error.

learning Python

 

Error messages are displayed in red font on the shell. This message is simply telling us that we have written code that doesn’t conform to the rules of the language.

  1. Create a variable called firstName and assign your first name to it.
  2. Create another variable called lastName and assign your last name to it.
  3. Check the contents of these variables to see that contents stored in them.
  4. Now create a variable named fullName by joining the first two strings together:

>>> fullName = firstName + lastName

The process of joining strings is called string concatenation.

  1. Check the contents of fullName. Do you notice anything wrong?

We write our full names with a space between the first and last name. Python doesn’t put in the space automatically for you. You need to be explicit, by concatenating a space after the first name.

>>> space = “ “.

  1. Rewrite the last statement and add the space between your first and last name.

 

The process of putting contents within a variable is called assignment. We assigned the value of the calculation to the variable second.

So we now know how to write arithmetic statements for Python to compute. We’ve also learnt how to assign a value to a variable through assignment, and later use the value through referencing. Finally, we learnt how to display the contents of a variable by simply writing out the label we gave it and executing that statement.

We also learnt that we can store different types of contents in variables. A string of characters can be stored in them and joined together, through concatenation. When we don’t follow the rules of Python when writing statements in the shell, we get an error message. These messages may seem cryptic at the moment, but as you gain experience with the language, you’ll learn what they mean and how they can help you fix your mistakes.

You’ve now grasped the absolute basics of Python through the interactive shell. Next we’ll learn how to do more interesting stuff with the language.

Comment with your views on this article in the comments section below, and follow Hyperion Hub developments in the future if you’d like to see more articles like these for the South African market. Here’s more information about HyperionDev’s Python short course. Alternatively, you could do the Full Stack Web Development course, which includes Python.