You are on page 1of 9

SNBP INTERNATIONAL SCHOOL & KIDZONE

SENIOR SECONDARY SCHOOL


MORWADI, PIMPRI, PUNE
CBSE AFFILIATION No.:- 1130522
ACADEMIC SESSION 2022-2023

Grade XI
Informatics Practices
Block 4: Data Handling

Tokens – Individual words and punctuation marks are called


tokens or lexical units or lexical elements. The smallest
individual unit in a program is known as a token or lexical
unit.

Python has following tokens:


1. Keywords 2. Identifiers 3. Literals 4. Operators 5.
Punctuators.

Literals are data items that have a fixed value.


Types of literals available in Python are
1.String literals: The text enclosed in quotes form string
literals :
Eg: ‘a’ , “ert”, “123”

For multiline string , we use \

2.Numeric literals: int, float


3.Boolean literals: True , False
4.Special Literal: Python has only one special literal. None[is
used to indicate the absence of value]
5. Literal Collections : tuple, list
Punctuators:

Punctuators are the symbols that are used in programming.


Eg: ‘ ‘’ # \ () [] , : . =
Expressions

An expression is defined as a combination of constants,


variables and operators. An expression always evaluates to a
value. A value or a standalone variable is also considered as
an expression but a standalone operator is not an expression.

(i) num – 20.4 (iii) 23/3 -5 * 7(14 -


2)

Precedence of Operators

When an expression contains more than one operator, their


precedence (order or hierarchy) determines which operator
should be applied first.

Note:
Parenthesis can be used to override the precedence of
operators. The expression within () is evaluated first.
For operators with equal precedence, the expression is
evaluated from left to right.

15.0 / 4.0 + (8 + 3.0)


Solution:
= 15.0 / 4.0 + (8.0 + 3.0) #Step 1
= 15.0 / 4.0 + 11.0 #Step 2
= 3.75 + 11.0 #Step 3
= 14.75 #Step 4

The value of some objects can change. Objects whose value


can change are said to be mutable; objects whose value is
unchangeable once they are created are called immutable.

• Some mutable data types in Python are:

list, dictionary, set, user-defined classes.

• Some immutable data types are:

int, float, decimal, bool, string, tuple, range.

Objects of built-in types like (int, float, bool, str, tuple,


unicode) are immutable. Objects of built-in types like (list, set,
dict) are mutable. Custom classes are generally mutable. To
simulate immutability in a class, one should override attribute
setting and deletion to raise exceptions.
Now comes the question, how do we find out if our variable is
a mutable or immutable object. For this we should
understand what ‘ID’ and ‘TYPE’ functions are for.

ID and TYPE
The built-in function id() returns the identity of an object as
an integer. This integer usually corresponds to the object’s
location in memory, although this is specific to the Python
implementation and the platform being used. The is operator
compares the identity of two objects.

The built-in function type() returns the type of an object. Lets


look at a simple example
''' Example 1 '''
>>> x = "Holberton"
>>> y = "Holberton"
>>> id(x)
140135852055856
>>> id(y)
140135852055856
>>> print(x is y) '''comparing the types'''
True''' Example 2 '''
>>> a = 50
>>> type(a)
<class: ‘int’>
>>> b = "Holberton"
>>> type(b)
<class: 'string'>

We have now seen how to compare two simple string


variables to find out the types and id’s .So using these two
functions, we can check to see how different types of objects
are associated with variables and how objects can be changed
.

Mutable and Immutable Objects


So as we discussed earlier, a mutable object can change its
state or contents and immutable objects cannot.

Mutable objects:

list, dict, set, byte array

Immutable objects:

int, float, complex, string, tuple, frozen set [note: immutable


version of set], bytes
A practical example to find out the mutability of object types
x = 10x = y

We are creating an object of type int. identifiers x and y


points to the same object.
id(x) == id(y)id(y) == id(10)

if we do a simple operation.
x=x+1

Now
id(x) != id(y)id(x) != id(10)

The object in which x was tagged is changed. object 10 was


never modified. Immutable objects doesn’t allow
modification after creation

In the case of mutable objects


m = list([1, 2, 3])n = m

We are creating an object of type list. identifiers m and m


tagged to the same list object, which is a collection of 3
immutable int objects.
id(m) == id(n)

Now poping an item from list object does change the object,
m.pop()

object id will not be changed


id(m) == id(n)

m and n will be pointing to the same list object after the


modification. The list object will now contain [1, 2].

So what have we seen so far from the above examples?

• Python handles mutable and immutable objects


differently.

• Immutable are quicker to access than mutable


objects.

• Mutable objects are great to use when you need to


change the size of the object, example list, dict etc..
Immutables are used when you need to ensure that
the object you made will always stay the same.

• Immutable objects are fundamentally expensive to


“change”, because doing so involves creating a copy.
Changing mutable objects is cheap.

Type Casting
Explicit type conversion is known as Type casting.
Syntax : <datatype> (expression)
int(a)
Debugging:
Debugging means to remove bugs(error) from a program.
Debugging refers to the process of locating the place of error,
cause of error, and correcting the code accordingly.
There are types of errors:
1. Compile time error – Errors that occurs during complie-
time, are compile time errors. When a program compiles, its
source code is checked for whether it follows the
programming language’s rules or not.
a. Syntax Error – when rules of a programming language
are misused.
Eg: if x =(x*y)

b. Semantics Error – When statements are not meaningful.


Semantics refers to the set of rules which give the
meaning of a statement.
Eg: x*y= k

2. Run time error - Error occurs during execution of


program are called run time errors. These are harder to
detect errors. Eg: an infinite loop, or wrong value of different
datatype other than required.

3. Logical error - Sometimes, even if we don’t encounter


any error during compile time and run time, your program
does not provide the correct results. These errors are due to
programmers mistaken analysis of problem.
Exception :
Exception in general refers to some contradictory
or unusual situation which can be encountered
unexpectedly while executing a program.
Exception is an irregular unexpected situated
occurring during execution.
EG: opening a file and displaying its content, but
the file does not exist.

You might also like