Python Variables Tutorial with Examples

In this lesson, general aspects of the variables are discussed.

Learn what are variables in python and how to create them.

We will also take a look at the rules that we should follow while creating a variable in python.

This tutorial has plenty of examples for better understanding.

Before we start learning the variable concept in python let’s go back to school days to revise an important concept of geometry.

let’s calculate the area of a circle.

If you still remember the area of a circle can be calculated using the formula πr2.

Here the value of the pi would be 3.1416 and let’s assume the radius of the circle is 3 meters.

Now open the python shell and calculate the area of the circle.

Python variable tutorial
Python variable tutorial

Although the above syntax works fine, there is one major limitation. Here the radius of the circle is hardcoded.

Therefore, if you want to change the radius of the circle then you have to rewrite the same syntax again and again.

And the code repetition is not good practice while working for real-world projects.

Isn’t it?

Here variable concept comes into the picture.

Let’s use variable and rewrite the above code syntax using the variable.

What is Variable in python

A variable can be understood as a kind of box in which value can be stored. 

That box usually corresponds to a memory location in the computer memory.

The symbol equality (=) must be understood as an assignment, not as a mathematical equality.

As you could see in the above example, a is a variable (unique identifier) that represents value 10.

In simple words, we are asking python to keep the value 10 in a “box”, and later if we want to access the value we can simply use the variable name to retrieve or re-assign the value.

How to declare a variable

In python, variables are created when they are defined and assigned a value for the first time.

It is very easy to define a variable in python.

You just have to tell the name of a variable and assign a value to that variable using the assignment operator (=).

However, If you are coming from Java then you would be a little surprised to see the variable declaration in python.

In python, you don’t have to explicitly declare the type of the variable.

Thus, we can say that Python is not a statically typed programming language.

Here ‘pi’, ‘radius’, and ‘area_of_circle’ are the variables.

You can assume that a variable is nothing but a container that represents a value temporarily.

In the above example, the variable pi is representing value 3.14159, radius representing 3 and area_of_circle is used to represent the area of the circle ( πr2 ) or value 28.274.

How to re-declare a variable

What will be the output of the above program?

Runtime error, ‘Hello’ or 101?

It is 101.

In python, you can re-declare a variable anytime.

Because python is not a statically typed language, python will determine the type of that variable from its value.

Key Points to remember while creating variables in python

1. The variable name can start only with an underscore or alphabets

Below are valid Syntax

Below are invalid syntax

2. Variable name should be meaningful

Although it is not mandatory, it is recommended that the name of the variable should be meaningful to the information stored in it.

In the above example, there are two variables sum and ddh.

Both variables represent the same values 10.

but only ‘sum’ clearly tells us that this variable represents the summation of numbers.

Therefore, to make it easier to understand the program always use the meaningful name of variables.

3. Reserved keywords are not allowed for variable names.

You can’t use reserved keywords for the variable names. The above syntax is not valid.

4. A variable name cannot start with a number.

A variable name can’t start with a number.

5. A variable name can only contain the alpha-numeric characters and the underscore.

The above variable declaration is valid.

The name of a variable must start with a letter or an underscore (_) and can continue with more letters, numbers or underscores.

Also, when the name of a variable contains several words, it is advisable to separate them with underscores for better readability.

6. Variables are the case-sensitive

For the variable ‘name‘, we have assigned the value ‘Mark’.

However, we are trying to print the value of Name (capital N) which is not valid.

How to know the type of a variable

In python, it is easy to determine the type of a variable.

we can use type() function.

The output of the above program would be <class 'int'>

The output will be

How to assign values to multiple variables

You can assign a single value to multiple variables or multiple values to multiple variables in a single line

Conclusion

In this tutorial, we discussed about variables and how to create them.

We also discussed do’s and don’t while declaring variable names with few examples.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.