0% found this document useful (0 votes)
12 views6 pages

Pb-U7.1 Data Handling

The document provides an overview of data handling in Python, covering data types such as numbers, strings, lists, tuples, and dictionaries, along with their mutable and immutable characteristics. It explains the properties of each data type, including how they store values and their memory behavior. Additionally, it discusses the internal attributes of Python objects, including type, value, and id.

Uploaded by

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

Pb-U7.1 Data Handling

The document provides an overview of data handling in Python, covering data types such as numbers, strings, lists, tuples, and dictionaries, along with their mutable and immutable characteristics. It explains the properties of each data type, including how they store values and their memory behavior. Additionally, it discusses the internal attributes of Python objects, including type, value, and id.

Uploaded by

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

Unit 3: Advance Python Vision 2 Win

DATA HANDLING
 Data types
o Data type in Python specifies the type of data we are going to store in any variable, the amount
of memory it will take and type of operation we can perform on a variable.
o Data can be of many types e.g. character, integer, real, string etc.

o Python supports following data types:


 Numbers ( int, float, complex)
 String
 List
 Tuple
 Dictionary.

 MUTABLE AND IMMUTABLE TYPES


o Python data object (data types) can be broadly categorized into two types – mutable and
immutable types.
o In simple words changeable/modifiable and non-modifiable types.

o Immutable types:
 A Immutable data type cannot change its state or contents or value in place.
 In python following types are immutable: integers, float, Boolean, strings, tuples.

Example: Code:
Values 10 20 30 40 50 a=10 b=a c=30
print(id(a)) print(id(b))
Address 100 101 110 1110 1111 print(id(c)) print(id(10)
 0 0 0
Output:
1000 1000 1100 1000
a b c

Artificial Intelligence 1 Class 10


Unit 3: Advance Python Vision 2 Win

Note : In python each value in memory is assigned a memory address. So each time a new
variable is pointing to that value they will be assigned the same address and no new memory
allocation.
o From the previous code it is clear that variable names are stored references to a value-object.
Each time we change the value the variable’s reference memory address changes. So it will
not store new value in same memory location that’s why Integer, float, Booleans, strings and
tuples are immutable.
o Variables (of certain type) are NOT LIKE storage containers i.e. with fixed memory address
where value changes every time. Hence they are immutable

o Mutable types:
 A mutable data type can change its state or contents or value in place.
 Mutable means in same memory address, new value can be stored as and when it is
required. Python provides following mutable types: Lists, Dictionaries, Sets
Example:
List 10 20 30 40 50 Output:
1010
Address  1010 [10,20,30,100,50]
Code: 1010
List =[10,20,30,40,50]
print(id(List)) See, even if we change the value, its
List[3]=100 reference memory address has remained
Print(List) same..
print(id(List))

 Number:
o From the name it is very clear the Number data types are used to store numeric values.
o Numbers in Python can be of following types:
 Integers
 Integers(signed)
 Booleans
 Floating point numbers
 Complex Numbers
o Integers
 Integers allow storing whole numbers only and there are no fraction parts.
 Integers or int are positive or negative numbers with no decimal point. Integers in Python 3
are of unlimited size.
 Example: 100, 250, -12, +50

 There are two integers in Python:


 Integers(signed) : it is normal integer representation of whole numbers. Integers in
python can be on any length, it is only limited by memory available. In Python 3.x int
data type can be used to store big or small integer value whether it is +ve or –ve.

Artificial Intelligence 2 Class 10


Unit 3: Advance Python Vision 2 Win

 Booleans: it allows storing only two values True and False. The internal value of
Boolean value True and False is 1 and 0 resp. We can get Boolean value from 0 and 1
using bool() function.

o Floating point numbers


 It allows to store numbers with decimal points. For e.g. 2.14. The decimal point indicate that
it is not an integer but a float value. 100 is an integer but 100.5 is a float value. In Previous
chapter we have already discussed float values can be of type type:
 Fractional Form : 200.50, 0.78, -12.787
 Exponent Form: it is represented with mantissa and exponent.

 Floating point numbers are mainly used for storing values like distance, area, temperature etc.
which have a fractional part.
 Floating point numbers have two advantages over integers:
 they can represent values between the integers
 they can represent a much greater range of values

 But floating point numbers suffers from one disadvantage also:


 Floating point operations are usually slower than integer operations.

o Complex numbers
 Complex numbers are combination of a real and imaginary part. Complex numbers are in the
form of X+Yj, where X is a real part and Y is imaginary pa”rt.
 Example: 10 + 2j, 1+3.54j .

 Strings
 A string is a sequence of characters. In python we can create string using single (' ') or double
quotes (" "). Both are same in python. String is a collection of any valid characters in a
quotation marks ( ‘ ‘ or “ ”)
 Each character of String in Python is a Unicode character
 Strings are used to store information like name, address, descriptions. etc
 Example: “hello”, “welcome‟, “sales2018”.

 In Python string is a sequence of characters and each character can be individually access
using index. From beginning the first character in String is at index 0 and last will be at len-1.
From backward direction last character will be at index -1 and first character will be at –len.

Forward indexing

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
C o m p u t e r S c i e n c e

Artificial Intelligence 3 Class 10


Unit 3: Advance Python Vision 2 Win

-16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1


Backward indexing
 From above example
 Length of string = 16
 First character of the string is at index 0 or –length. i.e is C ( 0 or -16)
 Second character of the string is at index 1 or –(length-1). i.e is o ( 0 or -15)
 Second last character of the string is at index (length-2) or -2. i.e is c ( 14 or -2)
 Last character of the string is at index (length-1) or -1. i.e is c ( 15 or -1)

 Example :
str='Computer Science' Output
print('str-', str) # print string str- Computer Science
print('str[0]-', str[0]) # print first char str[0]- C
print('str[-1]-', str[-1]) # print last char str[-1]- e
print('str[9]-', str[9]) # print 10th char from forward str[9]-S
print('str[-11]-', str[-11]) # print 11th char from backward str[-11]-t
print('str[1:3]-', str[1:3]) # print string from position 1 to 3 str[1:3]- om
print('str[3:]-', str[3:]) # print string staring from 3rd char str[3:]- puter Science
print("str +'yes'-", str +'yes') # concatenated string str +'yes'- Computer Scienceyes

 List
o Lists are compound data types i.e. they allow to store multiple values under one name of
different data types.
o Each item has its own index value. Index of first item is 0 and the last item is n-1.Here n is
number of items in a list.
o A list in python represents a list of comma-separated values of any data type between square
brackets [] .
o List can be changed/modified, i.e. Mutable

Code: Output:
List=[] []
List=[1,2] [1,2]
List=[1,2,13.34,16.24] [1,2,13.34,16.24]

 Tuples
o Tulpes are compound data types i.e. they allow to store multiple values under one name of
different data types.
o Each item has its own index value. Index of first item is 0 and the last item is n-1.Here n is
number of items in a list.
o Tuples as those list which cannot be changed i.e. not modifiable (Immutable).

Artificial Intelligence 4 Class 10


Unit 3: Advance Python Vision 2 Win

o Tuples are defined inside parenthesis () and values separated by comma.

Code: Output:
Tuple=() ()
Tuple=(1,2) (1,2)
Tuple =(1,2,13.34,16.24) (1,2,13.34,16.24)

 Dictionary
o It is an unordered collection of items where each item consists of a key and a value.
o It is mutable (can modify its contents) but Key must be unique and immutable.
o Dictionaries are defined inside Curly Brackets { } and values separated by comma.
o Keys defined in Dictionary cannot be same i.e. no two keys can be same.

Code: Output:
Dic={} {}
Dic={1:2,2:3,3:4} {1:2,2:3,3:4}
Dic={1:"JAVA",2:"C++",3:"HTML"} {1:"JAVA",2:"C++",3:"HTML"}

 Variable Internals
o Python is an object oriented language. So everything in python is an object. An object is any
identifiable entity that have some characteristics/properties and behaviour.

o Every python object has three key attributes associated with it:
 type of object
 value of an object
 id of an object.

 Type of an object:
 type of an object determines the operations that can be performed on the object.
 Built – in function type() returns the type of an object.
Example: Output:
a=100 <class 'int'>
b=10.5 <class 'int'>
name="Jaques" <class 'float'>
type(a) <class 'str'>
type(100)
type(b)
type(name)

 Value of an object

Artificial Intelligence 5 Class 10


Unit 3: Advance Python Vision 2 Win

The data items stored in the object is a value of object. The value stored in an object is a
literals.
 We can using print() to get the value of an object.
Example: Output:
a=100 100
name="India" India
print(a)
print(name)

 Id of an object
 It is the memory address of any object. Although id is dependent upon the system where it
is installed but in most cases it returns the memory location of the object
 Built in function id() returns the id of an object.
Example: Output:
a=100 1911018608
id(a) 1911018608
id(100) 1911018608
print(id(a))

Artificial Intelligence 6 Class 10

You might also like