Variables in Python Programming

0
151
Variables in Python Programming

Variables in Python Programming( Python Tutorials: 7 )

Variables are basically a memory space to store data. There is no need to declare a variable before using them, the declaration happens automatically when value is assigned to the variable. We use equal sign to assign values to the variable.

Python has local and global variables like most other languages, but it has no explicit variable declarations. Variables spring into existence by being assigned a value, and they are automatically destroyed when they go out of scope.

In python, variables normally do not have any types associated with them and types are only associated with objects so the variables just have a reference to these objects. This is how variables and objects work together in python.

For example :

Here site is a variable and lincoder is a value assigned to that variable. There is no need to declare the variable as like in other programming languages. The variable is not associated with any data types but is just a reference to the objects. The object “lincoder” has a type associated with it which is a string. So now you can clearly understand that variable is not the one which has type associated with it but the objects is.

Similarly, another example can be:

Here the count is variable and digit 8 is the value assigned to that variable. So you can now tell that the object 8 has a type called Numbers. To deal with floating point values, you can do:

That’s how you can define floating point values in python.

Just to remind you, there are 5 standard data types:

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

You can assign multiple values in just one command. Python uses sequences to assign multiple values. For example:

Here site is tuple of 4 elements and (l,g,y,f) is a tuple of 4 variables.

I hope this makes it clear about what variables are and how to use them.

LEAVE A REPLY