In this post, we learn How To Use Variables In Python. Before we get started, if you want to write comments in the python program, please go through the following article: How To Write Comments In Python.

One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value. Variables are an essential programming concept to master. Python is a dynamically typed programming language, which means you do not need to declare variables or their data type before using them. This tutorial will guide you through the basics of variables in Python 3.

When to use Variables?

Imagine that in your code somewhere you need this number 3726529215.733739. You might need to do some calculations with this number a couple of times in your entire program. In such a scenario storing this value in a Variable is better than hardcoding it every time.

Variables in Python

A Variable is a storage location for a value tied with an identifier called variable name. The variable name is used to reference the value in the program when needed. For the above example, we can store our number 3726529215.733739 in a variable named my_number. Now our number can be addressed anywhere by the identifier my_number. To assign the value to a variable, You need to use the assignment operator (=). The variable name should always be on the left side followed by the assignment operator and the value after it.

Now we can use this variable anywhere in the program.

We can also perform operations on the variable

We can also store the result of this operation as another variable to reference later

Unlike other programming languages such as Java and C, we don’t need to specify the datatype of the variable in Python. Python itself does that work for us; this can be seen in the below example:

If we print the above list, Python will print the list for us.

In Python, every variable is an object. You are giving a name to the object. Variables are quite handy if used properly, however, there are rules and norms to follow while naming Variables.

Naming Variables

Variable names can be arbitrarily long. They can contain both letters and numbers, but they have to begin with a letter(not a number). It is allowed to use uppercase letters, but it is a good idea to start variable names with lowercase letters.

A variable name should always be in one word without any space in between. If the variable name has two words separate them with an underscore (_) just like we did for my_number and my_list. Do keep in mind variable names are case sensitive which means, My_number, my_number, mY_number all are different so always stick with lowercase letters. In programming, there are two conventions of naming a variable :

Camel Case

In Camel Case the variable name consisting of two words is formed by capitalizing the first letter of every word then appending it without any space or underscore, you will see a lot of this in javascript. Example – myVariable, myResult, myInteger, myList

Snake Case

Snake Case in the convention of naming variable in which the words are separated with an underscore and the first letter of each word beginning with a lowercase letter. Example – my_variable, my_result, my_integer

You should always be consistent with the convention and stick to the snake case in Python to make your code more readable and appealing to others.

Reassigning Variables

We can assign new values to previously assigned values, the Variable will hold the most recent value. Assigning a different datatype to the same variable is also possible.

Now let’s reassign my_var with an integer.

Multiple Assignment

In Python, we can assign one single value in numerous different variables, like this :

The None Variable 

A predefined variable called None is a special value in Python. This variable has a type of its own and is used when you define a variable but not assign it with any value.

Local and Global Variables

Based on Scope, variables can be divided into two kinds, Local variables, and global variables. Global Variables are declared outside any function, and they can be accessed anywhere in the program whereas Local Variables are maintained and used only inside a function any other function can’t access them. Keep in mind there can be Local variables of the same name in different functions. Let’s look at the following example,

This gives us the following output,

Here we need to call the function to print the local variable however global variable can be addressed directly. Now let’s see if the Global variable is available in the function or not,

And we get the following output:

We can see the global variable is printed twice which indicates that the Global variable is available to the function. But when we try to print the local variable outside the function directly, it will give us an error.

Delete a Variable

You can also delete variables using the command del “variable name”. In the example below, we deleted variable f, and when we proceed to print it, we get error “variable name is not defined” which means you have deleted the variable.

We get the following output:

A programmer should take benefit of this nature of variables. Usually, it is better to keep the variable local, but if you are using a variable in many several other functions then make it global. Despite this, there are some predefined keywords in Python. You can not use these names as an identifier or a variable name in your code. These are reserved words for Python.

Next Recommended Article: Data Types In Python

The article was published on September 15, 2020 @ 11:10 AM

Leave a Comment

1 Comment