Beginner Variables
When you think of variables, what comes to your mind? Most people would say a value that can vary from time to time, like the temperature of your room, day of the week, your clothes, and maybe your relationship status.
In programming, variables are like containers or boxes that store values; think of it like your milk carton, shipping package, cookie jar, or warehouse. All of these can store a certain amount of things but different values or items. For example, the milk carton can store chocolate-flavored milk, vanilla-flavored, or even almond-flavored. But they hold a specific type of item, i.e., milk, albeit of different flavors.
Variables in Python are analogous to containers. They can store the value of a specific type for later use.
During development, you might use many variables to store information such as Name, Age, Gender, Address, Date Of Birth.
It is impossible not to use a variable and develop a usable program.
In the Python programming language, variables can store any value, such as IP Address, personal information, references to images, references to files, textual data, numeric data, and any other kind of information that describes observable reality.
Just a tidbit, NASA is using Python for machine learning to find exo-planets.
To understand the reasoning behind it, we must first understand how do computers work. Don't worry; I won't delve into the nitty-gritty technical details but will explain using a simple abstract diagram.
In short, computers input values, process them, and generate output, and most of the time, computer programs are just storing and passing data.
Let's take an example for a much better understanding. Suppose we are writing a program to tell the day ten day's from now. What would be the variable(s)?
Try, then click to reveal the answer.Current and future day
If you guessed it correctly, well done!. If not, it's okay; programming can be a bit challenging initially; we all have to go through this. I recall "the old days" when I used to think about developing real-world programs such as mobile applications or a website much like this one seemed like a pipe-dream. Realistically speaking, it will take time and a lot of practice, but it is possible, and you will achieve it, create the next best application and possibly retire early.
Variables allow our program to store and process data to generate output.
It is virtually impossible to develop any program without using any variable.
We know a bit about variables and their critical part in computers, but how do we declare them? In the Python programming language, it's straightforward.
Let us take a simple greeting program as an example. What is the different information we would require? Our name. So let's declare a variable to store our name.
Now let us deconstruct the above Python code.
The above statement contains three elements, namely:
Read the statement from Right to Left for easy understanding.
Let us apply the tip to read our one-liner python code. So, starting from the right, the interpretation would be James Bond is my name.
But wait, aren't we initializing the variable as well? Yes, that's correct. The Python programming language allows the programmer to declare and initialize variables together. Meaning, they don't have to be explicitly defined and separately initialized to be of any use, unlike other programming languages.
That is one of the abstractions the Python programming language provides since it is a high-level programming language.
Remember, the purpose of computers is to make the work of humans easy, not complicated, and Python completely embraces that.
There are specific rules that you as the programmer are required to follow.
Python defines the following rules to declare variables:
Examples of valid variables:
Examples of invalid variables:
TIP: Keep the name of the variable descriptive and not exceeding 79 characters.
As stated earlier, documentation is an integral part of software engineering. That same philosophy extends to having descriptive variables names. Let's quickly touch upon an example.
Which of the two variables convey what type of information they are going to store?
Click to reveal the answer.
You can think of scopes as the "lifetime" of the variables that affect their availability.
Python provides three scopes of variables.
In this chapter, we learned about variables in programming and their significance, variables of different data types, declaring variables in Python, rules for creating variable names, and PIP convention for declaring variables.
We also learned about the different scope(lifetime) of a variable and looked at a few examples.