Pages

Sunday, July 12, 2015

Python – Data Types - 1

A data type or simply type is a classification identifying one of various types of data, such as realinteger or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of that type can be stored

Data Types in Python can be classified into

  • Simple types -- The basic building blocks, like int and float
  • Container types -- Hold other objects like List, Tuple, Set
  • Code types -- Encapsulate the elements of your Python program
  • Internal types -- Used during program execution

Simple Types – We will start with the Simple Types that are available in Python. Simple types in Python are

Bool
Int
Long
Float
Complex

Before Starting to understand the Simple types in Python , there are 2 functions available in python which will help to identify the Python types. They are

Id()  - find the memory location allocated for the variable
Type() – Find the type of the variable

These 2 will be used in finding many while working with the Data Types in Python

Boolean – python bool type holds either True or False.  Python Bool provides only 2 objects : True and False. Many programs utilize the Boolean expressions which result in True or False
>>> b = True
>>> id(b)
47707397625616
>>> type(b)
<type 'bool'>
>>> bb = True
>>> id(bb)
47707397625616

If we check the above snippet, we can see that when both “b” and “bb” are assigned to True, both points to same memory location.
Boolean Operators are used in many areas of Python as

The Boolean comparison operators in Python
Operator
Description
Example
< 
less than
i < 100
<=
less than or equal to
i <= 100
> 
greater than
i > 100
>=
greater than or equal to
i >= 100
==
equality
i == 100
!=
inequality (also <>)
i != 100

Below are list of the Logical Operators in Python

Operator
Description
Example
not
logical negation
not b
and
logical and
(i <= 100) and (b == True)
or
logical or
(i < 100) or (f > 100.1)


Numeric Types -int, long, float, and complex are the numeric types available in Python. Python provides full support for arithmetic operations, including addition, subtraction, multiplication, and division 

How much Memory a Numeric Data Type use?
Unlike java and other language where there are limitations to the types defines, there are none in Python. They are based on the platform. So an int in python is a 32bit so the holding values may range from -2(32) to 2(32)-1. But when you go to the long values it had a unlimited prevision based on the memory limitations of the platform. An Integer can be saved as int and float in a python . in order to treat a integer as an long we need to add L at the end of the value as 100L

Python provides support for octal (base 8) and hexadecimal (base 16) numbers. To tell Python that a number should be treated as an octal numeric literal, simply append a zero to the front. Appending a zero and an x to the front of a number tells Python to treat the number as a hexadecimal numeric literal,

>>> print 127
127
>>> print 0127
87
>>> print 0x12
18

complex type - complex number has a real and an imaginary component, both represented by float types in Python. An imaginary number is a multiple of the square root of minus one, which is denoted by ior j

>>> print c
(3+1.2j)
>>> print c.real,c.imag
3.0 1.2

No Primitive Types in Python, Just Objects
One very important thing to keep in mind is that there are no primitive types in Python. All are objects in Python.

Consider the Simple Type “int” above. Int in Python is not a Primitive type it’s a full-fledged Object with its own methods and classed.

So as in java we don’t have int I =10 , where a memory location is created with a int primitive type . So when we use I = 10 , a Python will create a Python object with a memory allocated to that with value I.

One more thing to keep in mind is that these simple built-in types are immutable, which means you can't change an object's value after the object has been created. If a new value is needed, you must create a new object

>>> i = 10
>>> id(i)
7240752
>>> i =100
>>> id(i)
7242576

But if we create a new object for every value change, what happens to the old one?
We may at this point think that what will happen to the old one when left alone and that can cause Memory leaks.

Like java and other high level language, Python also use Garbage Collector that frees up memory used to hold objects that are no longer referenced like the above one.

Are they really are Objects?
As we already discussed that every thing in Python is an Object but the example discussed above does not have any Objects declarations. For the Simple data types , Python does a lot of work for you like creating an Object etc. Every Simple Data type does contain a Object Constructor that are identical to the name of the relevant data type.

We can use

>>> b = bool (True)
>>> print b
True
>>> i =int(10)
>>> print i
10

Similarly with other Simple Types we have the Constructors available that can be used in initializing like,
>>> l = long(100)
>>> f = float(100.1)
>>> c = complex(3.0, 1.2)


Deletion of Variable
Python provides a feature which is not available in many high level languages called “del”.
Using this we can delete a reference to a number object.

 The syntax of the del statement is −
del var1[,var2[,var3[....,varN]]]]

You can delete a single object or multiple objects by using the del statement. For example −

del var

No comments :

Post a Comment