0% found this document useful (0 votes)
23 views

CHAPTER - 7 Data Handling - Data Types

Uploaded by

abhishekbulani5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

CHAPTER - 7 Data Handling - Data Types

Uploaded by

abhishekbulani5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CHAPTER -7

Data Handling

Data Types in Python

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))

Output: <type 'int'>


<type 'str'>
<type 'float'>

Standard data types

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.

Python built-in data types are given below.


1. Numbers (integers, floating point, complex numbers, Booleans)
2. String
3. List
4. Tuple
5. Dictionary

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

Python supports 3 types of numeric data.

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’

Note : The str() function converts a value to string.

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.

These may be written in one of the two forms :


a. Fractional Form : A real constant in fractional form must have at least one digit
before a decimal point and at least one digit after the decimal point. It may also
have either positive (+) or negative (-) sign preceding it.
Valid real literals are : 2.0, +23.5, -45.34, -0.0034 etc
Invalid real literals are : 7, 9. , +12/4 , 13,56.90.84 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.

3. Complex numbers : A complex number is made up of both real and imaginary


components. A complex numbers is in the form of A+B j where A and B forms the real
numbers and j is imaginary.
e.g. z= 4 +3j where real component is 4 and imaginary component is 3
and j =√−𝟏 i.e j2 = -1.
Note :

 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

Type Size (in Bytes) Range


-2147483648 to +2147483647
OR
Plain integers 4 -2 to +231 -1
31

Long integers - Unlimited range, subject to available


(virtual) memory.
Boolean - Two values 0 (false) and 1 (true)
Floating point numbers - Unlimited range, subject to available
(virtual) memory.
Complex Numbers - Same a floating point numbers because
real and imaginary parts are represented
as floats.

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.

>>> str = "Hello"


>>> print(type(str)) then it will print string (str).

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.

Python offers two strings types :


a. ASCII strings (Normal Strings) : This string type holds strings of zero or more ASCII
characters enclosed between the quotation marks. E.g ‘abc’, “abc”, ‘ I said “Hello” to
you’ etc. ASCII character is 1 Byte long.

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.

Unicode is a system designed to represent every character from every language.


Strings indexing and splitting

A string is a sequence of characters and each character can be individually accessed


using its index. The index is the numbered position of a letter in the string which starts from
0. Strings in python are stored as individual characters in contiguous location with two way
index for each location i.e forward indexing (0,1,2,3...) and backward indexing (-1, -2, -3....)

For example, The string "HELLO" is indexed as given in the below figure.

To access any character use <stringname>[<index>]


>>>print (str[0]) ans : H
>>> print (str) ans : HELLO
>>> print (str[3]) ans : L
>>> print(str[-1]) ans : O
>>> print(str[-4]) ans : E

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.

e.g. name = “RAM”


name [2] = ’J’ will cause an TypeError : ‘str’ object does not support item
assignment.

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.

e.g. >>> a=[1,2,3,4,5]


>>> print(a) ans : [1, 2, 3, 4, 5]
>>> a[0] = 10
>>> print(a) ans : [10, 2, 3, 4, 5]
>>> a[2] = 30
>>> print(a) ans : [10, 2, 30, 4, 5]
D. Tuples

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

Dictionary is an unordered set of a comma separated key:value pairs and enclosed in


the curly braces {}. It is like an associative array or a hash table where each key stores a
specific value. Key can hold any primitive data type whereas value is an arbitrary Python
object.

You might also like