Chapter 7 Data Handling
Chapter 7 Data Handling
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.
4. Strings
Name= “ELEPHANT”
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
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’
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
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’)
Sets Declaration
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.
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:-
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
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
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
** Exponentiation x ** y 5 **2 = 25
= 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 OR operator
d. Comparison operators
!= Not equal x != y
f. Logical Operators
and Returns True if both statements are true x < 5 and x < 10
Bitwise
~ inverts individual bits ~x
NOT
3. Statistics library
- Statistics.mean(<seq>)
- Statistics.median(<seq>)
- Statistics.mode(<seq>)