Python for everybody | 5 | What are variables in Python programming

Python Tutorial 5
What Are Variables

In this tutorial, we will cover what are variables in Python programming.

Nearly 80% of our codes will consist of variables.

That’s how important variables are.

Variables will be referred to throughout the tutorials.

Without further ado, let’s get started!

What Are Variables In Python Programming?


To put it very simply, a variable is a name for something.

The word variable suggests that it is something that varies or changes.

What are variables in Python programming | 1

A variable in Python is a container or storage box of which its contents can change depending on what we put into the container or storage box.

It is a storage holder that has a name or label pasted outside it and it holds something of value inside it.

We can imagine a storage box with the label course_name and inside this storage box it contains the “Python Course”.

The container’s label should match whatever is contained inside the container.

The container’s label should clearly and appropriately describe what will be stored inside the container.

80% of programming codes consist of containers, storage boxes, or variables because computers are so good at storing things that change.

Creating Variables

When we create a variable, we give it a name — it is like when we use a storage box, the first thing we do is to label it with a name.

We use a variable name to reference data such that we can access the data inside the variable by referencing the variable’s name.

The variable’s name is fixed but its content or value can change — it is like now we put something into the storage box and then further on we can take it out and put something else in.

Assigning Variables

To assign a value to a variable name, we use the assignment operator.

What are variables in Python programming | 2

The = sign is called the assignment operator.

The variable name student_name has its value changed from “Mary” to “John”.

The variable name count has its value changed from 5 to 6 by adding 1 to it and then changed again from 6 to 10 by taking out the 6 and putting in the 10.

Naming Variables

Select meaningful variable names that represent what they are for.

Try to use the variable name count instead of just c, and year instead of just y.

Variable names must only use:

What are variables in Python programming | 4

Examples of valid variable name are:

student_name
_student_name
Student_name
studentname
studentName
studentName1
STUDENTNAME_

Examples of invalid variable name are:

1studentName
student@name
student name
student-name

1studentName is invalid because variable names cannot start with a digit.

student@name is invalid because variable names cannot have any special characters except the underscore character _.

student name is invalid because variable names cannot have the space character.

student-name is invalid because hyphens are not allowed.

The general naming convention in Python when it comes to variable names, is to use lowercase alphabets and separating each word using the underscore character _.

The camelCase

However, many Python software developers use the camelCase instead of the lowercase separated by the underscore character.

camelCase refers to variable names starting with a lowercase character, and instead of using the underscore character _ to separate 2 words, it starts the next word with an uppercase character.

It is important to note that regardless of whether we use lowercase with underscore character _ or camelCase, we should be consistent and adopt the same naming convention throughout our program.

And variable names are case-sensitive:

student_name is not the same as STUDENT_NAME

Python Keywords

All Python keywords cannot be used for variable names:

Python is a case-sensitive language and Python keywords are case-sensitive.

Please take note that these 3 keywords — False, None, True — they all start with an uppercase character and not a lowercase character:

False
None
True

Variable names are also case-sensitive:

False is not the same as false

False cannot be used as a variable name

false  can be used as a variable name

The Python keyword False, which starts with an uppercase F, is not the same as lowercase false.

Python keyword False cannot be used as a variable name but lowercase false can be used as a variable name.

Tracing Variables

As the value of a variable changes throughout our program because new values are constantly assigned to it, we can use the print() function to trace the value of our variables.

The print() function can be used for debugging purposes whereby we need to know what went wrong in our program and one way to find out is to trace how the value of our variables changes in the course of our program.

There are other more advanced debugging tools that we can use which will be covered in later tutorials.

Watch my online Python course for a different learning experience.

>> Click here to watch my online Python course at Udemy