Overview

In computer science we have items called variables, so what are variables?

Simply put a variable is something that stores information and can be referenced and manipulated, we can compare it to a shipping container.

Containers store items that can be changed and modified, hence variables are like shipping containers for the programmer that store information to be referenced and manipulated.

Each variable has a type name and information stored inside it, the name is like a label for the variable.

Variables

To assign a value to a variable we use the = symbol

When naming our variables we should avoid ambiguity, the variable name should make sense, not only to you but also to fellow programmers.

Let’s say we want to name a variable for student names, we cannot name the variable SN, you might know “SN” stands for student name but a programmer taking over your code will be puzzled.

So always be clear when naming variables;


In the example above we have named our variable student_name and have assigned the value “Thato” to it.

If we have two or more words to our variable name we separate them with an underscore (_) making them one word.

The variable value Thato is placed inside inverted commas (“”) as it is a string, strings are always places inside inverted commas, which will explain in more detail when discussing strings.

Secondly, we cannot start our variable name with a number;


When we print the output we get a syntax error, meaning Python does not recognize variable names starting with numbers, it does not know what to do with them.

We can have numbers in the middle or at the end of our variable names;

We get no errors when we print the variable name with a number value at the middle.

One last thing to remember when naming our variables in Python is that variable names can start with a capital letter, but is not advisable.

We see from the examples above that we do not get any errors, Python does allow for variable names to start with a capital letter but it is unconventional.

Data Types

There are multiple data types, but we will just be exploring primitive data types.

Integers

These are whole numbers, including negative whole numbers;

1, 54, 89, -67, -109, -6

Floats

These are numbers with a decimal point; 0.7, 8.90, 10.0

String

This is how Python stores characters/ text, to indicate a value is a string we use quotation marks.

You can use double or single quotation marks, but double is preferred for me.

Boolean

These are True or False values, the True and False always start with capital letters