**Contents**show

## chapter-3

## Data Handling In Python Class 11

## notes

### 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 type | Range |

Integers | an unlimited range, subject to available(virtual memory) only |

Boolean’s | two value True (1), False(0) |

Floating point numbers | an unlimited range, subject to available (virtual) memory on underlying machine architecture. |

Complex numbers | Same as floating point numbers because the real and imaginary parts are represented as floats. |

### 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
```

### 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.

- 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.

#### (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 (+, -, *, /, %)

Symbol | Name | Example | Result | Comment |

+ | Addition | 6+5 5+6 | 11 11 | adds values of its two operands. |

– | Subtraction | 6-5 5-6 | 1 -1 | subtracts the value of right operand form left operand. |

* | multiplication | 5*6 6*5 | 30 30 | multiplies the values of its two operands. |

/ | division | 60/5 | 12 | divides 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 Remainder | 60%5 6%5 | 0 1 | divides the two operands and gives the remainder resulting. |

// | Floor division | 7.2//2 | 3.0 | divides and truncates the fractional part from the result. |

** | Exponentiation (Power) | 2.5**3 | 15.625 | return base raised to power exponent. |

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

p | q | p<q | p<=q | p==q | p>q | p>=q | p!=q |

3 | 3.0 | 1. False | 1. True | 1. True | 1. False | 1. True | 1. False |

6 | 4 | 2. True | 2. False | 2. False | 2. True | 2. True | 2. True |

‘A’ | ‘A’ | 3. False | 3. True | 3. True | 3. False | 3. True | 3. False |

‘a’ | ‘A’ | 4. False | 4. False | 4. False | 4. True | 4. True | 4. True |

#### 3. Logical (and,or,not)

Operation | Result | Notes |

x or y | if x is false, then return y as result, else x | it (or) only evaluates the second argument if the first one is false |

x and y | if x is false, then x as result, else y | it (and) only evaluates the second argument if the first one is true |

not x | if x is false, then return True as result, else False | not has a lower quantity priority than non-Boolean operators. |

#### 4. Identify Operator (is,is not)

Operator | Usage | Description |

is | a is b | returns True if both its operands are pointing to same object (i.e., both referring to same memory location), returns False otherwise. |

is not | a is not b | returns True if both its operands are pointing to different objects (i.e., both referring to different memory location), returns False otherwise. |

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

Operation | Description | Comment |

x += y | x = x+y | Value of y added to the value of x and result assigned to x |

x -= y | x = x-y | Value of y subtracted from the value of x and result assigned to x |

x *= y | x = x*y | Value of y multiplied to value of of x then result assigned to x |

x /= y | x = x/y | Value of y divides value of x and then result assigned to x |

x //= y | x = x//y | Value of y does floor division to value of x and then result assigned to x |

x **= y | x = x**y | x^y compute and then result assigned to x |

x %= y | x = x%y | Value of y divides value of x and then remainder assigned to x |

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

Operator | Operation | use | Description |

& | bitwise and | op1 & op2 | The AND operator compares two bits and generates a result of 1 if both bits are 1; otherwise, it returns 0. |

| | bitwise or | op1 | op2 | The OR operator compares two bits and generates a result of 1 if the bits are complementary; otherwise, it return 0. |

^ | bitwise xor | op1 ^ op2 | The 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 | ~op1 | THe COMPLEMENT operator is used to invert all of the bits of the operand |

### 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

Operator | Description |

( ) | Parenthesis (grouping) |

** | Exponentiation |

~x | Bitwise nor |

+x, -x | Positive, negative (unary +, -) |

*, /, //, % | Multiplication, division, floor division, remainder |

+, – | Addition, subtraction |

& | Bitwise and |

^ | Bitwise XOR |

| | Bitwise OR |

<, <=, >, >=, <>, !=, == is, is not | Comparisons ( Relational operator), identity operator |

not x | Boolean NOT |

and | Boolean AND |

or | Boolean OR |

### Read More :-

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