Python Data Types
Python Data Types
Data types in Python are a way to classify data items.
They represent the kind of value, which determines what operations can
be performed on that data.
Since everything is an object in Python programming, Python data types
are classes and variables are instances (objects) of these classes.
The following are standard or built-in data types in Python:
1. Numeric Data Types
Python numbers represent data that has a numeric value.
A numeric value can be an integer, a floating number or even a complex
number.
These values are defined as int, float and complex classes.
Example :-
Access Real and Imaginary Part Operations on Complex no.
2. Sequence Data Types
A sequence is an ordered collection of items, which can be of similar or
different data types.
Sequences allow storing of multiple values in an organized and efficient
fashion.
There are several sequence data types of Python:
String Data Type
List Data Type
Tuple Data Type
1. String Data Type
In Python, there is no character data type, a character is a string of
length one.
It is represented by str class.
Strings in Python can be created using single quotes, double quotes or
even triple quotes.
We can access individual characters of a String using index.
Output
2. List Data Type
Lists are similar to arrays found in other languages.
They are an ordered and mutable collection of items.
It is very flexible as 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 sequence inside the square
brackets[].
Output
3. Tuple Data Type
Tuple is 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.
Tuples are created by placing a sequence of values separated by a
‘comma’ with or without the use of parentheses for grouping data
sequence.
Tuples can contain any number of elements and of any datatype (like
strings, integers, lists, etc.).
3. Boolean Data Type in Python
Python Boolean Data type is 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).
However non-Boolean objects can be evaluated in a Boolean context as
well and determined to be true or false.
It is denoted by class bool.
4. Set Data Type in Python
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.
5. Dictionary Data Type
A dictionary in Python is a collection of data values, used to store data
values like a map, unlike other Python Data Types, a Dictionary holds a
key: value pair.
Key-value is provided in dictionary to make it more optimized.
Each key-value pair in a Dictionary is separated by a colon : , whereas
each key is separated by a ‘comma’.
NoneType in Python
In Python, NoneType is the type of the special value None.
It represents the absence of a value or a null value.
It is often used to indicate that a variable has no data or that a
function does not return anything.
Key Points
• None is a built-in constant in Python.
• The type of None is NoneType.
• It is not equal to 0, False, or an empty string ''.
Example
Use Cases Of NoneType
To initialize a variable with “no value” initially.
To check if a variable or function result is empty.
To represent “no result” in functions.