Python variables or object

To start writing program in Python you will certainly need to use data such integers,characters,strings(a sentence),etc. Suppose you want to use an integer in your program we will given a name and then assign it with any integer value,for instance.

>>> size=12

The name ‘size’ here is known as an ‘object’ and the value 12 is assigned to ‘size’and note the object ‘size’ is of ‘int’ type meaning integer.Consider another example.

>>> text="New world!!"

In the above code the sentence “New world” is assign to ‘text’ object,the ‘text’ here is a string type(string simply means a text or a sentence) because we have assigned a string value to it.We can say that the type of the object is determine by the type of the value assigned to it.


Changing the object type

In the above we have seen that the type of the object is determined by the type of the value assigned to it.But note this does not mean the type of the object will remain constant,the type can be changed immediately by assignng a different type value to the object.

>>> text="New world" #'text' type is string type
>>> text=12345 #'text' type is now integer type

The above example clearly shows how fickle the object type is in Python.


Object name

Some points to note while giving a name to an object.

 
i)An object name must never start with a number.

ii)An object name must start with a letter or the underscore character(_).

iii)An object name must only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).

iv)Remember object names are case-sensitive meaning lower-case(small letter) and upper-case(capital letter) are treated differently.(new, new and NeW are three different object)

>>> new=12
>>> New=23 #different from the first one
>>> NeW=28 #different from the first and second

How to output an object value

You can output the value of any object on the screen by using the statement ‘print‘.

  
>>> text="New world"
>>> print(text)

Press Enter and you will get,
 
New World

Here is another example.

>>> new=12
>>> New=23 #different from the first one
>>> NeW=28 #different from the first and second
>>> print(new , New , NeW )

Press Enter and the output is,

12   13   23

Which also clearly suggest that new,New and NeW are completely different objects.

 

Using Python as calculator

Even without using an object name we can perform some operations in python which makes it behave like a calculator.For instance,

>>> 34+2 #addition
36
>>> 23-10 #subtraction
13
>>> 24/4 #'/' is used for division
6
>>> 40%3 # '%' returns the remainder
1
>>> 45/5 +9
18.0



Leave a Reply

Your email address will not be published. Required fields are marked *