This is a detailed tutorial of Python Data Types. Learn the different built-in data types available in python to store and do different things with them.
We know that Python Variables can store values of different data types. In most of the programming languages, data types are defined into two types, built-in data types, and user-defined data types. In this article, we’ll discuss the built-in data types in detail with examples.
Table of Contents
Python Built-In Data Types
Python has a variety of different built-in data types that can be used for numerous different applications. These are given below in tabular form as per their data categories.
Data Category | Data Type(s) Available |
Textual | str |
Numerical | int , float , complex |
Sequence & Series | list , range , tuple |
Key-Value Pair Relation or Mapping | dict |
Set | set , frozenset |
Boolean | bool |
Binary | bytes , bytesarray , memoryview |
You must know that we need not explicitly declare the python variables with the data type. It will automatically pick up the data type for your values. But anytime, if you feel it is not picking the right data type for you, you can specify the data type yourself. We’ll know about it, later in this article.
The different built-in data types in Python are described below with examples in tabular form.
Data Type | Description | Example |
str | Used to store any textual content, whether it is a single character or a long string. Literals must be enclosed within quotes, either single or double. | name = “Gurmeet” or name = ‘Gurmeet’ |
int | Used to store integer values. It is written without any quotes. | age = 21 or temperature = -10 |
float | Used to store numbers with decimal values | distance = 52.55 |
complex | Used to store complex numbers | vector = 2i + 5j |
list | Used to store a list of any data types or even a list of values of different data types. Each element of the list is separated by commas and all of the elements are enclosed by square brackets.s | cities = [“Ludhiana”,”Chandigarh”,”Delhi”] or numbers = [1,2,3,4,5,6] or bio = [“Gurmeet”,21] |
range | Used to define a series of numbers. The function range() is used for it. |
points = range(1,11) |
tuple | Used to store a collection of data. It is enclosed by parenthesis and each element is separated by commas. | fruits = (“Mango”,”Banana”,”Apple”) |
dict | Pronounced as the dictionary. Used to store key-value pair data values. Written enclosed within Curly braces, a semi-colon is used to define the key-value relationship of each element separated with commas. | bio = {“name”:”Gurmeet”, “age”: 21} |
set | Used to store a set of values | words = {“Hello”,”Hi”,”How are you?”} |
frozenset | It is a set whose values can not be changed. | words = frozenset({“Hello”,”Hi”,”How are you?”}) |
bool | Used to store boolean values either True or False | isAvailable = True or isAvailable = False |
bytes | Byte immutable object consisting of Unicode 0-256 characters. | coded = b”Gurmeet” |
bytearray | An array of the bytes type objects | arr = bytearray(10) |
memoryview | Gives the memory for a bytearray | the_address = memoryview(bytearray(10) |
Initializing Variables & Checking their Data Types
You can check the data type of any variable or object in python using the built-in type()
function. Have a look at the following example.
1 2 3 4 |
name = "Gurmeet" age = 21 print(type(name)) print(type(age)) |
You can see in the output that it is detecting the variable name
belongs to str
datatype and the variable age
belongs to the int
datatype. Similarly, you can check the data type of any other variable or object in Python.
Example
The following example code illustrates the assignment of all of the built-in python data types. We’re also checking them using the type()
function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
name = "Gurmeet" age = 21 marks = 99.57 vector = 2 + 5j food = ["Pizza","Burger","Noodles"] numbers = range(1,51) collection = ("Swimming","Dancing","Running") bio = {"Name" : "Gurmeet", "age": 21} fruits = {"Manngo","Banana","PineApple"} juices = frozenset({"Rose","Choclate","Butter Scotch"}) isAvailable = False data = b"Hello Hi" dataArray = bytearray(100) view = memoryview(bytearray(10)) print(type(name)) print(type(age)) print(type(marks)) print(type(vector)) print(type(food)) print(type(numbers)) print(type(collection)) print(type(bio)) print(type(fruits)) print(type(juices)) print(type(isAvailable)) print(type(data)) print(type(dataArray)) print(type(view)) |
The output screenshot for the above code snippet is given below.
Python Type Conversion (Setting Data Type Manually)
In many programming languages, type conversion or typecasting is the term used to define conversion of the variable of one data type into another. The variables can also be converted from one data type to another in python as well.
Observe the following example. In it, we’ve defined the variable age
as a string first and then we’re converting it into integer data type.
1 2 3 4 |
age = "21" print(type(age)) age = int(age) print(type(age)) |
Initially, we’ve defined the variable age
in double-quotes, so it detects the variable to of str
datatype. Then we used the int()
function to set its data type as int
and you can see in the output screenshot that now it detects it as int
datatype. Similarly, you can change almost any other data type value from one to another.
But make sure the values contain valid literals that can actually be converted into the data type that you wanted. For example, if you will try to convert the text "Gurmeet"
into an integer value, it will give an error rather than type conversion.
Example
An example code given below illustrates some of the possible type-conversions. In this, we’re are basically setting specific data types to some variables.
1 2 3 4 5 6 7 8 9 10 11 |
age = 21 food = ["Pizza","Burger","Noodles"] fruits = {"Manngo","Banana","PineApple"} juices = frozenset({"Rose","Choclate","Butter Scotch"}) isAvailable = False print(type(list(juices))) print(type(set(food))) print(type(frozenset(juices))) print(type(str(isAvailable))) print(type(complex(age))) |
I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!