Variables are fundamental in every programming language. Similarly, in python, python variables are the fundamentals. These are the location where memory is reserved. The function stores and helps in data manipulations. This beginner’s guide on python variables will emphasize what the python variables are and the different kinds of python variables.
A python variable stores values. The variable used in the python program gives the computer the data for processing. There are various types of variables in python. Each variable used in python has a particular datatype. The varied data types used in python are numbers, tuple, list, dictionary, strings, etc. The variables that are used in python can be declared using alphabets or names like a, b, c or xyz, abc, etc.
In this article let us look at:
The variable first has to be declared in python. The variable first needs to be defined in python and then the declaring variables in python should be done. Let us see an example where we define the variable, declare it as an “a” and then print it.
Once you have declared the python variables you can go ahead and declare it again. Here in this example, we declare a variable “a” and then declare the variable to “xyz”.
a = 0
# variable re-declaring works
a = ‘xyz’
Here we will find out if it is possible to concatenate the different types of data. This could be to concatenate a string and a number with each other. Let us try to concatenate the string “Guru” and the number “100” together.
In python, when you declare the variables it requires you to declare the number in a string else it throws a TypeError.
The following code is not right and gives you an undefined output:
b = 100
print a b
Now here the integer has been declared as a string and this can concatenate the “Guru” str(“100”)= “Guru100” in the output.
b = 100
The variables in Python are of two kinds. These are the local and the global variables in python. If you wish to use the same variable for the entire program or the module then you will declare it in the form of a global variable. If you wish to use the variable only for one specific function or a method then this is a local variable while you declare variables in python.
Read below to understand the types of python variables.
Understanding the difference between a local and a global variable.
We can define a variable say ‘f” in python where the variable is global. This is given the value of 101 and is printed as an output
The variable f is declared again in a function and this becomes a local scope. This is assigned as a value “Python” which will be printed in the output. The variable declared in Python is not the same as the global f variable that has been defined previously.
Once the function call is done the local f variable will be destroyed. In the example below when you print the value of f again then this will print the global f variable value which is 101.
# Declaring variable and initializing it
f = 101
# Global and local variables in functions
# global f
f = Python’
It is also possible to delete the python variable which can be done using the “del” command variable name. Here is an example where we delete the python variable.
f = 11;
This article lets you learn about the concepts of the python variable, what a variable is, how a variable is declared and how to carry out operations on the variable.
If you are interested in making a career in the Data Science domain, our 11-month in-person Postgraduate Certificate Diploma in Data Science course can help you immensely in becoming a successful Data Science professional.