CHAPTER - 7 Data Handling - Data Types
CHAPTER - 7 Data Handling - Data Types
Data Handling
Python is a dynamically typed language hence we need not define the type of the variable
while declaring it. The interpreter implicitly binds the value with its type. Variables can hold values
of different data types.
Python enables us to check the type of the variable used in the program. Python provides us
the type() function which returns the type of the variable passed.
A=10
b="Hi Python"
c = 10.5
print(type(a));
print(type(b));
print(type(c))
The real life data is of many types. So Python provides various standard data types that define
the storage method on each of them. Programming language provide ways and facilities to handle
these which are known as data types.
A. Numbers : Number stores numeric values in Python. Python creates Number objects when a
number is assigned to a variable. e.g. a = 3, b = 5 # a and b are number objects
1. Integers : Integers are whole numbers such as 7,-20, 0 etc. They have no fractional parts
i.e no decimal point. Integers can be positive or negative.
Plain integers (int) : Plain integers are stored in 32 bit wide memory space.
Long integers : Long integers are used to store numbers outside the range of
plain integers. There is no size limit to store a long integer in Python. Long
integers or longs are integers of unlimited size followed by an uppercase (L) or
lowercase (l).
E.g. 33L is a long integer whereas 33 is an integer.
Booleans : A Boolean in Python is used to represent one of the two values i.e
True or False. To get Boolean equivalent of 0 type bool(0) and for 1 type bool(1).
>>>bool(0) >>>str(True)
False ‘True’
>>>bool(1) >>>str(False)
True ‘False’
2. Floating point : Float is used to store floating point numbers or fractional part like 1.9,
9.902, 15.2, etc. In Python the floating point numbers have precision of 15 digits (double
precision). Floating point variables represent real numbers, which are used for
measurable quantities like distance, area, temperature etc.
b. Exponent Form : A real literal in Exponent form consists of two parts : mantissa
and exponent.
Valid real literals are : 2.0E08, 23E-3, 12E+2 etc.
Invalid real literals are : 2.E05, 23E, 1.2E2.5, 12,345E03, .25E-7 etc.
Advantages :
i. They can represent values between the integers.
ii. They can represent a much greater range of values.
Disadvantage :
i. Floating point operations are slower than integers operations.
Python displays complex numbers in parenthesis when they have nonzero real
part.
>>> c = 0+4.5j >>> d= 1.1 +3.4j
>>> print c >>> print d
Ans : 4.5j Ans : (1.1 + 3.4j)
Note : The real and imaginary parts of a complex number z can be retrieved through the
read only attributes z.real and z.imag. Python represents complex numbers as a pair of
floating point numbers.
>>> z=3+4j
>>> z.real Ans : 3.0
>>> z.imag Ans : 4.0
DataTypes Table
B. Strings : In python, strings can be created by enclosing the character or the sequence of
characters in the quotes. Python allows us to use single quotes, double quotes, or triple
quotes to create the string. A string can hold any type of known characters i.e letters,
numbers and special characters of any known scripted language.
Note : In python, strings are treated as the sequence of strings which means that python
doesn't support the character data type instead a single character written as 'p' is treated as
the string of length 1.
b. Unicode Strings : This string type holds strings containing Unicode characters. Unicode
code strings values are written as normal strings prefixed with letter u (not in quotes).
E.g. u ‘abc’, u “abc”, u ‘ भारत’ etc. Unicode character is atleast 2 Byte long.
For example, The string "HELLO" is indexed as given in the below figure.
Note : We cannot change the individual letters of a string in place by assignment because
strings are immutable and hence item assignment is not supported.
However, you can assign to a string another string or an expression that returns a string
using assignment i.e
Name = “RAM”
Name = “RAJ”
As shown in python, the slice operator [] is used to access the individual characters of
the string. However, we can use the : (colon) operator in python to access the substring.
Consider the following example.
c. LIST
Lists are similar to arrays in C and C++. A list can contain data of different types. The
items stored in the list are separated with a comma (,) and enclosed within square brackets
[]. Lists are mutable i.e can be changed/modified.
A tuple is similar to the list in many ways. Like lists, tuples also contain the collection
of the items of different data types. The items of the tuple are separated with a comma (,)
and enclosed in parentheses ().
Tuples are immutable i.e a tuple is a read-only data structure as we can't modify the
size and value of the items of a tuple.
>>> a=(1,2,3,4,5)
>>> print(a) ans : (1, 2, 3, 4, 5)
C. Dictionary