Home
About Us Privacy Policy
 

VARIABLES IN PYTHON

ADVERTISEMENT

Beginner Variables



What are 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.


ADVERTISEMENT

What can a variable store?

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.


Why are they used, and what can they do?

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.

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.


ADVERTISEMENT

How to declare a 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.

my_name = "James Bond"

Now let us deconstruct the above Python code.

The above statement contains three elements, namely:

  • my_name is the name of the variable.
  • "James Bond" is the value that we are going to store in it.
  • "=" is the operator.

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.


Rules for creating variables names

There are specific rules that you as the programmer are required to follow.

Python defines the following rules to declare variables:

  1. They must begin with either lowercase or the uppercase alphabet or an underscore '_' character.
  2. They can contain any alphabet, either lowercase or uppercase, underscore, or even numbers.
  3. They cannot have any whitespace character, such as space, tabs, line returns.
  4. Special characters such as '-+|]{}()' are prohibited.

Examples of valid variables:

  1. my_name
  2. my_age
  3. python_v3
  4. __hidden_key
  5. ______
  6. ____0
  7. _69_

Examples of invalid variables:

  1. 8_is_my_birthday
  2. my name(Notice the space between)
  3. will_store_+_of_two_numbers
  4. 5


Variable Naming Convention

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?

  1. a
  2. my_age

Click to reveal the answer.


ADVERTISEMENT

Scope of variable

You can think of scopes as the "lifetime" of the variables that affect their availability.

Python provides three scopes of variables.

  1. Global
  2. Local
  3. Non-Local
We are going to cover scopes of variables in detail later on. But for now, remember variables can have three lifetimes.


Conclusion

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.


ADVERTISEMENT



All product names, logos, and brands are property of their respective owners.