Data Handling Part 1 Ip
Data Handling Part 1 Ip
3) Complex numbers
Integers
Eg - +12, -15, 300
There are two types of integers in Python:
1) Integers(signed) –
Disadvantages:
1) Floating point operations are slower than integer
operations.
Note: In python ,the floating point numbers have precision
Complex numbers:
Complex numbers are of the form a+bj, where a and b are
floats and j represents (which is an imaginary number)
>>>c=0+4.5j
>>>d=1.1+3.4j
>>>c
4.5j
>>>d
(1.1+3.4j)
>>>print(c)
4.5j
>>>print(d)
(1.1+3.4j)
To retrieve the two components for a complex number z
we can do as follows:
>>>z=(1+2.56j)+(-4-3.56j)
>>>z
(-3 -1j)
>>>z.real
-3.0
>>>z.imag
-1.0
The Range of Python numbers:
Strings
In python each character stored in a string is a Unicode
character.
-9 -8 -7 -6 -5 -4 -3 -2 -1
>>>name=‘hello’
>>>name=‘new’
List and Tuples
List and tuples are basically of the same types
with one difference.
>>>a[0]=10
>>>a
[10,2,3,4,5]
>>>a[2]=30
>>>a
[10,2,30,4,5]
Tuples
Tuples cannot be changed/modified(ie. Immutable)
p=(1,2,3,4,5)
q=(2,4,6,8)
r=(‘a’, ‘e’, ‘i’, ‘o’, ‘u’)
h=(7,8,9,’A’,’B’,’C’)
Note:
Python data types strings, lists, tuples, dictionary ,etc are
all iterables.
An iterable is any Pyhton object that can return its
members, one at a time. Since you can access elements
of strings, lists, tuples and dictionary one at a time, these
are called iterables.
Dictionary
Dictionary data type is an unordered set of
comma-separated key : value pairs, within {}
No two keys can be the same.
Example –
{‘a’ : 1, ‘e’ : 2, ‘i’ : 3, ’o’: 4, ’u’ : 5}
>>>vowels={‘a’ : 1, ‘e’ : 2, ‘i’ : 3, ’o’: 4, ’u’ : 5}
Here ‘a’ ‘e’ ‘i’ ‘o’ ‘u’ are keys of dictionary vowels
1,2,3,4,5 are values of these keys respectively
>>>vowels[‘a’]
1
>>>vowels[‘u’]
5
Mutable and Immutable types
In Python data objects can be classified into 2
categories –
Immutable(non-modifiable).
Immutable types
Immutable types are those that can never change their
value in place.
In python, the following types are immutable:
Integers
Floating point numbers
Booleans
Strings
Tuples
Example
p=5
q=p
r=5
. #will give 5,5,5
.
.
p =10
r= 7
q= r
Since p,q,r are integer values you may think that they can change
values ,but its not the case .
•Python variable–names are just the references to value-
objects . ie. Data values.
•The variable names do not store values themselves ie.
They are not storage containers.
•So although it appears that the value of variable p,q,r is
changing ; values are not changing in place.
•The fact is that the variable names are instead made to
refer to new immutable integer object.
•Changing in place means modifying the same value in
same memory location.
When p=5 q=p r=5
Internally python keeps a
count of how many variables
are referring to a value
(3
)
5 6 7 8 9 10
20200 20216 20248
20232 20264 20280
p q r
>>>id(p)
>>>p =5
20200
>>>q=p
>>>id(q)
>>>r=5
20200
>>>id(5)
>>>id(r)
20200 20200
When p = 10 r=7 q=r
Currently two
identifiers/variables are
referring to this value
(2 (1
) )
5 6 7 8 9 10
20200 20216 20248
20232 20264 20280
p q r >>>id(r)
>>>id(p) 20232
>>>p =10
20280 >>>id(5)
>>>r=7
>>>id(7) 20200
>>>q=r
20232 >>>t=5
>>>id(10)
>>>id(q) >>>id(t)
20280 20232 20200