Introduction

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:

  1. How to Declare and use a Python Variable
  2. Re-declare a Python Variable
  3. Concatenate Variables
  4. Local & Global Variables
  5. Delete a variable
  6. Summary

1. How to Declare and use a Python Variable

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.

a=500 

print (a)

2. Re-declare a Python Variable

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

print a

# variable re-declaring works

a = ‘xyz’

print a

3. Concatenate Variables

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: 

a=”Guru”

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. 

a=”Guru”

b = 100

print(a str(b))

4. Local & Global Variables

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

print f

# Global and local variables in functions

def someFunction():

# global f

    f = Python’

    print f

someFunction()

print f

  • When you do a variable declaration in Python using the global keyword you can also reference this global variable in the function.
  • The variable f has a global scope and this is assigned a 101 value which is printed in the final output
  • The variable f gets declared with the global keyword. This is not a local variable but it is the global variable that was declared earlier. Hence the output printed is 101
  • The value of f was changed in the function. Once the function is called the variables changed value will persist.

5. Delete a variable

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;

print(f)

del f

print(f)

6. Summary

  • Python variables are buckets where the information can be maintained and can be referenced to.
  • It is possible to declare the variable by a name or an alphabet. It is also possible to re-declare the variables after declaring them
  • You cannot concatenate the string and the number directly but have to declare it as a separate variable
  • The types of python variables are local and global
  • You need to declare the local variable to use it in the current function
  • Declare the global variable when you use the variable in the rest of the program
  • The “del” keyword is used to delete a variable.

Conclusion

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. 

ALSO READ

SHARE