0% found this document useful (0 votes)
6 views30 pages

Data Handling Part 1 Ip

This document covers fundamental concepts of programming languages, specifically focusing on data types in Python, including numbers, strings, lists, tuples, and dictionaries. It explains the differences between mutable and immutable types, detailing how integers, floats, booleans, strings, and tuples are immutable, while lists are mutable. Additionally, it provides examples of how to work with these data types and their properties within Python.

Uploaded by

ashugamerz1988
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views30 pages

Data Handling Part 1 Ip

This document covers fundamental concepts of programming languages, specifically focusing on data types in Python, including numbers, strings, lists, tuples, and dictionaries. It explains the differences between mutable and immutable types, detailing how integers, floats, booleans, strings, and tuples are immutable, while lists are mutable. Additionally, it provides examples of how to work with these data types and their properties within Python.

Uploaded by

ashugamerz1988
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Data Handling

Std XI – Computer Science / Informatics


Practices
Introduction
In this lesson we will see some fundamentals of
programming languages like
1. Data types
2. Variables (Mutable- change values and Immutable – do
not change value)
3. Operators
4. Expressions
Data types
Python offers following built-in core data types:
1)Numbers
2) String
3) List
4) Tuple
5) Dictionary
Numbers
Number data types are used to store numeric values in
Python.
They have the following core data type:
1) Integers
Integers(Signed)
Boolean

2) Floating point numbers

3) Complex numbers
Integers
Eg - +12, -15, 300
There are two types of integers in Python:

1) Integers(signed) –

 Integers in Python can be of any length.

 It stores both big integer and small integers.


2) Boolean - It represents truth values True or False, ie. 1
and 0

bool(0) will return value False


bool(1) will return value True

Example - >>> bool(0) Example –


>>>str(False)
False ‘False’
>>>bool(1)
>>>str(True) True
‘True’

The str() function will convert Boolean values to string.


Floating Point Numbers
1) Fractional Form, Example – > 12.6
2) Exponent form, Example -> 3.5E02

They have 2 advantages over integer :


1) They represent value between the integers.
2) They can represent as much greater range of values.

Disadvantages:
1) Floating point operations are slower than integer
operations.
Note: In python ,the floating point numbers have precision
Complex numbers:
Complex numbers are of the form a+bj, where a and b are
floats and j represents (which is an imaginary number)

a is real part and b is the imaginary part.( both are


considered as floating point numbers)
Example  a= 0+3.1j (0 is real part and 3.1 is imaginary part)
b= 1.5+2j (1.5 is real part and 2 is imaginary part)

>>>c=0+4.5j
>>>d=1.1+3.4j
>>>c
4.5j
>>>d
(1.1+3.4j)
>>>print(c)
4.5j
>>>print(d)
(1.1+3.4j)
To retrieve the two components for a complex number z
we can do as follows:
>>>z=(1+2.56j)+(-4-3.56j)
>>>z
(-3 -1j)
>>>z.real
-3.0
>>>z.imag
-1.0
The Range of Python numbers:
Strings
In python each character stored in a string is a Unicode
character.

Unicode is a system designed to represent every


character from every language.

A string can hold letters , numbers, symbols.


Example – ”34%^$”
“???”
“@#$FR^&56”
String as a sequence of
characters
A python string is a sequence of characters and each
character can be individually accessed using its index(also
called subscript).
Strings in Python are stored as individual characters in
contiguous location, with two way index for each location.
Subject=“Computers”
0 1 2 3 4 5 6 7 8
Subject C O M P U T E R S

-9 -8 -7 -6 -5 -4 -3 -2 -1

Thus, Subject[0]=‘C’ Subject[2]=‘M’


Subject[6]=‘E’
Subject[-9]=‘C’ Subject[-7]=‘M’
Subject[-3]=‘E’
As len() function is used to find the length of the string we can say
that:
first character of the string is at index 0 or –length
second character of the string is at index 1 or –(length-1)
.
.
 second last character of the string is at index -2 or (length-2)
Last character of the string is at index -1 or (length-1)
So in the above string Subject, if the length is ln ,
then the valid indices are 0,1,2,….ln-1
So, if you type
>>>Subject(ln) # It will show error

Also you cannot change the individual letters of a


string in place because strings are immutable and
hence item assignment is not supported.
>>>name=‘hello’
>>>name[0]=‘p’ # It will show error
However you can assign to a string another string
or an expression that returns a string using
assignment.

>>>name=‘hello’
>>>name=‘new’
List and Tuples
List and tuples are basically of the same types
with one difference.

List can be changed/modified(ie. Mutable)

Tuples cannot be changed/modified(ie.


Immutable)
Lists
A list in python represents a list of comma-separated
values of any datatype between square brackets.
Example – [1,2,3,4,5] [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
[‘Neha’, 102, 79.5]
We can assign a list to a variable:
>>> a=[1,2,3,4,5]
>>>a
[1,2,3,4,5]
>>> print(a)
[1,2,3,4,5]
To change a value in a list :

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

>>>a[2]=30
>>>a
[10,2,30,4,5]
Tuples
Tuples cannot be changed/modified(ie. Immutable)

A Tuple in python represents a list of comma-


separated values of any datatype between
parentheses(round brackets).

p=(1,2,3,4,5)
q=(2,4,6,8)
r=(‘a’, ‘e’, ‘i’, ‘o’, ‘u’)
h=(7,8,9,’A’,’B’,’C’)
Note:
Python data types strings, lists, tuples, dictionary ,etc are
all iterables.
An iterable is any Pyhton object that can return its
members, one at a time. Since you can access elements
of strings, lists, tuples and dictionary one at a time, these
are called iterables.
Dictionary
Dictionary data type is an unordered set of
comma-separated key : value pairs, within {}
No two keys can be the same.

Example –
{‘a’ : 1, ‘e’ : 2, ‘i’ : 3, ’o’: 4, ’u’ : 5}
>>>vowels={‘a’ : 1, ‘e’ : 2, ‘i’ : 3, ’o’: 4, ’u’ : 5}
Here ‘a’ ‘e’ ‘i’ ‘o’ ‘u’ are keys of dictionary vowels
1,2,3,4,5 are values of these keys respectively

>>>vowels[‘a’]
1
>>>vowels[‘u’]
5
Mutable and Immutable types
In Python data objects can be classified into 2
categories –

Mutable(changeable or modifiable) and

Immutable(non-modifiable).
Immutable types
Immutable types are those that can never change their
value in place.
In python, the following types are immutable:
Integers
Floating point numbers
Booleans
Strings
Tuples
Example
p=5
q=p
r=5
. #will give 5,5,5
.
.
p =10
r= 7
q= r

Since p,q,r are integer values you may think that they can change
values ,but its not the case .
•Python variable–names are just the references to value-
objects . ie. Data values.
•The variable names do not store values themselves ie.
They are not storage containers.
•So although it appears that the value of variable p,q,r is
changing ; values are not changing in place.
•The fact is that the variable names are instead made to
refer to new immutable integer object.
•Changing in place means modifying the same value in
same memory location.
When p=5 q=p r=5
Internally python keeps a
count of how many variables
are referring to a value
(3
)
5 6 7 8 9 10
20200 20216 20248
20232 20264 20280

p q r

>>>id(p)
>>>p =5
20200
>>>q=p
>>>id(q)
>>>r=5
20200
>>>id(5)
>>>id(r)
20200 20200
When p = 10 r=7 q=r
Currently two
identifiers/variables are
referring to this value
(2 (1
) )
5 6 7 8 9 10
20200 20216 20248
20232 20264 20280

p q r >>>id(r)
>>>id(p) 20232
>>>p =10
20280 >>>id(5)
>>>r=7
>>>id(7) 20200
>>>q=r
20232 >>>t=5
>>>id(10)
>>>id(q) >>>id(t)
20280 20232 20200

You might also like