4.Python Data Type
4.Python Data Type
Numbers:
Number data type is used to store numeric values only. Number in python have the following
data types:
i. Integer Numbers
a. Integers (signed): numeric value with no decimal point. It can be + or - .
b. Booleans: These represents the truth value False and True. Boolean type is a subset
of Integer, Boolean value False and True behaves like the values 0 and 1,
respectively.
>>> bool(0)
False
>>> bool(1)
True
str(False)
'False'
str(True)
'True'
ii. Floating Point Numbers: A number having fractional part is a floating-point number.
Fractional form: 3.14, 344.5556
Exponent form: 3.50075E03, 0.5E-04
Advantage: They can represent values between the integers. They can represent a much
greater range of values
Disadvantage: Floating point operations are slower than integer operations
iii. Complex Numbers: Python represent complex numbers in the form of 3+ 4j
Python uses j in place of traditional i, so value of j= √−1 . Both real and imaginary
parts are of type float.
Example
a=0+2j
b=4+9j
print(a)
print(b)
output
2j
4+9j
z=(1+2.56j)
print(z.real)
print(z.imag)
output
real 1.0
imaginary 2.56
Sequence
i. Strings: A string can hold any type of data of known characters, letters, numbers and
special character. String values are enclosed either in single 'Hello' or double '"Hello"
quotes.
A string is a sequence of characters and each character can be accessed using its
index. The individual elements of a string are stored in continues memory locations.
The characters of the string are given two-way indices:
0, 1, 2…. forward direction
-1, -2, -3…..backward direction
Length of the string can be determined using the function len( )
0 1 2 3 4 5
P Y T H O N
subject=
-6 -5 -4 -3 -2 -1
Example
subject="ENGLISH"
print(subject[0])
print(subject[1])
print(subject[5])
print(subject[-3])
print(subject[-7])
print(subject[-1])
print("length:", len(subject))
output
E
N
S
I
E
H
length: 7
String are immutable, we cannot change the individual elements of a string.
Example
subject="ENGLISH"
subject[4]='s'
Output
TypeError: 'str' object does not support item assignment
output
hellohellohello
Example
s1="345"
s2="hello"
s3=s1+ s2
print(s3)
output
345hello
ii. Lists: List is a sequence of items separated by commas and items are enclosed in
square brackets [ ]. Once created, List elements can be changed or modified
(mutable).
Note that items may be of different date types.
Example
a=[1, 'hello',3,4.5,5]
print(a)
# to change the 1st value in the list
a[0]=10
print(a)
# to change the 3nd value in the list
a[2]=15
print(a, type(a))
a[1]='WELCOME'
output
[1, 'hello', 3, 4.5, 5]
[10, 'hello', 3, 4.5, 5]
[10, 'hello', 15, 4.5, 5] <class 'list'>
The values internally are numbered from 0(zero). 1 st item of the list is internally
numbered as 0, 2nd item of the list as 1, 3rd item as 2 and so on.
iii. Tuples: Tuple is a sequence of items separated by commas and items are enclosed in
parenthesis ( ). This is unlike list, where values are enclosed in brackets [ ]. Once
created, Tuples cannot be changed or modified (immutable). Similar to List, items
may be of different data types.
Example
student=(1,"Ravi",97.5)
print(student, type(student))
output:
(1, 'Ravi', 97.5) <class 'tuple'>
Mapping
i. Dictionary: The dictionary is an unordered set (non-sequential collections) of elements
in the form of a key: value pairs, within { }. In a dictionary no two keys can be same
(i.e., there are unique keys within a dictionary). To access the any value from a
dictionary we specify its key in square brackets[ ].
The values in the dictionary are (mutable)
The keys in the dictionary are (immutable types). so the dictionary can have
string, number, a tuple since these data types are immutable
Example
vowel={'a':1,'b':2,'c':3,'d':4}
print(vowel, type(vowel)))
print("The value in b is:", vowel['b'])
output