Datatypes and Its Types
Datatypes and Its Types
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.
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