Data Handling - Ch-8
Data Handling - Ch-8
Class-11th
Chapter - 8
CORE DATA TYPES OF PYTHON
Data type
Boolean Tuple
List
Mutable and immutable data types
The Python data types are broadly classified into two types.
1. Mutable→ value can be changeable or modifiable.
2. Immutable→ value can not be changeable or non-modifiable.
X=5
y=6 Mutable data types immutable data types
if x==y:
Lists Integers
print(“ X value is equal to y”)
Dictionaries Floating point numbers
else: Sets Booleans
print(“ X value is not equal to y”) Strings
tuples
Number data types
The number data types are used to store numeric values in Python. These number
data types are :
i) Integers
a) Signed integers
b) Booleans
ii) Floating-point Numbers
iii) Complex Numbers
Integers data types
Signed integers Booleans
Integers are whole numbers . It is the normal integer These represent the truth
They have no fractional parts. representation of whole values False and True
number. behaves like the values 0
Integers cab be positive or negative. and 1.
Integers are immutable types (i.e. the
values cannot be changed or
modified) Python provides single data It is a sub type of the plain
type(int) to store any integer, integers.
Integers are represented in Python by whether big or small.
numeric values with no decimal point.
There are two types of integers in
python :
It is signed representation To get the Boolean
a) Signed integers means the integer can be equivalent of 0 or 1, you
b) Booleans positive as well as negative. type bool(0) or bool(1),
python returns False or True
The str() function converts a Boolean respectively .
value to string as ‘False’ or ‘True’.
String type data
A python string is a sequence of characters and each character can be individually
accessed using its index or ( also called subscript).
Strings in python are stored as individual characters in contiguous location, with two-way
index for each location.
Strings are immutable data type i.e. you can not change the individual letter / character of a
string in place by assignment.
The individual elements of a string are the characters contained in it those are stored in
contiguous memory locations.
Length of string can be determined using len( ) function.
String characters internally stored
Syntax : len(string) and addressed by its index.
Example: len(“Carmel”) output : 6
Two-way index for each location in a string are:
0 1 2 3 4 5
❑ Forward direction : ➔ 0, 1, 2, 3, 4, 5, ……….. ‘C’ ‘a’ ‘r’ ‘m’ ‘e’ ‘l'
❑ Backward direction: -1, -2, -3 , -4, -5, -6, ……….. -6 -5 -4 -3 -2 -1
Nm=“Banshika Das”
print(len(Nm)) output➔12
LISTS data type
List element Values internally stored
and addressed by its index.
A List in Python represents a list of comma-separated values of
any data type between square brackets i.e. [ ].
0 1 2 3 4
The List in Python is a compound data type.
1 2 3 4 5
A List can be changed/ modified . So it is a mutable. -5 -4 -3 -2 -1
Like any other value, you can assign a list to a variable.
When you assign values of a list in a variable, the values internally
are numbered from 0 onwards in memory. That is, 1st item of the 0 1 2 3 4
list is numbered 0, 2nd item of the list as 1, 3rd item of the list as 2, ‘a’ ‘e’ ‘i’ ‘o’ ‘u’
and so on. -5 -4 -3 -2 -1
Example:
>>> a=[1,2,3,4,5]
>>> VL=[‘a’, ’e’, ‘i’, ‘o’, ‘u’] >>>VL[0] ➔a 0 1 2
>>> ST=[‘Nikita’, 102, 78.5] ‘Nikita’ 102 78.5
-3 -2 -1
How to change any value in the LIST data type?
➢ To change first value in the above list namely a.
A List can be changed/ modified ( i.e.
mutable) Example: -1
Like any other value, you can assign a list to >>> a[0]=20 # first value is changed
a variable.
>>> print(a) # To see the changed values
Example:
[20,12,13,14,15]
➢ To see and check the values in a list.
Or
>>> a=[1,2,3,4,5] Or
>>>print(a)
>>> a >>>print(a)
[20,12,13,14,15]
[1,2,3,4,5] [1,2,3,4,5]
Example:-2
➢ To see and check individual values in a list.
➢ To change third value in the above list namely a.
>>> a=[10,12,13,14,15]
Example:
>>> a[0] >>>print( a[-3] )
>>> a[2]=30 # third value is changed
10 13
v>>> a[3] >>>print( a[-1] ) >>> a # To see the changed values
14 15 [10,12,30,14,15]
Tuples
Tuples are represented as a list of comma-separated values of any data type within
parentheses i.e. ( ).
Tuples are those lists which cannot be changed i.e. are not modifiable.
A Tuple can’t be changed/ modified . So it is an immutable.
Like any other value, you can assign a tuple to a variable.
When you assign values of a tuple in a variable, the values internally are numbered
from 0 onwards in memory. That is, 1st item of the tuple is numbered 0, 2nd item of
the tuple as 1, 3rd item of the tuple as 2, and so on.
Example:
>>> p=(1,2,3,4,5)
>>> Q=(‘a’, ’e’, ‘I’, ‘o’, ‘u’)
>>> S=(‘N’, ‘p’ 12, 7, 5.5)
Dictionary
The Dictionary is an unordered set of comma-separated key : value pairs, within { } , with the
requirement that within a dictionary.
No two keys can be the same ( i.e. there are unique keys within a dictionary).
A Dictionary can be changed/ modified . So it is a mutable.
Like any other value, you can assign a Dictionary to a variable.
Each element/ item value in a Dictionary is referred and accessed by its corresponding
key.
Python variable names are just the references to value-objects i.e. data
values.
The variable-names do not store values themselves i.e. they are not
storage containers.
Each time when you change the value, the variable’s reference memory
address changes.
Variables are not like storage containers i.e. with fixed memory address
where value changes every time. Hence, they are immutable.
Variable Internals
❑ Python is an object oriented language.
❑ Python calls every entity that stores any values or any type of data as an
object.
❑ So, all data or values are referred to as object in Python.
❑ An object is an entity that has :
➢ certain properties
➢ and that exhibit a certain type of behavior
❑ Every Python object has three attributes associated to it. These are:
➢ The type of an object
➢ The value of an object
➢ The id of an object
Attributes of every Python object
1. The type of an object 2. The value of an object
The type of an object determines the operations that ➢ It is the data item contained in the object.
can be performed on the object. ➢ For a literal, the value is the literal itself and for a
variable the value is the data-item it is currently
Built-in function type( ) ➔ This function returns the referencing.
type of an object. ➢ Using print() statement you can access and display
Example: value of an object.
Example: >>> a , b=5 , 14.5
>>> a=5
>>> print(a) output: 5
>>> type(5) output: <class ‘int’>
>>> print(b) output: 14.5
>>> type(a) output: <class ‘int’>
3. The id of an object
➢ The id of an object is generally the memory location of the object.
➢ Built-in function id( ) ➔ This function returns the id of an object i.e. the memory location of the object.
➢ The id( ) of a variable is same as the id( ) of value it storing.
➢ Remember that while storing complex numbers, id’s are created differently.
Example: >>> a=5 >>> a=4
>>> id(5) output: 30899132 >>> id(4) output: 30899130
OPERATORS
• Operators are tokens that trigger some computation/ action when applied to variables and other
objects in an expression.
• Operator requires some operands to work upon. Mainly there are types of operators:-
2 Unary minus (-) Bitwise Operators (3 operators) & (bitwise AND) , ^ (bitwise XOR) , | (bitwise OR)
3 Bitwise Complement (~) Shift Operators (2 operators) << (shift left) , >> (shift right)
Function/operation This operator (+) precedes an This operator (-) precedes an operand
operand which must have arithmetic which must have arithmetic type and
type and the result is the value of the result is the negation of its
the argument. operand’s value.
Sl. no Operators
1 Arithmetic operators (7 operators) +, -, *, /, %, **(exponent), // (floor division)
2 Bitwise Operators (3 operators) & (bitwise AND) , ^ (bitwise XOR) , | (bitwise OR)
3 Shift Operators (2 operators) << (shift left) , >> (shift right)
4 Identify Operators (2 operators) is , is not
5 Relational Operators (6 operators) < , > , <= , >= , == , !=
6 Logical Operators (2 operators) and , or
7 Assignment Operators (8 operators) = , /= , += , *= , %= , - = , **= , //=
8 Membership Operators (2 operators) in , not in
1. Addition Operator (+) 3. Multiplication Operator (*)
This (+)add operator adds values of its operands and This (*) operator multiplies the values of its
the result is the sum of the values of its two operands. operands.
The operands may be of number types. The operands may be of integer or floating point
number types.
Python also offers (+) as a concatenation operator
Python also offers (*) as a replication operator
when used with strings, lists and tuples.
when used with strings.
➢ Example: Example:
>>>A , B= 5 , 7 >>>a=57 >>>A,B=5,7 >>>a=7
>>>print(A+B) >>>b=13 >>>print(A*B) >>>b=11
output : 12 >>>a+b output: 70 output : 35 >>>a * b output: 77
Addition(+) -5 + 3 -2
Subtraction(-) -5 - 3 -8
Multiplication(*) -5 * 3 - 15
Exponentiation(**) -5 ** 3 -125
Modulus (%) -7 % 4 1
Augmented Assignment Operator (=)
Python has an assignment operator (=) which assigns the value specified on RHS ( Right hand Side) to the
variable / object on the LHS ( Left Hand Side) of the assignment operator (=).
Python also offers Augmented Assignment arithmetic Operators, witch combine the impact of an
arithmetic operator with assignment operator.
These operators can be used any where that ordinary assignment is used.
a-=b a=a-b Value of b subtracted to the value of a and then the result assigned to a.
a*=b a=a*b Value of b multiplied to the value of a and then the result assigned to a.
a/=b a=a/b Value of b divides the value of a and then the result assigned to a.
a // = b a = a // b Value of b does floor division to the value of a and then the result assigned to a.
a%=b a=a%b Value of b divides the value of a and then the remainder assigned to a.
Relational Operators
Relational refers to the relationships that values or operands can have with one other.
Python has Relational operators which determines the relation among different operands.
Python provides 6 relational operators for comparing values.
It checks / compares the values and returns Boolean value True when it satisfies otherwise; False.
Logical operators
Example:→ X Y R=X and Y X Y R=X or Y
x = True T T T T T T
y = False
print('x and y is', x and y) T F F T F T
print('x or y is', x or y) F T F F T T
print('not x is', not x)
Output F F F F F F
( x and y is False )
( x or y is True ) X R = not X
( not x is False )
T F
F T
Bitwise operators
These operators are used to manipulate bit values.
Operator Meaning Example
& Bitwise AND x& y Returns 1 if both the bits are 1 else 0.
| Bitwise OR x|y Returns 1 if either of the bit is 1 else 0.
~ Bitwise NOT ~x Returns one’s complement of the number.
^ Bitwise XOR x^y Returns 1 if one of the bits is 1 and the other is 0 else returns false (0).
Shifts the bits of the number to the right and fills 0 on voids left( fills 1 in the
>> Bitwise right shift x>> 2 case of a negative number) as a result. Similar effect as of dividing the
number with some power of two.
Shifts the bits of the number to the left and fills 0 on voids right as a result.
<< Bitwise left shift x<< 2
Similar effect as of multiplying the number with some power of two.
Bitwise operators continue Bitwise operators continue
>>> a, b, c= 6, 3, 0
>>> c = a ^ b
>>> print ('a=',a,':', bin(a), 'b=',b,':', bin( b) )
>>> print ("result of EXOR is ", c,':', bin(c))
>>> c = a & b
>>> c = ~a
>>> print ("result of AND is ", c,':', bin(c) )
>>> print ("result of COMPLEMENT is ",c,':‘, bin( c ) )
>>> c = a | b
>>> print ("result of OR is ", c,':', bin(c) )
Python Membership Operators
Test for membership in a sequence
Operator Description
in Evaluates to true if it finds a variable in the specified sequence and false otherwise.
not in Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.
a= 5
b = 10
list = [1, 2, 3, 4, 5 ]
output
if ( a in list ):
Line 1 - a is available in the given list
print ("Line 1 - a is available in the given list") Line 2 - b is not available in the given list
else:
print ("Line 1 - a is not available in the given list")
if ( b not in list ):
print ("Line 2 - b is not available in the given list")
else:
print ("Line 2 - b is available in the given list")
Operators Precedence : Highest precedence to lowest precedence table
Operator Description
~+- Complement, unary plus and minus (method names for the last two are +@ and -@)
* / % // Multiply, divide, modulo and floor division
+- Addition and subtraction
1. Write the corresponding Python expressions for the following mathematical expression:
a) 𝒂𝟐 + 𝒃𝟐 + 𝒄𝟐 ➔ math.pow(a,2)+math.pow(b,2)+math.pow(c,2)
𝒑
b) log 𝟏𝟎 + 𝒒 ➔math.log(10)+p/q
c) 𝒂𝟐 + 𝒃 𝟐 ➔
Working with standard Random Module in Python
Usage of the random module in Python →
The random module provides access to functions that support many operations.
Perhaps the most important thing is that it allows you to generate random
numbers.
A random number in simple words means – a number generated by chance, i.e.
randomly.
When to use it ?
We want the computer to pick a random number in a given range.
Pick a random element from a list, pick a random card from a deck, flip a coin
etc.
When making your password database more secure or powering a random page
feature of your website.
Random functions from Random Module
This function returns a random integer N in the range ( a, b) , i.e., a <= N <= b
( both range-limits are inclusive). Remember, it generates an integer.
Randint accepts two parameters: → a lowest and a highest number.
Generate integers between 1 , 5. The first value should be less than the second.
Example:- 1 To generate a random integer number in range 0 to 5.
import random
print(random.randint(0, 5))
This will output either 0 or 1 or 2 or 3 or 4 or 5.
Example:- 2 Find a random integer number between 15 to 35.
import random
print("random integer between 15 to 35 = " , random.randint (15 , 35) )
2. random() function
random() ➔ It returns or generates a random floating point number N in the
range (0.0, 1.0) , i.e. 0.0<= N < 1.0 .
Remember that the number generated with random( ) will always be less
than 1.0 , (only lower range limit is inclusive).
Note that , If you want a larger floating point number between range lower
to upper using random( ) :
You multiply random( ) with difference of upper limit with lower limit i.e.
formula : (upper limit – lower limit)
Then add the result with the lower limit .
Example →1 Find a random floating point number between 0.0 to 1.0 .
Sol n → Here, simply use random( ) to generate random floating point
number be tween 0.0 and 1.0.
import random
print(random.random())
Example: random() function
Example-2 Find a random number between 0 and 100.
import random
print(random.random() * 100)
OUTPUT
Enter the Principal value: 2000
Enter time period: 5
Enter Rate of interest: 10
The Simple interest = 1000.0
Q.1 Write a python program to read length (L) , Width (W) and Height (H) of a parallelogram and
calculate its area and perimeter.
Hint : Area= Length x Height
Perimeter =2 x Length + 2x Width
OUTPUT
Enter base/length of the parallelogram : 5
Enter width of the parallelogram : 4
Enter height of the parallelogram : 5
The Area of the parallelogram = 25.0
The Perimeter of the parallelogram = 18.0