Data Types1
Data Types1
Data Types1
CHAPTER : 3
DATA TYPE
Number data types are used to store numeric values. Numbers in Python can be of following
types:
(i)Integers
a) Integers(signed)
b) Booleans
(ii)Floating point numbers
(iii)Complex Numbers
INTEGERS
Integers allows to store whole numbers only and there is no fraction parts.
Integers can be positive and negative e.g. 100, 250, -12, +50
There are two integers in Python:
1) Integers(signed) : it is normal integer representation of whole numbers.
Integers in python can be on any length, it is only limited by memory available.
2)Booleans: it allows to store only two values True and False.
The internal value of boolean value True and False is 1 and 0 resp.
We can get boolean value from 0 and 1 using bool() function.
>>>bool(1) True
>>>int(False) 0
>>>str(False)
”False”
FLOATING POINT NUMBERS
Floating point number are mainly used for storing values like distance, area,
temperature etc. which have a fractional part.
Floating point numbers have two advantage over integers:
they can represent values between the integers
they can represent a much greater range of values
But floating point numbers suffers from one disadvantage also:
Floating point operations are usually slower than integer operations.
COMPLEX NUMBERS
>>>a=1+3.54j
Sequence
In Python string is a sequence of characters and each character can be individually access using index.
From beginning the first character in String is at index 0 and last will be at len-1.
From backward direction last character will be at index -1 and first character will be at –len.
Forward 0 1 2 3 4 5 6
indexing W E L C O M E
messag
e -7 -6 -5 -4 -3 -2 -1
Backward indexing
STRING
To access individual character of String (Slicing). we can use the syntax:
StringName[index position]
>>>stream=“Science”
>>>print(stream[0]) S
>>>print(stream[3]) e
>>>print(stream[-1]) e
STRING
>>>print(stream[5])
>>>print(stream[-4])
>>>print(stream[-len(stream)])
>>>print(stream[8])
LIST
Each element in list is accessed using value called index as we do in string(forward and backward).
The fist index value is 0, the second index is 1 and so on.
To access an element, use square brackets with the index [] value of that element.
>>> list1[1+4]
12
>>> list1[-1] #return first element from right
12
#length of the list1 is assigned to n
>>> n = len(list1)
>>> print(n)
6
#Get the last element of the list1
>>> list1[n-1]
12
TUPLE
create a dictionary
>>> dict1 = {'Fruit':'Apple', 'Climate':'Cold', 'Price(kg)':120}
>>> print(dict1)
{'Fruit': 'Apple', 'Climate': 'Cold', 'Price(kg)': 120}
#getting value by specifying a key
>>> print(dict1['Price(kg)'])
120
HOME WORK
What are data types? What are python built-in datatypes Which data type of python handles
Numbers?
Why Boolean considered a subtype of integer? Identify the data types of the values:
5, 5j, 10.6, “100‟, “100”, 2+4j, [10,20,30], (“a”,”b”,”c”),
{1:100,2:200}
What is the difference in output of the following ?
print(len(str(19//4)))
print(len(str(19/4))