Different types of Datatypes in Python
Numbers
Strings
Lists
Tuples
Dictionary
Set
Frozenset
Numbers
Strings
Lists
Tuples
Dictionary
Set
Frozenset
1. Numbers
Python supports integers, floating-point numbers, and complex numbers. In Python, these numbers are defined as int, float, and complex classes in Python.
When we put a decimal in an Integer then it converts into a Floating-point number.
Example: 5 is an integer while 5.0 is a floating-point number.
Points to remember :
1. Integer length is not defined it can be of any length.
2. Floating-point is valid till 15th place 16th place is invalid.
Numbers we use daily are of type decimal (Base 10) number system. But other number systems are also present like Binary (Base 2), Hexadecimal (Base 16), and Octal (Base 8).
It's simple to represent these numbers in Python.
Example :
Complex numbers are represented in form of (x+yj) where x is a real part while y is an imaginary part.
Thatβs all you need to know about numbers in Python.
2. Strings
String is a sequence of Unicode characters.
Points to remember:
1. String can be represented using single quotes βNeptuneβ or double quotes βNeptuneβ.
2. Multiline Strings can be represented using tripe quotes.
Examples:
3. Strings are immutable in Python.
3. Lists
List is an ordered sequence of items. It is a widely used datatype in Python.
Points to remember:
1. We can declare a list using [ ] or list class.
2. List index starts from Zero.
Example :
3. Lists are mutable datatype means we can change the values in the existing list.
Example:
4. List is a heterogeneous datatype which means we can store different types of data in a single list.
Examples :
5. List can also be sliced using [ start index: end index - 1 ].
Examples:
4. Tuples
Tuple is the same as a list it is an ordered sequence of items. But tuple is an immutable data type which means it canβt be modified once created.
1. We can create a tuple using ( ) or tuple class.
2.Tuple is immutable in nature.
3. Tuple values can be accessed using [ ] just like a list.
5. Dictionary
Dictionary is an unordered collection of key-value pairs. It can be defined within braces { key: value } where key and value can be of any data type.
Example:
1. Using the key we can fetch value in constant time O(1).
Example:
2. Different types of keys.
Example:
6. Set
Set is an unordered collection of Unique items. It is defined using { } values separated by comma.
Example:
1. Set is an unordered datatype so there is no index.
Example:
7.FrozenSet
FrozenSet is a modified version of Set. FrozenSet values canβt be changed after creation while we can add or remove values from Set.
Example:
1. We canβt add or remove value from frozenSet.
These all are the data types in python. Hope you got something from this article.
Thanks for Reading !!!