Contents

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

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

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

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

### 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 = 10
>>> a
[10,2,3,4,5] ``````

To change 3rd item, you may write

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

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

### 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`````` Following figure summarizes the core data types of Python – Data Handling In Python Class 11

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

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

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