As I see it, the purpose of programming is to build machines out of bytes that are impossible or too expensive to build out of atoms. Computers are machines that can imitate any other machine. Programming is the act of making them do that.Eric Hodges
From the above quote, we can see that a computer is a universal machine.
As we begin the journey of learning how to program on this site, the following characteristics of universal machines will shape the content for this training.
- A way to keep track
- A way to make decisions
- A way to keep going
We achieve the first feature by using variables. The second feature is implemented using branching while the third feature is implemented using loops.
This post will focus on variables. Variables are a container for information, they are used to store values in a program.
The way variables are created in the Python programming language is similar to how they are treated in algebra.
if x = 6, what is x?
If you know algebra, you would answer 6 and you would be right. Like in algebra, the = symbol is for assignment. It assigns the value on the right to the value on the left.
A variable is created the moment you assign a value to it. Once we assign a value to a variable, the Python interpreter creates it.
In the last post about Input and Output, a variable name was created when the input from the user was assigned to it.
Like containers in the real world, the type of material a container is made out of determines what kind of material it can store. In computing, this is the concept of data types.
In the Python programming language, we have 5 standard data types:
For this program, we shall only look at numbers. However, I will illustrate Numbers and Strings in this blog post.
We have two types of numbers in computing: integers and decimals. Integers are whole numbers while decimals have a . in them for example, 1 is an integer while 1.0 is a decimal.
Strings are a sequence of characters. Your name is a sequence of characters. Strings are placed in the “”.
The type function will let you know the data type of the variable passed into it. The code below will illustrate the concept in action.
The above image shows that in the first 3 lines we declare the respective variables. Do bear in mind that the variable names are for you, not the users.
Once we use the type function, notice that it gives us the type each variable. The decimal variable is depicted as a float.
Content and Context
This is a very important concept when choosing the data type of a variable. The “label” of a variable is different from its content.
As a result of this, you cannot declare the phone number of a user as either an integer or a float because you cannot add two phone numbers together.
Rather you would define them as a string. As seen in the blog post on Input and Output, you have to convert a number to a string before you can join it to it.
This is important as we continue on our journey.
Learn Python in One Week
If you want a quick and easy introduction to the Python programming language, you should check out my fifth book: Learn Python in One Week on the Amazon store.