CH 7&8 DataHandling & Module
CH 7&8 DataHandling & Module
DATA HANDLING
Jini N K
BUILT-IN CORE DATA TYPES
STRING TUPLE
NUMBERS LIST
DICTIONARY
Jini N K
NUMBERS Used to store numeric values
Fractional Form
Integers
Exponent
Booleans
Jini N K
THE RANGE OF PYTHON NUMBERS
DATA TYPE RANGE
Jini N K
STRINGS
Any number of valid characters into a
set of quotation marks
Jini N K
STRINGS IN PYTHON
Strings in Python are stored as individual
characters in contiguous locations, with
two way index for each location
Forward Indexing
0 1 2 3 4 5
S L E S S O N
-6 -5 -4 -3 -2 -1
Backward Indexing
Jini N K
ACCESSING SINGLE ELEMENT OF A STRING
0 1 2 3 4 5
name L E S S O N
-6 -5 -4 -3 -2 -1
Jini N K
STRINGS ARE IMMUTABLE
Jini N K
LISTS IN PYTHON
• List stores a sequence of values of any data
type
Jini N K
ACCESSING SINGLE ELEMENT OF A LIST
Forward Indexing
0 1 2 3 4 5
L1 C H E R R Y
-6 -5 -4 -3 -2 -1 Backward Indexing
Jini N K
LISTS ARE MUTABLE
Elements of the list can be changed. Can add, change, delete a list’s
elements
Jini N K
TUPLES IN PYTHON
• Tuple stores a sequence of values of any data type
• The tuples are depicted through parentheses
Eg:
T=(‘Q’, ‘h’, ’joy’, ’14’, 30.07)
T=( )
T=(5,10,15,20,25)
Jini N K
SETS
• Created by specifying comma separated values
enclosed in curly brackets
• Set is mutable
Jini N K
Jini N K
DICTIONARIES IN PYTHON
• Collection of key-value pairs separated by commas
• Mutable, unordered collections that associate keys to
values
• Dictionaries are enclosed within curly brackets
Jini N K
Jini N K
PYTHON DATA OBJECTS
Mutable Immutable
• Values can be changed in place • Values cannot be changed in place
• Lists, Dictionaries, Sets • Integers, Floating point numbers,
Booleans, strings, Tuples
Jini N K
IMMUTABLE
10 15 20 21 40 55
…280 …284 …290 …312
146003692 14003700
8 8
a b c
a=10
b=a
c=15
Jini N K
Jini N K
VARIABLE INTERNALS
• Python is an Object Oriented Language
• Everything is an object in Python
• Data items, literal value, strings, numbers, data types,
variables → Objects
• An object is an entity that has certain properties and
that exhibit a certain type of behavior
Jini N K
EXAMPLES
1. What is the output of the following code?
2. Why did the code not print the output exactly as the input?
Jini N K
EXAMPLES - ANSWERS
1. What is the output of the following code?
2. Why did the code not print the output exactly as the input?
Sets ignore duplicate values
3. How are following two variables created using { }
different from one another?
V1={11,12,13,14} V2={11:12, 13:14}
Jini N K
V1 is a set and V2 is a dictionary as it has key : value pairs
& |
^ ~
Jini N K
OPERATORS
The symbols that trigger the operation/action on data
OPERANDS
The data on which operation is being carried out
OPERANDS
OPERATOR
Jini N K
UNARY OPERATORS Operators that act on one operand
Jini N K
Binary Operators
Operators that act upon two operands
+ - *
Addition Operator Subtraction Operator
Multiplication
Operator
Jini N K
/ // %
Floor Division
Division Operator Modulus Operator
Operator
Divides and returns the Divides and truncates Divides and gives the
result as a float value the fractional part. remainder
Rounds to the lowest
value if negative
Jini N K
**
Exponentiation Operator
Jini N K
EXAMPLES
Jini N K
EXAMPLES
1. What will be the output produced by the following code?
Jini N K
EXAMPLES
2. Print the area of a circle of radius 3.75 metres
Jini N K
AUGMENTED ASSIGNMENT OPERATORS
+= a+=1 → a=a+1
//= p//=10 → p=p//10
-= b-=5 → b=b-5
**= x**=5 → x=x**5
*= x*=y → x=x*y
%= m%=5 → m=m%5
/= a/=7 → a=a/7
True is equivalent to 1
Boolean
False is equivalent to 0
Jini N K
a=9, b=15, p=9.0
c= ‘n’, d=‘g’, e=‘N’
f=‘god’, g=‘God’, h=‘god’, j=‘God’, k=‘Godhouse’
L=[1,2,3], M=[2,4,6], N=[1,2,3]
O=(1,2,3), P=(2,4,6), Q=(1,2,3)
Jini N K
a=9, b=15, p=9.0
c= ‘n’, d=‘g’, e=‘N’
f=‘god’, g=‘God’, h=‘god’, j=‘God’, k=‘Godhouse’
L=[1,2,3], M=[2,4,6], N=[1,2,3]
O=(1,2,3), P=(2,4,6), Q=(1,2,3)
Jini N K
It is not recommended to use == operator
to compare two floating point values
Jini N K
IDENTITY OPERATORS
Values with truth value as false Values with truth value as false
None
False
All other values are considered
0, 0.0, 0j
true
Empty sequence [],(), ‘’
Empty mapping {}
Jini N K
LOGICAL OPERATORS
Used to combine expressions
or
and
not
Jini N K
THE or OPERATOR
• Combines two expressions
• Works in two ways
p q p or q
False False False
False True True
True False True
Jini N K
True True True
Jini N K
(ii) Numbers / Strings / Lists as operands
x Y x or y
false false y
false true y
true false x
true true x
Jini N K
Operation Result
0 or 0 0
0 or 4 4
6 or 0.0 6
‘hello’ or ‘’ ‘hello’
‘’ or ‘g’ ‘g’
‘’ or ‘’ ‘’
‘s’ or ‘j’ ‘s’
Jini N K
THE and OPERATOR
• The or operator evaluates to True if both of its operands evaluate to True
• False if either or both operands evaluate to False
p q p and q
False False False
False True False
True False False
True True True
Jini N K
Jini N K
Jini N K
(ii) Numbers / Strings / Lists as operands
x Y x and y
false false x
false true x
true false y
true true y
Jini N K
Operation Result
0 and 0 0
0 and 4 4
6 and 0.0 0.0
‘hello’ and ‘’ ‘’
‘’ and ‘g’ ‘’
‘’ and ‘’ ‘’
‘s’ and ‘j’ ‘j’
Jini N K
THE not OPERATOR
Jini N K
BITWISE OPERATORS
Used to change individual bits in an operand
&
^
~
Jini N K
Operato
Operation Use Description
r
Results 1 if both bits are
& bitwise and op1 & op2
1; otherwise returns 0
Results 1 if either of the
| bitwise or op1 | op2 bits are 1; results 0 if both
bits are 0 or 1
Results 1 if the bits are
^ bitwise xor op1 ^ op2 complementary;
otherwise returns 0
bitwise Inverts all of the bits of
Jini N K
~ complement
~op1
the operand
THE AND OPERATOR &
Jini N K
THE INCLUSIVE OR OPERATOR |
Jini N K
THE EXCLUSIVE OR OPERATOR ^
For complement
operations, if the operand op1 Result
bit is 1 the result is 0 and 0 1
if the operand bit is 0 the 1 0
result is 1.
Jini N K
OPERATOR PRECEDENCE
Operator Description
() Parantheses
** Exponentiation
+, - Addition, Subtraction
or
Jini N K
Logical OR
Operator Description
() Parantheses
8*7/4//3 **
+x, -x
Exponentiation
Positive, Negative
* , / , //, Multiplication,
56/4//
56/4//3 % Division, Floor
Division, Remainder
+, - Addition, Subtraction
4 is, is not
not Logical NOT
and Logical AND
or Logical OR
Jini N K
Operator Description
() Parantheses
** Exponentiation
len(“Explore”)==49/7 or 50/10 Positive, Negative
+x, -x
* , / , //, Multiplication,
7 == 49/7 or % Division, Floor
Division, Remainder
50/10 +, - Addition, Subtraction
<, <=, >, Comparison Operators
7 == 7.0 or 50/10 >=, !=, == Identity Operators
is, is not
7 == 7.0 or 5.0 Logical NOT
not
True or 5.0 and Logical AND
or Logical OR
Jini N K
Operator Description
() Parantheses
len(“Explore”)==49/7 or 50/10 ** Exponentiation
+x, -x Positive, Negative
8*2+5**2//5-8 **
+x, -x
Exponentiation
Positive, Negative
* , / , //, Multiplication,
8*2+25//5-8 % Division, Floor
Division, Remainder
16+25//5-8 +, - Addition, Subtraction
<, <=, >, Comparison Operators
16+5-8 >=, !=, == Identity Operators
is, is not
21-8 not Logical NOT
13 and Logical AND
or Logical OR
Jini N K
Operator Description
() Parantheses
True and False or not True <, <=, >, Comparison Operators
Identity Operators
>=, !=, ==
True and False or False is, is not
False or False not Logical NOT
and Logical AND
False Logical OR
Jini N K
or
Operator Description
() Parantheses
** Exponentiation
5<7 or 50 / (10 – (8+2) ) +x, -x Positive, Negative
* , / , //, Multiplication,
5<7 or 50/(10-10) % Division, Floor
Division, Remainder
5<7 or 50/0 +, - Addition, Subtraction
<, <=, >, Comparison Operators
5<7 or DivisionByZeroError >=, !=, == Identity Operators
is, is not
True or DivisionByZeroError Logical NOT
not
True and Logical AND
or Logical OR
Jini N K
Operator Description
() Parantheses
“xyz”==“Xyz” and not ** Exponentiation
Jini N K
EXAMPLES
1. What will be the output produced by the following code?
Jini N K
2. What will be the output produced by the following code?
Jini N K
2. What will be the output produced by the following code?
Jini N K
3. What will be the output produced by the following code?
Jini N K
3. What will be the output produced by the following code?
Jini N K
4. What will be the output produced by the following code?
Jini N K
4. What will be the output produced by the following code?
Jini N K
5. What will be the output produced by the following code?
Jini N K
5. What will be the output produced by the following code?
Jini N K
6. Why is the following code giving error?
Jini N K
7. Which of the following expressions will yield an integer type
value as its output
(i) 5 * 2 → 10
(ii) 5**2 → 25
(iii) ‘5’+’2’ → ‘52’
(iv) ‘5’*2 → ‘55’
(v) 5/2 → 2.5
(vi) 5//2 →2
(vii) 5%2 →1
(viii) 5+2.0 → 7.0
(ix) 5.0*2.0 → 10.0
(x) ‘5’-2 → Type Error
Jini N K
EXPRESSIONS Any valid combination of operators,
literals and variables
Jini N K
Jini N K
Jini N K
Jini N K
int()
str()
float()
bool()
complex()
Jini N K
Which of the following expressions will yield a Boolean type
value as its output?
Jini N K
Write Boolean expressions in Python for these conditions.
Jini N K
Write Boolean expressions in Python for these conditions.
(i) x % y == 0
Jini N K
WORKING WITH math MODULE OF PYTHON
import math
ceil math.ceil(num) Returns the smallest integer not less than num
Jini N K
exp math.exp(arg) Returns the natural logarithm e raised to the arg power
floor math.floor(num) Returns the largest integer not greater than num
Jini N K
log math.log(num,[base]) Returns the natural logarithm for num
Jini N K
Returns the sine of arg. The value of
sin math.sin(arg) arg must be in radians.
Jini N K
The degrees() converts angle x from
degrees math.degrees(x) radians to degrees
Jini N K
Constants ⫪ and e
Jini N K
Write the corresponding Python expressions for the
following mathematical expressions.
(v) |e2 – x|
Jini N K
(i) math.sqrt(a*a + b*b + c*c)
(ii) 2 - y * math.exp(2 * y) + 4 * y
(v) math.fabs(math.exp(2) – x)
Jini N K
The value of a sphere is 7.5 metres.
Write Python script to calculate its area and volume.
(Area of a sphere = 4⫪r2; Volume of a sphere= 4/3⫪r3)
Jini N K
USING RANDOM MODULE
import random
Jini N K
randint(a,b)
Returns a random integer N in the range (a,b)
→ a <= N <= b
Jini N K
randrange
(<start>,<stop>,<step>)
Jini N K
What could be the minimum possible and maximum possible
numbers by following code?
Jini N K
What will the following code produce?
Jini N K
Write a code fragment to generate a random floating number
between 45.0 and 95.0. Print these numbers along with their
average.
Jini N K
Write a code fragment to generate two random integers between 450
and 950. Print these numbers along with their average.
Jini N K
Write a code fragment to generate three random integers in the range
10,70 with a step of 13. Create a set with these numbers
Jini N K
statistics MODULE OF PYTHON
import statistics
Jini N K
statistics. mean(seq) Returns the average value of the set/sequence
statistics. mode(seq) Returns the most often repeated value of the set/sequence
Jini N K
Given a list containing these values [22, 13, 28, 13, 22, 25, 7, 13, 25].
Write code to calculate mean, median and mode of the list.
Jini N K
DEBUGGING
Debugging refers to the process
of locating the place of error, and
correcting the code accordingly
PROGRAM BUG
Error in a program.
Jini N K
An error or bug is anything in
Jini N K
EXCEPTIONS
Jini N K
ERRORS EXCEPTIONS
Jini N K
BUILT-IN EXCEPTIONS
Hits an end-of-file (EOF)
EOFError When a mapping key is not
without reading any data KeyError found in the set of existing keys
Jini N K
JINI N K
Jini N K