Saturday, 2 April 2016

Python Variables, Objects and Values

Hello guys how are you!!!, Hope you will be fine and also enjoying this tutorial. In my previous post you learned about main function in python. You also learned the importance of declaring the main function in python.



Today you will learn about python variables, objects and values.

Objects, Variable, Value:
      Everything in Python 3 is an object: variables, functions, even code; all of these things are actually objects. In Python every object has an ID, a type, and a value. The id is a unique identifier that identifies a particular instance of an object. This cannot change for the life of the object. The type identifies the class of an object, and again this cannot change for the life of the object. The value is the contents of the object, including whatever variables, or methods, or properties the object may have. Mutable objects can change their value; immutable objects cannot.

Let's take a look in an example of an object. For that purpose we will use python shell. This is called IDLE. It's the graphical interface of the Python shell. Below screen shot shows python shell.

 

Python comes with this in graphical environments like Windows and Mac. And if you are in a non-graphical environment, an environment with just a command line, you can just run the Python by interpreter with the command line and you'll get exactly the same interface.

So let's go ahead and create a variable. For example, let's create an object. We will call it x, and we will assign it the number 10. So we type x by itself. The Python shell here will give us the value of x. So that's its value.

 

As we know that in python every object has an "id". It is a unique identifier that identifies this particular object. In the below screen shot we will find out the "id" of "x". And the id of "x" is 1568784544, generally the number looks like an address.

 

We also know that In python every object has a "type". Remember every thing in python is an object, and so objects have class. So the type of x is class int.

 

In the above screen shot the type of x is a class and so in this case its an integer.

All variables in Python are first- class objects, and what that means is that what might look like a simple variable may actually be something more complex. It could be an object that's been defined in a library, it could be a built-in object, it can be any sort of a thing, and it will oftentimes have attributes and methods, and we will get into the details of that later on in the course.
Today we learned about python object, variable, type and id. But for now what's important to understand is that everything in Python is an object, including variables, and as we look at variables and we look at the types of variables and how we use them, you need to realize that they are objects, and we will oftentimes be using syntax like this v.attribute or v.method to access the attributes and the methods within those objects. In my next post you will learn about mutable and immutable (Mutable and Immutable Objects) objects in python.


No comments:

Post a Comment