A datatype denotes the type of data required to store in a variable or in a memory space. Datatypes are categorized into two ways :
1. Built-in Datatypes : The datatypes that are already available in Python language are called Built-in types. Built-in datatypes are divided into five categories :
1. None datatype : It denotes that object does not contain any value or is null. It can be used as a default value for parameters inside a function.
2. Numeric datatype : It involves int, float, and complex datatype.
3. Sequence : It involves str for strings, bytes, bytesarray, list, tuple, range.
4. Sets : It involves unordered collection of elements. It includes set and frozen set.
5. Mapping : It represents a group of elements in the form of key-value pairs.
2. User-Defined Datatypes : The datatypes that are defined by users are called user-defined datatypes. Some of the user defined datatypes are : Array, Class etc.
Let us look at them in a gist.
1. int : It represents an integer number. Integer is something without fractional part. For example, 4,55,-456 are all integers.
Storing a integer value to a variable : a = 34
2. float : It represents floating point numbers. For example, 34.67, -56.32 are all floating point numbers.
Storing a floating value to a variable : a = 34.56
Sometimes we also use scientific notations such as E or e, to represent the power of 10. Like, we can write 2.9*10^7 as 2.9E7.
3. complex : It represents a number written in the form of x+yj, where x represents real part and y represents an imaginary part.
Storing a complex value to a variable : a = 4+3j.
4. str : It represents string datatype. A String represents a group of characters. They are generally enclosed in single quotes or double quotes.
Storing a string value to a variable :
s = “Code Of Geeks”
5. bytes : It represents a group of byte numbers. It covers all integers between 0 to 255.
Storing bytes value to a variable :
a = bytes( [23,45,56,45])
6. bytearray : It is similar to bytes datatype. But unlike bytes, bytearray can be modified.
7. list : A List may represent a group of elements. It can store different types of elements and can grow dynamically. In lists, elements are written inside [ ].
Storing list values to a variable :
lst = [3,4,4,2,’Code’]
8. tuple : Tuple is same as list i.e it represents a group of different types of elements but are enclosed in ( ) (parentheses). Tuples can not be modified and hence are read-only lists.
Storing Tuple values to a variable :
tup = (10,20,’Code’)
9. range() : It is a type of sequence datatype that represents the sequence of numbers. Range plays a key role in looping.
To create a range of numbers, we use,
range(5) # prints the numbers from 0 to 4.
Considering other example :
range(2,10,2)
Above statement will produce numbers from 2 to 9 in a step of 2.
Ex : 2,4, 6, 8
10. set : It represents the unordered collection of unique elements. Values in set are enclosed in curly braces { }.
Consider following code :
L = [3,3,5,6,7,7]
s = set(L)
print(s)
Output
{3,5,6,7}
11. Dict : It represents a dictionary. It contains different elements in the form of key-value pairs.
We create an empty list as : d = {}
So these were the different kinds of Python Datatypes. We’ll look at them in detail, in our upcoming lessons.
That’s all, see you in the next lecture.
