Python Data Types

 

As discussed in the python variables chapter, the data type of variable is based on the assigned value. For example, if you assign a string value, the type of variable is string the same way if you assign a numeric value, the type of variable is a number.

 

Generally, the data types are useful to identify the type of variables so that we will get to know the type of operations such as logical, relational, or mathematical we can perform on the variables data without causing an error.

 

Python has various built-in or standard data types available to perform standard operations on the variables based on the assigned values.

Python Standard Data Types

The following are the various types of built-in or standard data types in python.

 

TypeData Types
Number Types int, float, complex
Text Type str
Boolean Type bool
Sequence Types list, tuple, range
Mapping Types dict
Set Types set, frozenset
Binary Types bytes, bytearray, memoryview

Python Get Variable Data Type

In python, by using type() function, you can get the data type of any variable or object based on your requirement.

 

Following is the example of getting the data type of variables in python using type() function.

 

a = 10
print(type(a)) #<class 'int'>
b = "Welcome"
print(type(b)) #<class 'str'>
c = 15.7
print(type(c)) #<class 'float'>

If you observe the above example, we created different variables by assigning different values. The type() function has returned the data type of variables based on the assigned value.

 

In python, everything will treat as an object, so the data types are the class objects, and variables are the instances of those class objects.

 

As discussed, python will set the variable type based on the assigned value. If you change the assigned value of a variable to another value, then the python will also change the variable type.

 

Following is the example of changing the type of a variable by assigning the different values in python.

 

a = 10
print(type(a)) #<class 'int'>
a = "Welcome"
print(type(a)) #<class 'str'>

If you observe the above example initially, we assigned integer value (10) to the variable, so the type of variable is set to an integer (int). After changing the value of a variable to string (Welcome), the variable type also changed to string (str).

Python Numbers

In python, the number data types will store only numeric values. The three-number types such as integers, floating-point numbers, and complex types will consider as number types in python.

 

Following is the example of creating the variables with different numeric types in python.

 

a = 10
b = 31.54
c = 3j
print(type(a)) #<class 'int'>
print(type(b)) #<class 'float'>
print(type(c)) #<class 'complex'>

If you observe the above example, we created different variables (a, b, c) in python by assigning integer, floating-point, and complex number types. The python complex numbers are written as a combination of real and imaginary parts (j).

 

To learn more about number types in python, visit Python Numbers.

Python Strings

Generally, the string is a sequential collection of characters. In python, you can use either a single quote ('), double quote ("), or triple quote (''' or """) to represent the strings. The triple quotes (''' or """) are useful when you want to represent multi-line strings.

 

Following is the example of defining the variables with string type in python.

 

a = 'welcome'
b = "learn python"
c = '''Python tutorial
with examples'''
d = """Best learning resource
for programming"""
print(a)
print(b)
print(c)
print(d)

When you execute the above python example, you will get the result as shown below.

 

welcome
learn python
Python tutorial
with examples
Best learning resource
for programming

To learn more about string types in python, visit Python Strings.

Python Boolean

In python, the variables with values either True or False will treat as boolean-type variables. If you observe the starting letters (T and F) of boolean values (True, False) are capital, and you need to write it as they are; otherwise, you will get an error because python is a case-sensitive programming language.

 

Following is the example of creating the variables with boolean values in python.

 

a = False
b = True
print(type(a)) //#<class 'bool'>
print(type(b)) //#<class 'bool'>

Python List

In python, List is a collection of ordered sequences of items, and you can store the values of different data types.

 

In python, you can create the list by enclosing the list items within brackets [ ] and the list items must be separated by commas.

 

Following is the example of creating a list with different types of items in python.

 

a = [30, 10, 20, "tutlane"]
print("a = ", a)
print(type(a))

When you execute the above python program, you will get the result as shown below.

 

a = [30, 10, 20, 'tutlane']
<class 'list'>

To learn more about the list in python, visit Python Lists with Examples.

Python Set

In python, Set is a collection of an unordered sequence of unique items, and you can store the values of different data types.

 

In python, you can create the Set collection by enclosing the list items within braces { } and the set items must be separated by commas.

 

Following is the example of creating the Set with different types of items in python.

 

a = {30, 10, 10, 20, "tutlane"}
print("a = ", a)
print(type(a))

When you execute the above python program, you will get the result as shown below.

 

a = ['tutlane', 10, 20, 30]
<class 'set'>

If you observe the above result, the Set collection has returned the unique elements of the unordered list. In python, the order of items in Set collection will vary whenever you execute the program.

 

To learn more about sets in python, visit Python Sets with Examples.

Python Tuple

In python, Tuple is a collection of an ordered sequence of items, and it is same as the List, but the only difference is the Tuples are immutable. Once the tuple object is created, we are not allowed to make any changes to the tuple object.

 

In python, you can create the tuple by enclosing the items within parentheses ( ) and the tuple items must be separated by commas.

 

Following is the example of creating the tuple with different types of items in python.

 

a = (30, 10, 20, "tutlane")
print("a = ", a)
print(type(a))

When you execute the above python program, you will get the result as shown below.

 

a = (30, 10, 20, 'tutlane')
<class 'tuple'>

 If you try to change the tuple object by modifying the existing values, you will get an error, as shown below.

 

a = (30, 10, 20, "tutlane")
a[0] = 50
print("a = ", a)
print(type(a))

If you observe the above example, we are trying to change the value of the tuple object. When you execute the above python program, you will get an error like as shown below.

 

TypeError: 'tuple' object does not support item assignment

To learn more about tuple in python, visit Python Tuples with Examples.

Python Dictionary

In python, Dictionary is a collection of an unordered sequence of key-value pair items. In python, while storing the elements in the dictionary object, you need to make sure that the keys are unique because the dictionary object will allow us to store duplicate values, but the keys must be unique.

 

In python, you can create the dictionary by enclosing the key-value pair items in key:value form within the braces { } and the key-value pair items must be separated by commas.

 

Following is the example of creating the dictionary with different key-value pair items in python.

 

a = {1: "Welcome", "Id": 10}
print("a = ", a)
print(type(a))

When you execute the above python program, you will get the result as shown below.

 

a = {1: 'Welcome', 'Id': 10}
<class, 'dict'>

To learn more about the dictionary in python, visit Python Dictionaries with Examples.

Python Data Types Conversion

In python, you can convert one data type to another by using different conversion functions such as int(), float(), str() based on your requirements.

 

Following is the example of converting variables from one data type to another in python.

 

a = int(10.56)
b = float(46)
c = str(13)
print(a)
print(b)
print(c)
print("A Type: ", type(a))
print("B Type: ", type(b))
print("C Type: ", type(c))

If you observe the above example, we convert one data type to another based on our requirements.

 

When you execute the above python program, you will get the result as shown below.

 

10
46.0
13
A Type: <class 'int'>
B Type: <class 'float'>
C Type: <class 'str'>

To learn more about type conversion in python, visit Python Type Conversion.