Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Python Data Types

Last Updated on November 29, 2022 by Prepbytes

In this article, we will be discussing what is data type in Python Programming Language and the role of data types as well as the importance of python data types. By the end, we will have strong concepts on what is data type in python and its myriad types.

About Python

Python is a high-level interpreted programming language that supports object-oriented programming language. It is dynamically typed and code readability is easier as compared to other programming languages.

Python supports primitive as well as non-primitive data types that come built-in with the programming language that can be assumed as built-in data structures. Apart from the non-primitive ( or primitive ) data types, Python also extends its support to user-defined data structures.

What is Data Type in Python

All the data items are stored in various python data types available. The distinction of different data is made on top of the python data types which are as follows:-

Primitive Data Type

The Data Types are predefined and have a fixed size to hold the value. The size of primitive data types depends on the allocation provided to the data type by the programming language.

Non-Primitive Data Type

These data types can be defined by programmers and can store up to multiple values in an ordered or unordered manner. List, Tuple, Dictionary and Set are Non-Primitive Data Types.

On looking at the example of each of the python data types, the answer to what is data type in python will be clearer as we will focus on each of the data types. There are different python data types mentioned as follows:-

Text Data Types – str

One among the many python data types used specifically for storing words or sentences. Since there is no character data type in Python, str can be used to store a single character as well.

The condition for string is that it must be enclosed within single quotes or double quotes to be accessed.

For example:

x = "Prep Bytes"

Numeric Data Types – int, float, complex

To store the numerical values, the numeric data type is in place to hold numeric values whereas int object holds integer values. The float object holds floating point values with decimals and provides precision. E or e is the scientific notation used for extremely large values.

Complex class is used to store complex numbers in the format a + ib where i is the imaginary part.

For example:

a = 33 #int
a = 24.5 #float
a = 2j #complex

Sequence Data Types – list, tuple, range

Sequence data types are used to store ordered collections of data. The data be multiple values which can be accessed with the help of unpacking or indexing.

A list is a mutable and non-primitive data type. It can hold multiple values of different data types. A list can be created by opening and closing the big brackets or list() method. List elements are accessed or altered by entering a valid or existing element index.

ans = [] #Creation of list
ans = list() #Creation using list method

ans.append(“Prep”) #Append a value
ans.append(“Bytes”) #Append a value

ans[-1] # Access the element at last index
ans[0] #Access the element at first index

On the other hand, a tuple is immutable which means the values in a tuple can not be altered. Tuples can be created using parenthesis along with all the elements that we need to place.

ans = ("Prep","Bytes")
ans = tuple(("Prep",'Bytes")) 

ans[-1] # Access the element at last index
ans[0] #Access the element at first index

Range class can take the starting value, ending value and step increment/decrement parameter (default value set as 1) to return an immutable object in the given range.

for i in range(0,10):
    print(i,end=" ")

Output: 0 1 2 3 4 5 6 7 8 8

Mapping Data Types – dict

Mapping data types is used to map a key to a value. Hence, no iteration is required in the collection of elements as the key can be accessed to retrieve the value in a more optimal manner.

dict method is used to create the dictionary object in python or simply opening and closing the curly brackets would work. Ordering is not performed in the dictionary as the values are accessed with the help of keys.

x = {"course" : "BTech", "duration" : 4} #Using Standard Method
x = dict(course="BTech", duration=36) # Using Dict Method

Set Data Types – set, frozenset

Set Data Types are used to store an unordered collection of elements. A set can be iterated, is immutable and holds unique elements.

A set can be created using the set method and values can be directly checked using the ‘in’ operator as a set is not iterable or indexed.

a = {"Hello", "Prep", "Bytes"}
b = set(("Hello", "Prep", "Bytes"))

Frozenset follows the exact properties of a set except that they are immutable and any attempt to alter or include an object/element will result in an error.

a = frozenset(("Hello", "Prep", "Bytes"))

Bool Data Type – True, False

Not only restricted to Python Data Types but an ever-existent data type across programming languages where a statement is either true or false. Not to forget that the first character must be uppercase in both True and False or else it will result in an error.

a = True
a = bool(3)

More on Python Data Types

Now that we have an idea of what is data type in python, we must be able to find out what class the object belongs to or what is the data type in python of the object is. To identify this, we use the type() method to find.

print(type(5))
print(type("PrepBytes"))
print(type(True))

Output:


Conclusion

We understood how python data types work under the hood and understood what are data types in python along with its practical use and code.I hope you liked this article on what is data type in Python. We hope to see you at PrepBytes soon.

Leave a Reply

Your email address will not be published. Required fields are marked *