Python Data Presistence – Data Types

Python Data Presistence – Data Types

Data and information, these two words are so common nowadays – they are on the lips of everybody around us. But many seem to be confused about the exact meaning of these words. So much so, people use them almost as if they are synonymous. But they are not.
The computer is a data processing device. Hence, data is a raw and factual representation of objects, which when processed by the computer program, generates meaningful information.

Various pieces of data items are classified into data types. Python’s data model recognizes the following data types (figure 1.3):

Python Data Presistence - Data Types chapter 1 img 1

Number Types

Any data object having a numerical value (as in a mathematical context) is a Number. Python identifies integer, real, complex, and Boolean number types by the built-in type names int, float, complex, and bool respectively. Any number (positive or negative) without a fractional component is an integer, and the fractional component is afloat. The boolean object represents truth values True and False, corresponding to 1 and 0 respectively.

A number object is created with a literal representation using digit characters. Python has a built-in type ( ) function to identify the type of any object.

Example

>>> #this is an integer
. . . 100
100
>>> type ( 100 )
<class ‘ int ‘ >
>>> #this is a float
. . . 5.65
5.65
>>> type ( 5 . 65 )
<class ‘float’>
>>> #this is bool object
. . . True
True
>>> type ( True )
<class ‘ bool ‘ >

Any number with a fractional component (sometimes called the mantissa) is identified as a floating object. The fractional component is the digits after the decimal point symbol. To shorten the representation of a float literal with more digits after the decimal point, symbols ‘e’ or ‘E’ are used.

Example

>>> #this is float with scientific notation
. . . 1 . 5e – 3
0 . 0015
>>> type ( 1 . 5e – 3 )
<class ‘ float ‘ >

A complex number consists of two parts – real and imaginary – separated by ‘+’ or sign. The imaginary part is suffixed by ‘j’ which is defined as an imaginary number which is the square root of \(\sqrt{-1}\) (• A complex number is represented as x+yj.

Example

>>> 2 + 3j
( 2+3j )
>>> type ( 2+3j )
<class ‘ complex ‘ >

Arithmetic Operators

All number types can undergo arithmetic operations. Addition (‘+’), subtraction multiplication (‘*’) and division (7’) operators work as per their traditional meaning. In addition, few more arithmetic operators are defined in Python, which are:

  • Modulus or remainder operator (‘%’), returns the remainder of the division of the first operand by the second. For example, 10%3 returns 1.
  • Exponent operator (‘**’), computes the first operand raised to second. For example, 10**2 returns 100.
  • The floor division operator (7/’) returns an integer not greater than the division of the first operand by the second. For example, 9//2 returns 4.

Example

>>> #addition operator
. . . 10+3
13
>>> #subtraction operator
. . . 10-3
7
>>> #multiplication operator
. . . 10*3
30
>>> #division operator
. . . 10/3
3.3333333333333335
>>> #modulus operator
. . . 10%3
1
>>> #exponent operator
. . . 10**3
1000
>>> #floor division operator
. . . 10//3
3

Sequence Types

An ordered collection of items is called a sequence. Items in the sequence have a positional index starting with 0. There are three sequence types defined in Python.
1. String: Ordered sequence of any characters enclosed in single, double, or triple quotation marks forms a string object. Each character in the string object is accessible by index.

Example

>>> #string using single quotes
. . . ‘Hello. How are you?’
‘Hello. How are you?’
>>> #string using double quotes
. . .  “Hello. How are you?”
‘Hello. How are you?’
>>> #string using triple quotes
. . . ”’Hello. How are you?”’
‘Hello. How are you?’

2. List: An ordered collection of data items, not necessarily of the same type, separated by a comma and enclosed in square brackets [ ] constitutes a List object. The list is a sequence type because its items have a positional index starting from 0.

3. Tuple: A tuple is also an ordered collection of items, which may be of dissimilar types, each separated by a comma and enclosed in parentheses ( ). Again each item in tuple has a unique index.

Example

>>> [ ‘ pen ‘ , 15 , 25 . 50 , True ]
[ ‘pen ‘ , 15 , 25 . 5 , True ]
>>> type ( [ ‘ pen ‘ , 15 , 25 . 50 , True ] )
<class ‘ list’ >
>>> ( ‘ Python ‘ , 3 . 72 , ‘ Windows ‘ , 10 , 2 . 5E04 )
( ‘ Python ‘ ,  3 . 72 ,  ‘ Windows ‘ , 10 , 25000 . 0 )
>>> type ( ( ‘ Python ‘ ,  3 . 72 , ‘ Windows ‘ , 10 , 2 . 5E04 ) )
<class ‘ tuple ‘ >

Apart from the type of brackets – [ ] or ( ) – List and Tuple appear similar. However, there is a crucial difference between them – that of mutability. This will come up for explanation just a few topics afterward.

Mappings Type

A mapping object ‘maps’ the value of one object with that of other. Python’s dictionary object is an example of mapping. A language dictionary is a collection of pairs of words and corresponding meanings. Two parts of the pair are key (word) and value (meaning). Similarly, a Python dictionary is also a collection of key-value pairs, separated by a comma and is put inside curly brackets {}. The Association of the key with its value is represented by putting V between the two.

Each key in a dictionary object must be unique. The key should be a number, string, or tuple. (All are immutable objects). Any type of object can be used as the value in the pair. The same object can appear as the value of multiple keys.

Example

>>> {1:’one’, 2:’two’, 3 :’three’}
{1: ‘one’ , 2: ‘two’ , 3 : ‘three’}
>>> type({1:’one’, 2:’two’, 3:’three’})
<class ‘diet’>
>>> {‘Mumbai’:’Maharashtra’ ,
‘Hyderabad’:’Telangana’, ‘Patna’: Bihar ‘}
{‘Mumbai’: ‘Maharashtra’ , ‘Hyderabad’: ‘Telangana’,
‘Patna’: ‘Bihar’}
>>> type({‘Mumbai’:’Maharashtra’ ,
‘Hyderabad’:’Telangana’ , eclass 1 diet’> ‘Patna’: Bihar ‘})
>>> {‘Windows’: t’Windows XP’, ‘Windows 10 ‘ ] ,
‘Languages’: [‘Python’, ‘ Java’] }
{‘Windows’: [‘Windows XP ‘Windows 10’ 1 ,
‘Languages’: [‘Python’, ‘Java’]}
>>> type({‘Windows’:[‘Windows XP’ ‘Windows 10’],
‘Languages’ : [‘Python’, ‘Java’]})
<class ‘diet’>

 

Leave a Comment