1
Data types in Python
2
Data types in Python
In Python, every value belongs to a specific data type.
Data type identifies the type of data values a variable can hold and the
operations that can be performed on that data.
Note:
In order to determine the data type of the variable, we can use the built-
in function type()
3
Data types in Python
4
Data types in Python: Numeric
Number data type stores numerical values only. It is further classified into
three different types:
- int
- float and
- complex
Type/class Description example
int integer numbers –12, –3, 0, 125, 2
float real or floating point numbers –2.04, 4.0, 14.23
complex complex numbers 3 + 4j, 2 – 2j
5
Data types in Python: Boolean
Boolean data type (bool) is a subtype of integer, consisting of 2 constants:
True
False
Boolean True value is non-zero, non-null and non-empty and
boolean False is the value zero.
6
Data types in Python: Sequence
Variables of simple data types like integers, float, boolean, etc., hold single
values. But such variables are not useful to hold a long list of information, for
example,
- names of the months in a year,
- names of students in a class,
- names and numbers in a phone book or
- the list of artefacts in a museum.
For this, Python provides data types like tuples, lists, dictionaries and sets.
7
Data types in Python: Sequence
A Python sequence is an ordered collection of items, where each item is
indexed by an integer.
The three types of sequence data types available in Python are
1) Strings
2) Lists
3) tuples
8
Data types in Python: Sequence
Strings Lists Tuple
Group of characters sequence of items separated Like list, tuple is a
by commas sequence of items,
separated by commas
Enclosed in either single items are enclosed in square items are enclosed in
quotes (‘ ’) or double quotes brackets [ ] parenthesis ( )
Immutable data type Mutable data type Immutable data type
e.g.) “de4dr”, ‘fre67’ [5, 3.4, "New Delhi", "20C", 45] (10, 20, "Apple", 3.4, 'a')
9
Data types in Python: Set
Set is an unordered collection of items separated by commas, and the
items are enclosed in curly brackets { }
e.g.) {10,20,3.14,"New Delhi"}
Note:
A set is similar to list, except that
it cannot have duplicate entries
It is immutable, and
It is unordered
10
Data types in Python: None
None is a special data type with a single value. It is used to signify the
absence of value in a situation.
None supports no special operations,
and it is neither False nor 0 (zero).
11
Data types in Python: Mapping
Mapping is an unordered data type in Python. Currently, there is only one
standard mapping data type in Python called dictionary.
Dictionary in Python holds data items in key-value pairs. Items in a dictionary
are enclosed in curly brackets { }. Every key is separated from its value using a
colon (:) sign.
e.g.) {'Fruit': 'Apple', 'Climate': 'Cold', 'Price(kg)': 120}
12
Mutable and Immutable data types
Mutable: Variables whose values can be changed after they are created
and assigned are called mutable
E.g.) Lists, dictionary, etc.
Immutable: Variables whose values cannot be changed after they are
created and assigned are called immutable.
E.g.) Numbers, strings, tuples etc.
What happens when we try to update a variable?
Is value changed or the location changed
13
When to use which Data type?
List: when we require collection of data that requires frequent modifications
e.g.) entry register in a hotel
Tuple: when data doesn’t require any change
E.g.) name of week’s days
Sets: when we need uniqueness of data, and avoid duplicity
E.g.) name of month’s name,
Dictionary: when we need lookup of data using key, or we need a logical
association between the key : value pair
E.g.) Mobile phone book, dictionary
14
Summary
15
Assignment
Write down the contents of the slide in your notebook
What happens when we try to update a variable? Is value changed or the
location changed
Which data type will be used to represent the following data values and
why?
a) Number of months in a year b) Resident of Delhi or not
c) Mobile number d) Pocket money
e) Volume of a sphere f) Perimeter of a square
g) Name of the student h) Address of the student