DATA HANDLING
DATATYPES
Datatypes are a means to identify the types of data.
Core datatypes in python
▪ Numbers
▪ Strings
▪ List
▪ Tuples
▪ Dictionary
NUMBERS
The number datatypes are used in Python to store numeric values.
Classification of Numbers:
▪ Integers
▪ Floating point numbers
▪ Boolean
▪ Complex numbers
INTEGERS
Integers may be defined as numbers without fractional part.
Integers may be both positive or negative.
Eg : 0, -6, 41, -743 etc
BOOLEAN
This datatypes is used to represent the Truth values True or False
1 True
0 False
>>> bool(0) >>> bool(1)
False True
FLOATING POINT NUMBERS
A number having fractional part are called floating point numbers.
Eg: 2.9, 6.78, -24.5, 12.0 etc.
Advantage of Floating point numbers over Integers:
▪ Floating point numbers may be used for representing integers
also.
Disadvantage of floating point numbers:
▪ Floating point operations are slower as compared to integer
operations.
COMPLEX NUMBERS
A number of the form A+Bi, where i represents an imaginary
number is called a complex number.
Complex numbers are represented in Python in the form A+Bj,
where j is an imaginary number. A and B represent real numbers.
Python displays complex numbers in parenthesis when they have a
non zero real part.
STRINGS
A string may be defined as a sequence of characters enclosed
within quotes.
A string can hold any type of known characters, i.e letters numbers
and special characters.
Eg:- “pqr” , “raj” , “76” etc.
LISTS
➢ Lists are used to store multiple items in a single variable.
➢ List is a sequence which can have any datatype values in it.
➢ It has comma separated values enclosed within square brackets [ ].
➢ The elements present in it are in sequence and list is Mutable.
➢ Mutable means changes are allowed in place.
Examples
#list1 is the list of six even numbers
list1 = [2,4,6,8,10,12]
#list2 is the list of vowels
list2 = ['a', 'e', 'i‘ ,'o‘ , 'u’]
#list3 is the list of mixed data types
list3 = [100, 23.5, 'Hello’]
#list4 is the list of lists called nested list
list4 =[['Physics’,69],['Chemistry’,56], ['Maths’,68]]
TUPLES
A Tuple may be defined as a list of comma separated values of any
datatype between first brackets.
The values within a list are enclosed within ( ).
Eg: (8,4,3,2,92), (‘Rahul’, 65, 1.4) etc.
Tuple are non mutable.
DICTIONARY
➢ In this the elements are stored in the form of Key and Value pair.
➢ Key Value is called as an items in Dictionary.
➢ The values within a dictionary are enclosed within { }.
➢ A key is separated from its value by a colon(:) and consecutive items
are separated by commas.
➢ Internally, dictionaries are indexed on the basis of keys.
OPERATORS
OPERATORS
Operators may be defined as the symbols that trigger the operation
on data.
Operands
Types of Operators:
1. Arithmetic operators
2. Relational operators
3. Identity operators
4. Logical operators
5. Bitwise operators
6. Membership operators
ARITHMETIC OPERATORS
Arithmetic operators are used in Python to do Arithmetic operations.
Eg: +,-,*,/,//,%,** etc
Types:
1. Unary operators:
➢ Unary+
➢ Unary-
2. Binary Operators
➢ Addition operator
➢ Subtraction operator
➢ Multiplication operator
➢ Division operator
➢ Floor Division operator
➢ Modulus operator
➢ Exponentiation operator
ASSIGNMENT & RELATIONAL OPERATORS IN PYTHON
ASSIGNMENT OPERATOR (=)
The assignment (=) is used in Python to assign values to a variable.
This operator takes the value or the computed value from RHS and
assigns it to the LHS.
Eg:- >>>a=5
>>>c=9+5
AUGMENTED ASSIGNMENT OPERATOR
➢ This operator is used to combine the impact of an arithmetic
operator with an assignment operator.
➢ Eg:- a= a+b a+=b
RELATIONAL OPERATOR
It refers to the relationship that values can have with one another.
Relational operators in Python
➢ Less than(<)
➢ Greater than(>)
➢ Less than or equal to(<=)
➢ Greater than or equal to(>=)
➢ Equal to(==)
➢ Not equal to(!=)
PRINCIPLES OF RELATIONAL OPERATOR
For numeric types, the values are compared after removing the trailing
zeroes after decimal point.
Smaller case alphabet has higher priority as compared to capital letters.
>>>’A’ > ’a’ >>> ‘a’ > ‘A’
False True
Spaces are real characters and they have a specific code.
>>> ‘Rahul’ == ‘ Rahul’
False
Two LISTS or two TUPLES are equal if they have the same elements in the
same order.
IDENTITY AND LOGICAL OPERATORS IN PYTHON
IDENTITY OPERATORS
Identity operators are used in Python to check whether the operands
points to the same object or not.
2 Identity operators in Python
➢ is operator
➢ is not operator
Is operator:-
➢ The is operator returns ‘True’ if both of its operands points to the same
object, otherwise it returns ‘False’.
Is not operator:-
➢ The is not operator returns ‘True’ if both its operands points to different
objects, otherwise it returns ‘False’.
LOGICAL OPERATOR
Logical operators are used to establish relationship among the operands.
Logical OR
Logical AND
Logical NOT
BITWISE OPERATORS
Bitwise operators work on binary representations of data.
Similar to logical operators with the difference that it works only on
Binary data.
Types of Bitwise operators:-
Bitwise AND
Bitwise OR
Bitwise XOR
Bitwise AND (&)
If both the bits evaluates to 1,then the result of AND operation is 1,
otherwise 0.