Variable declaration in Python

Variables are just used to store data values. It basically refers to a memory location. We don’t have to declare a variable type in Python. So, our variable can store different data types as and when required. But, it would keep erasing the previously stored data. In this article, we would see Variable declaration in Python through couple of examples.

Python has different data types – float, int, tuple, lists, dicts, string etc. Through variables it gets easier for us to perform required operations.

Variable declaration in Python

Variable declaration in Python is pretty easy. We may want the value of variable x to be 7. So, simply execute the following –

>>> x=7

Here, we have assigned the value 7 to variable x. Using print() function we can verify the code –

>>> print(x)

It would return with –

7

We can also know the type of an object using type() function –

type(x)

The outcome is –

<class 'int'>

Now, do the same with a string.

x='seven'

Use print() function to display the value of x –

print(x)

But, if we check the type of the object this time around, it would show that its a string

type(x)

This will return –

<class 'str'>

Conclusion

In this article, we discussed variable declaration, print() and type() functions in Python.

Additional Info –

Here, we will discuss a bit about local and global variable. Local variables are those variables which are declared inside the function. Such variable can’t be accessed outside of a function. Whereas, Global variables can be accessed across the entire code we have written. Furthermore, global variables are not limited in their scope. We will discuss more about in coming articles.

Similar Posts