Chapter 7: Data Handling
Data types supported by Python:
1. Numbers
a. Integers (signed) – Positive and negative integers.
b. Booleans – Binary numbers 0 and 1 (True and False).
2. Floating point numbers
a. Fractional Form – 1023.60 , 0.0005 , 147.92
b. Exponential Form – 0.5E-04 , 3.5E03
3. Complex numbers
Complex numbers have the format – a+bj where both a and b are floating point
numbers
Example
1. a = 1+ 3.2j
print (a) -> (1.0 + 3.2 j) …. Parentheses around the number when displayed.
2. b = 0+4.2j
print (b) -> 4.2j …. NO Parentheses around the number when displayed if real part is 0.
A = -3.1 – 1j
a.real – gives the real part.
a.imag – gives the imaginary part.
print (a.real) -> -3.1
print (a.imag) -> -1.0
4. Strings
String can be declared using “”(double quotes) or ‘’ (single quotes). Contains
Unicode characters.
Example “Bob is crying” , ‘It is a funny world’ ,
String as a sequence of characters
Name= “ELEPHANT”
+ve Indexing from 0 to len(str) -1
0 1 2 3 4 5 6 7
E L E P H A N T
-8 -7 -6 -5 -4 -3 -2 -1
-ve indexing from -len(str) to -1.
Name[0] = ‘E’ = Name [-8]
Name[1] = ‘L’ = Name [-7]
Name[2] = ‘E’ = Name [-6]
Name[3] = ‘P’ = Name [-5]
Name[4] = ‘H’ = Name [-4]
Name[5] = ‘A’ = Name [-3]
Name[6] = ‘N’ = Name [-2]
Name[7] = ‘T’ = Name [-1]
Name [8] or Name [-9] will give me an Error : IndexError : string index out of range.
Individual letter assignment is not allowed in Python. – Name [0] = ‘h’ is not allowed.
It will give an error : TypeError: ‘str’ object does not support item assignment.
Changing the whole string is allowed – Name = “Lion” is allowed.
Lists and Tuples
Pythons compound data types
They are the same with just one difference, Lists(mutable) can be modified but tuple(immutable)
cannot be modified.
Lists Declaration – List can be a mix of any data types (int , float, character or string)
1. a=[1, 2, 3, 4, 5]
2. vowels=[‘a’ , ‘e’, ‘I’, ‘o’ , ‘u’]
3. mixed= [‘Study’ , 109, 89.5]
Element of list is access using the index the same way as in Strings.
print (a[0]) -> 1
print (vowel [2]) -> ‘I’
List values can be changed.
a[3] = 7
print (a)
will result in
[1, 2, 3, 7, 5]
Note: brackets will be printed along with elements when a list is printed to indicate it is a list
Tuples declaration
Tuples is the same as lists in declaration and usage
1. a=(1, 2, 3, 4, 5)
2. vowels=(‘a’ , ‘e’, ‘I’, ‘o’ , ‘u’)
3. mixed= (‘Study’ , 109, 89.5)
Element of list is access using the index the same way as in Strings.
print (a[0]) -> 1
print (vowel [2]) -> ‘I’
print (vowel) -> (‘a’ , ‘e’, ‘I’, ‘o’ , ‘u’)
Tuples values cannot be changed.
Note: brackets will be printed along with elements when a tuple is printed to indicate it is a tuple
Tuples and Lists difference.
1. Lists are declared using [] and tuples are declared using ()
2. Lists single value can be changed (mutable) but tuples single value cannot be
changed(immutable)
Sets Declaration
Sets are declared using {}
Sets elements are unordered and unindexed.
Sets does not allow duplicate element. I added by mistake it removes it.
Sets cannot contain mutable elements.
1. a={1, 2, 3, 4, 5, 5 , 4}
2. vowels={‘a’ , ‘e’, ‘I’, ‘o’ , ‘u’}
3. mixed= {‘Study’ , 109, 89.5}
print (a) -> {1, 2, 3, 4, 5} …printed with curly brackets and duplicate elements are removed.
type (a) -> <class ‘set’>
Dictionary declaration
Dictionary is an unordered set of comma separated key:value pairs with a requirement that no
two keys are the same.
vowels={‘a’ : 1, ‘e’ : 3, ‘I’ : 4, ‘o’ : 5 , ‘u’ : 2}
Element of dictionary are accessed using the key as index.
print (vowel [‘a’]) -> 1
print (vowel [‘o’]) -> 5
print (vowel) -> {‘a’ : 1, ‘e’ : 3, ‘I’ : 4, ‘o’ : 5 , ‘u’ : 2}
Mutable and immutable
Mutability means that in the same memory address, new value can be stored as and when you
want.
Integers, Float, Booleans, strings and tuples are all immutable types
Lists , Dictionary and Sets are all mutable types.
Variable Internals definitions:-
1. Python is an object oriented language.
2. An object is an entity that has certain properties and that exhibit a certain type of behavior.
Example: Integer values are objects. They have their own properties. They support all arithmetic
operation (behaviour)
i. The type of an object – it determines the operations that can be performed on the object.
a= 4
type (a) -> <class ‘int’>
ii. The value of an object – It is the data item contained in the object.
a= 4
print (val(a)) -> 4
iii. The id of an object – It is the memory location of the object.
a =4
print (id (a)) -> 3089122
print (id(4)) -> 3089122
Type casting of variables
The conversion of one data type into the other data type is known as type casting. Two types
- implicit - done by python itself during execution.
- explicit – done by the programmer.
Int(): function take float or string as an argument and returns int type
object.
float(): function take int or string as an argument and return float type
object.
str(): function takes float or int as an argument and returns string type
object.
Complex() : function takes (1 or 2) int as an argument and returns complex
type object.
Bool() : function takes int, float, string as an argument and returns False for 0,
“” , ‘’ else True.
Operators:
1. Arithmetic operators:
a. Unary (+ and -)
b. Binary (involving two operands)
Operator Name Example
+ Addition x+y
- Subtraction x–y
* Multiplication x*y
/ Division x/y 5/2 = 2.5
% Modulus x%y 5%2 = 1
** Exponentiation x ** y 5 **2 = 25
// Floor division x // y 5//2 = 2
c. Augmented Assignment operators
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x–3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3 AND operator
|= x |= 3 x=x|3 OR operator
^= x ^= 3 x=x^3 XOR operator
>>= x >>= 3 x = x >> 3 Right shift
<<= x <<= 3 x = x << 3 Left Shift
d. Comparison operators
Operator Name Example
== Equal x == y (Doesn’t work with
floating point numbers)
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or x >= y
<= Less than or equal x <= y
e. Identity operators – not good for strings.
is – True when both variables refer to the same memory address
is not – True when both variables refer to different memory address
f. Logical Operators
Operator Description Example
and Returns True if both statements are true x < 5 and x < 10
or Returns True if one of the statements is true x < 5 or x < 4
Reverse the result, returns False if the result
not not(x < 5 and x < 10)
is true
g. Bitwise operators
OPERATOR NAME DESCRIPTION SYNTAX
Bitwise Result bit 1,if both operand bits are
& x&y
AND 1;otherwise results bit 0.
Result bit 1,if any of the operand bit
| Bitwise OR x|y
is 1; otherwise results bit 0.
Bitwise
~ inverts individual bits ~x
NOT
Results bit 1,if any of the operand bit
Bitwise is 1 but not both, otherwise
^ x^y
XOR
results bit 0.
The left operand’s value is moved
Bitwise right toward right by the number of bits
>> x>>
shift
specified by the right operand.
The left operand’s value is moved
Bitwise left toward left by the number of bits
<< x<<
shift
specified by the right operand.
Standard Libraries:
Import the library first:
1. Math Library
2. Random library
- random.random()- Return the next random floating point
number in the range 0.0 <= X < 1.0
- random.randint(a, b) - Return a random integer N such that
a <= N <= b.
3. Statistics library
- Statistics.mean(<seq>)
- Statistics.median(<seq>)
- Statistics.mode(<seq>)
Where <seq> is a list of numbers
Seq = [10,12,19,20,2,5,6,7,100]