‘Data types’ in Python is a common terminology that is used. Before kick-starting data types in Python, it is crucial to know what a data type is. In the programming world, a data type or type is nothing but an attribute to instruct the compiler or interpreter on how the programmer or developer is intended to use the data.
Most of the programming languages support basic data types of integer numbers, floating numbers, characters, and Booleans.
The Impact of Data Type
The data type in Python is also about specifying what relational, logical, or mathematical operations can be applied to the value that variables have.
Now, you will explore a couple of examples for data types in Python and types of supported values
Data Type |
Type of Value |
String |
Alphanumeric Characters |
Boolean |
True or False |
You have now seen the general data types. But what do data types in Python mean? How are data types empowering Python programming? Hold tight! You are going to discover that soon.
What does it really mean for Data Types in Python?
As we are aware, Python is an Object-Oriented Programming Language. In Python, everything is an object.
Hence, in Python the data types are classes, and instances of these classes are nothing but variables.
Built-in Data Types in Python
Python has its own built-in data types, and they are listed below:
str (Text Type)
int, float, complex (Numeric Type)
list, tuple (Sequence Type)
dict (Mapping Type)
set (Set Type)
bool (Boolean Type)
bytes, bytearray, memoryview (Binary Types)
These data types are nothing but classes in Python, as mentioned earlier.
Identify Data Types in Python
Programmers can use type keywords to identify the data type of the variable. The syntax is type (variable).
Let us see a couple of examples of how to identify the data type of variable in Python.
Program for type function
Output
Now, let’s get a deeper understanding of the Python data types.
String data type in Python
A string is defined as a collection of Unicode characters. A string in Python can be defined using single quotes or double quotes, that is ‘Test String’ and ‘’Test String” are the same.
You will now see a quick example for string data type in Python. In this example, make sure to use the print() function to display the string in Python.
Program for String
Output
You have seen how to define strings using single quotes and double-quotes. All good, but in case you want to display a multiline string, any idea how can that be achieved?
Multiline strings are supported in Python using triple quotes. We can use either triple single quote ‘ ‘ ‘ or triple double quotes “ “ “ to denote a multiline string.
Program for MultilineStrings using Triple Quotes
Output
Numeric data type in Python
As learnt above, the following falls under the Python numeric category.
- int
- float
- complex
Now, see a quick example for number data type in Python. In this example, please use the print() function to display the numeric value in Python.
i) Int – Integer or Int in Python is nothing but a whole positive or negative number. It has no limit on the length and no decimals.
Program for Integers
Output
ii) Float – Float or floating-point number in Python is nothing but a decimal containing a positive or negative number. Additionally, float can also have scientific numbers with an “e” to indicate the power of 10.
Program for Float
Output
iii) Complex – Complex numbers are formed using ‘j’ as the imaginary part. The complex numbers are considered as two parts known as real and imaginary. The syntax is x + yj.
Program for Complex
Output
Sequence Data Type in Python
Under the sequence data type, the following can be seen: i) List, ii) Tuple, and iii) Range. Now, you will explore them one-by-one.
i) List – A list is an organized sequence of items. A list can contain different data type items, and allows duplicates, and mutable values (can be changed after created). It is flexible in nature and it is the widely used data type in Python.
Program for List
Output
ii) Tuple – Tuple is also an organized sequence of items like a list. The key difference between these two is that Tuple is immutable i.e., once created, it cannot be modified further.
Program for Tuple
Output
Mapping Data Type in Python
Dictionary is defined as an unordered collection of key-value pairs. Key is a must to retrieve the value from the map i.e., key-value pair. Dictionaries are being defined with braces {} in Python.
Dictionary items are organized or ordered, and do not allow duplicates and changeability.
The syntax is {‘key’: ‘value’}
Program for Dict
Output
Set Data Type in Python
Set is an unordered and unindexed collection of data. In Python, a set can be defined using curly braces. The syntax is {item1, item2, item3, …}
Program for Set
Output
Boolean Data Type in Python
Boolean always represents either True or False. Programmers can evaluate any expression in Python using the Boolean variable and get either True or False as the value.
Program for Boolean
Output
Python also supports bool() function that evaluates any given value, and returns True or False.
Program for Bool()
Output
Conversion Between Data Types in Python
You have looked at all the different data types that are available built in for Python programming and have seen how to set those data type values. But how do you convert them from one data type to another? Let’s say, a programmer wants to convert the type of value from one data type to another data type in Python. Python provides the simplest way of conversion from one data type to another.
It provides different types of conversion functions, which can be used to convert a variable value from one data type to another. Some conversion functions are int(), float(), and str().
The syntax is a data type to be converted(x) i.e., int(x).
Program for float to int conversion.
Output
Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer
Conclusion:
Data type in Python is a crucial term in programming. Variables can store a variety of data, and different types can perform different tasks. A data type, also referred to as type, is a property of data that informs the interpreter or compiler about how the programmer intends to use the available information.
The categorization or grouping of data objects is known as data forms. It denotes the type of value that determines which operations can be performed on a given collection of data. Data types are simply classes, and variables are instances (objects) of these classes, since everything in Python programming is an entity.
In this tutorial, you have learned what is a data type, different data types in python with examples, how to verify the data type of specific variable in Python, and data Type Conversion in Python. To learn more about it, please join the Python Training Course from Simplilearn that will teach you the fundamentals of Python, conditional statements, data operations, shell scripting, and Django. This certification course on Python consists of 38 hours of applied Learning, and 8 hours of online self-paced learning to provide you the much-needed practical programming experience and help you on your way to a very rewarding career as a Python programming expert.
Are there any queries related to this “data types in Python” tutorial that we could answer for you? Feel free to share them with us by placing them in the comments section of this tutorial. Our subject matter experts will review and resolve them as soon as possible!