0% found this document useful (0 votes)
18 views

Datatypes and Its Types

Uploaded by

dibyashakti10
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Datatypes and Its Types

Uploaded by

dibyashakti10
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Module : M04

Boolean datatype:-
Python Data type with one of the two built-in values, True or
False.
Boolean objects that are equal to True are truthy (true), and
those equal to False are falsy (false).
but non-Boolean objects can be evaluated in a Boolean
context as well and determined to be true or false.
It is denoted by the class bool.

Note:- True and False with capital ‘T’ and ‘F’ are valid Booleans otherwise python will throw
an error.
Sequence type:-
in Python , sequence is the ordered collection of similar or
different Python data types.
Sequences allow storing of multiple values in an organized and
efficient fashion.

There are several sequence data types of Python:


Python String
Python List
Python Tuple
Sequence type- string:-
A string is a collection of one or more characters put in a
single quote , double-quote or triple quote.
In Python, there is no character data type Python, a
character is a string of length one.
It is represented by str class.
Creating string :-
Strings in Python can be created using single quotes, double
quotes, or even triple quotes.
Sequence type-list :-
Lists are just like arrays, declared in other languages which is
an ordered collection of data. It is very flexible as the items in
a list do not need to be of the same type.
Creating a List in Python
Lists in Python can be created by just placing the sequence
inside the square brackets[].
sequence type –tuple :-
Just like a list, a tuple is also an ordered collection of Python
objects.
The only difference between a tuple and a list is that tuples are
immutable.
Tuples cannot be modified after it is created.
It is represented by tuple class
Creating a Tuple in Python
In Python Data Types, tuples are created by placing a sequence
of values separated by a ‘comma’ with or without the use of
parentheses for grouping the data sequence.
Note: Tuples can also be created with a single element, but it is a bit tricky. Having one element
in the parentheses is not sufficient, there must be a trailing ‘comma’ to make it a tuple.

Note:- Creation of python tuples without the use of parentheses known as Tuple packing.
Sets:-
In Python Data Types, Set is an unordered collection of data
types that is iterable, mutable, and has no duplicate elements.
The order of elements in a set is undefined though it may
consist of various elements.
The type of elements in a set need not be the same, various
mixed-up data type values can also be passed to the set.
Create a Set in Python:-
Sets can be created by using the built-in set() function with an
iterable object.
a sequence by placing the sequence inside curly braces,
separated by a ‘comma’.
Dictionary:-
dictionary in Python is an unordered collection of data
values, used to store data values like a map, unlike other
Python Data Types that hold only a single value as an
element.
a Dictionary holds a key: value pair.
Key-value is provided in the dictionary to make it more
optimized.
Each key-value pair in a Dictionary is separated by a colon :
each key is separated by a ‘comma’.
Creating dictionary:-
In python , a dictionary can be created by placing a sequence
of elements within curly {} braces,separated by ‘comma’
Values in a dictionary can be of any datatype and can be
duplicated, whereas keys can’t be repeated and must be
immutable.
The dictionary can also be created by the built-in
function dict().
An empty dictionary can be created by just placing it to curly
braces {}.
Note – Dictionary keys are case sensitive, the same name but
different cases of Key will be treated distinctly.
Mutable and immutable data types :
Sometimes we may require to change or update the values of
certain variables used in a program.
However for certain datatypes, python does not allow us to
change the values once a variable that type has been created
and assigned values.
Variables whose values can be changed after they are created
and assigned are called mutable
variables whose values cannot be changed after they are
created and assigned are called immutable.
When an attempt made to update the value of an immutable
variable , the old variable is destroyed and a new variable is
created by the same name in the memory.
Deciding usage of python data types:-
it is preferred to use lists when we need a simple iterable
collections of data that may go for frequent modifications.
For example:- If we store the names of students of a class in a
list. Then it is easy to update the list when some new students
join or some leave course.
Tuples are used when we do not need any change.
For example:- Names of month in a year.
When we need uniqueness of elements and to avoid duplicacy
it is preferable to use sets.
For example – list of artefacts in a musem
If our data is being constantly modified or we need a fast
lookup bases on a custom key or we need a logical associations
between the key- value pair. It is advised to dictionaries.
For example:- a mobile phone book is a good application of
dictionary

You might also like