Data Handling in Python Class 11

chapter-3

Data Handling In Python Class 11

notes

data handling in python class 11
data handling in python class 11

3.1 Introduction :

Data Handling In Python Class 11

In any language, there are some fundamentals you need to know before you can write even the most elementary programs. This chapter introduces some such fundamentals : data types, variables, operators and expressions in Python.

Python provides a predefined set of data types for handling the data it uses. Data can be stored in any of these data types. This chapter is going to discuss various types of data that you can store in python. Of course, a program also needs a means to identify stored data.

So, this chapter shall talk about mutable and immutable variables in Python.

Data Handling In Python Class 11

3.2 Data Types:-

It is the classification of data based on its nature and associated operator.

3.2.1 number:-

as it is clear by name number data types are used to store numeric values in python .  

The number in python have following core data types:-

3.2.1.A Integer:-

Integer are whole number such as 5,66,5,44,0 etc.they have no fractional parts.  

  • integer(signed) :-

It is the normal integer1 representation of a whole number. the two objects representing the values false and true are only boolean objects in python.  

  • Boolean :-

These represent the truth values true and false.

The Boolean type is subtype of plain integer, and Boolean values false and true behave like the values o and 1, respectively.

To get the the Boolean equivalent of o or 1, you can type bool(0) or bool(1), Python will return false or true respectively.

3.2.1.B Floating Point Numbers :-

A number having fractional part is floating-point number.for example, 3.1233 is floating point number. the fractional number can be written in two forms:-

(i) Fractional Form(normal decimal notation) e.g 3500.75,0.00003 etc
(ii) Exponent Notation e.g, 3.50075E03  

3.2.1.C complex number :-

A complex number is in the form A+Bi where i is the imaginary number,equal to the square root of -1. complex number in python:-python represents complex number as a pair of floating point numbers.  

a=0+3.1j

b=1.5+2j

  A and b are two complex numbers. The real and imaginary parts of a complex number z can be retrieved through the read-only attributes   z.real and z.imag.

  • z.real gives the real part.
  • z.imag gives the imaginary part as a float, not as a complex value.
For example,
>>> z = (1 + 2.56j) + (-4 -3.56j)
>>> a
(-3 -1j)
>>> z.real 
-3.0
>>> z.imag
-1.0

The range of numbers represented through Python’s numeric data types is given below.

Data typeRange
Integersan unlimited range, subject to available(virtual memory) only
Boolean’stwo value True (1), False(0)
Floating point numbersan unlimited range, subject to available (virtual) memory on underlying machine architecture.
Complex numbersSame as floating point numbers because the real and imaginary parts are represented as floats.
The Range of Python Numbers – Data Handling In Python Class 11

3.2.2 String:-

A string can hold any type of known character, special character,letter,of any known scripted language. following are all legal string in python:  

“abcd”, “1234”, ‘@#$^”, “??????”

  Since length of string variable can be determined using function    len(<string>) -first character of the string is at index 0 or -length -last character of the string is at index (length-1) or -1  

String are immutable and hence item assignment is not supported

Strings In Python Tutorial

3.2.3 List and Tuples

The lists and tuples are python’s compound data types. list can be changed/modified(mutable) but tuples cannot be changed or modified(immutable).  

A Lists :-

A list in python represents a list of comma-separated values of any datatype between square brackets e.g. ,

following are some lists:

 

[1,2,3,4,5] 
['s','f','o','l'] 
['neha', 102', 55.6'] 

Like any other value you can assign a list to a variable e.g.,

>>> a = [1,2,3,4,5]     # Statement 1
>>> a
[1,2,3,4,5] 
>>> print(a)
[1,2,3,4,5] 

To change first value in a list namely a (given above), you may write

>>> a[0] = 10
>>> a
[10,2,3,4,5] 

To change 3rd item, you may write

>>> a[2] = 30
>>> a
[10,2,30,4,5] 

Python Lists Tutorial

B Tuples:-

Tuples are represented as list of comma-separated values of any data type within parenthesis, following are some tuples:

 

p = (1, 2, 3, 4, 5) 
q = ('a', 'e', 'l', 'k') 
r = ('a', 2, 3, 4, 5, 'c') 

Python Tuples Tutorial

3.2.4 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( there are unique keys within a dictionary).

For instance, following are some dictionaries:              

{'a':1, 'e':2, 'i':3, 'o':4, 'u':5}
>>> vowels = {'a':1, 'e':2, 'i':3, 'o':4, 'u':5}
>>> vowels['a']
1
>>> vowels['u']
5
data handling in python class 11
Following figure summarizes the core data types of Python – Data Handling In Python Class 11

Python Dictionaries Tutorial

3.3 Mutable And Immutable Types

The python data objects can be broadly categorized into two- mutable and immutable types, in simple words changeable or modified-able and non-modifiable.  

Mutability means that in the same memory address,new value can be stored as and when you want the types that do no support this property are immutable.  

  1. Immutable types :- The immutable those that can never change their value in place. In Python, the following types are immutable : integers, floating point numbers, Booleans, strings, tuples.
sample code
p = 5
q = p
r = 5
      .
      .
      .                     # will give 5, 5, 5
p = 10
r = 7
q =3

2. Mutable types :- The mutable types are those whose values can be changed in place. Only three types are mutable in Python. These are :

lists, dictionaries and sets.

To change a member of a list, you may write :

chk = [2, 4, 6]
chk[1] = 40

3.3.1 Variable internals:-

Python is an object oriented language.python calls every entity that stores any values or any types of data as an object.  

Python object’s key Attributes

1. a type

2. a value

3. an id

Python Tutorial 2021

(i) The type of an object

The type of an object determines the operations that can be performed on the object. Built-in function type( ) returns the type of an object.

Consider this :

>>> a=4 
>>> type(4) 
<class 'int'> 
>>> type(a) 
<class 'int'>  

(ii) The value of an object

It is the data-item contained in the object. For a literal, the value is the literal itself and for a variable the value is the data-item it (the variable) is currently referencing. Using print statement you can display value of an object.

For example,

>>> a=4 
>>> print(4) 
4 
>>> print(a) 
4  

(iii)the id of an object

The id of an object generally the memory location of the object. Although id is implementation dependent but in most implementations it returns the memory location of the object Built-in function id( ) returns the id of an object.

e.g.,

>>> id(4) 
30899132 
>>> a=4 
>>> id(a) 
30899132  

3.4 Operators

data handling in python class 11

Operator that act upon two operands are referred to as binary operator.

1. Arithmetic    (+, -, *, /, %)  

SymbolNameExampleResultComment
+Addition6+5
5+6
11
11
adds values of its two operands.
Subtraction6-5
5-6
1
-1
subtracts the value of right operand form left operand.
*multiplication5*6
6*5
30
30
multiplies the values of its two operands.
/division60/512divides the value of left operand with the value of right operand and returns the result as a float value.
%Modulus ( pronounced mo-du-lo) or Remainder60%5
6%5
0
1
divides the two operands and gives the remainder resulting.
//Floor division7.2//23.0divides and truncates the fractional part from the result.
**Exponentiation (Power)2.5**315.625return base raised to power exponent.
Binary Arithmetic Operator – Data Handling In Python Class 11

2. Relational  (<,>,<=,>=)    

pqp<qp<=qp==qp>qp>=qp!=q
33.01. False1. True1. True1. False1. True1. False
642. True2. False2. False2. True2. True2. True
‘A’‘A’3. False3. True3. True3. False3. True3. False
‘a’‘A’4. False4. False4. False4. True4. True4. True
Relational Operator in Python – Data Handling In Python Class 11

     

3. Logical    (and,or,not)    

OperationResultNotes
x or yif x is false, then return y as result, else xit (or) only evaluates the second argument if the first one is false
x and yif x is false, then x as result, else yit (and) only evaluates the second argument if the first one is true
not xif x is false, then return True as result, else Falsenot has a lower quantity priority than non-Boolean operators.
Logical (and, or, not) – Data Handling In Python Class 11

4. Identify Operator (is,is not)    

OperatorUsageDescription
isa is breturns True if both its operands are pointing to same object (i.e., both referring to same memory location), returns False otherwise.
is nota is not breturns True if both its operands are pointing to different objects (i.e., both referring to different memory location), returns False otherwise.
Identity Operator – Data Handling In Python Class 11

5. Short hand assignment  (+=, -=, *=, /=, %=)  

OperationDescriptionComment
x += yx = x+yValue of y added to the value of x and result assigned to x
x -= yx = x-yValue of y subtracted from the value of x and result assigned to x
x *= yx = x*yValue of y multiplied to value of of x then result assigned to x
x /= yx = x/yValue of y divides value of x and then result assigned to x
x //= yx = x//yValue of y does floor division to value of x and then result assigned to x
x **= yx = x**yx^y compute and then result assigned to x
x %= yx = x%yValue of y divides value of x and then remainder assigned to x
Augmented Assignment Operators – Data Handling In Python Class 11

6. bit-wise    ($,^,|)  

OperatorOperationuseDescription
&bitwise andop1 & op2The AND operator compares two bits and generates a result of 1 if both bits are 1; otherwise, it returns 0.
|bitwise orop1 | op2The OR operator compares two bits and generates a result of 1 if the bits are complementary; otherwise, it return 0.
^bitwise xorop1 ^ op2The EXCLUSIVE-OR (XOR) operator compares two bits and return 1 if either of the bits are 1 and it gives 0 if both bits are 0 or 1.
~bitwise complement~op1THe COMPLEMENT operator is used to invert all of the bits of the operand
BITWISE OPERATOR – Data Handling In Python Class 11

unary operator:-

The operator that act one operand are referred to as unary operator.

(1)+   unary plus

if a = 5   then + a means 5.

if a = 0   then + a means 0.  

(2) ~   bit-wise compliment

(3)not    logical negation

(4)  –       unary minus

if a = 5   

then – a means -5.

if a = 0   

then – a means 0.(there is no quality known as -0)      

Operator Precedence

data handling in python class 11

OperatorDescription
( )Parenthesis (grouping)
**Exponentiation
~xBitwise nor
+x, -xPositive, negative (unary +, -)
*, /, //, %Multiplication, division, floor division, remainder
+, –Addition, subtraction
&Bitwise and
^Bitwise XOR
|Bitwise OR
<, <=, >, >=, <>, !=, ==
is, is not
Comparisons ( Relational operator), identity operator
not xBoolean NOT
andBoolean AND
orBoolean OR
Operator Precedence – Data Handling In Python Class 11

Read More :-

Data Handling In Python Class 11 important questions

cbse class 12 computer science question paper with solutions

Python Tutorial 2021

In this content your Data Handling In Python Class 11 topic is clear.