What are the Types of Variables in Python?

Video Tutorial
FREE
Variables thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate
Topics Covered

What are the types of variables in python?

Types of variables in python depend on the type of data used to declare data types to store data and perform mathematical functions on it. Some of the most basic data types that are used while coding in python are :

  • int
  • float
  • string
  • char
  • bool

Unlike other programming languages, we do not need to define the data types in python. For Example: In C++ Or Java, We have,

Whereas in python, the definition of data types is fetched automatically depending upon the value we assign.

For Example:

Local Variable in Python

Like other programming languages, the definition of a local variable also remains the same. That is, A variable declared inside a function is a local variable. It is because we can access the variable only inside the function. Calling the variable outside the function will pop an error. For example : When a local variable is declared inside a function,

Input:

Output:

This code doesn't populate any error because we had called the function AreaOfRectangle(), not the variable declared locally inside the function.

But what if we call the local variable outside the function?

Input:

Output : local-variable-area-of-rectangle-output

So, we get an error that the variable length is not defined. It is because the local variable is not in scope. It is only locally available to the function defined within' AreaOfRectangle()`.

Global Variable in Python

Global variables are variables that are declared globally. That is, they are available inside the function as well as outside the function.

Now, let us understand the concept of a global variable with the same code.

Input:

Output:

In this example, we have declared the variable length and breadth outside the function, and while printing the value of length in the 8th line, we have incremented the value by 2. We can see no error populated because both variables are in the function's scope. It proves the function can access both the variable length and breadth.

But you might wonder why we increment the length value outside the function and not inside.

Let us try to increment the length variable inside the function.

Input:

Output: global-variable-area-of-rectange-output

We get a runtime error stating the local variable ‘length’ referenced before the assignment. This error is because the variable length is declared locally and cannot be updated inside the function. To solve this problem, we can convert the local variable length to the global variable length. Let's see how we can do that.

Input:

Output:

When we execute the code, it executes without populating any error. It is because we have defined the local variable length to the global variable length to update the variable's value inside the function.

Learn More

Conclusion

  • Python Variables are containers which store values.
  • Python is not “statically typed”. We do not need to declare variables before using them or declare their type.
  • A variable is created the moment we first assign a value to it.
  • A Python variable is a name given to a memory location.
  • Data types are the classification or categorization of data items.
  • It represents the kind of value that tells what operations can be performed on a particular data.
  • Following are the standard or built-in data type of Python:
    • Numeric
    • Sequence Type
    • Boolean
    • Set
    • Dictionary