free stats

Python Data Types

Last Update : 20 Aug, 2022 Python, Programming

In this tutorial, you will learn about Python data types and the usage of data types.

Data types are a very important concept in computer programming. Also, every value in Python has a data type.

The categorization or classification of knowledge items is called data types. It represents some value that tells what operations can be performed using a specific data type.

So, Everything is an object in the Python programming language. Also, Data types are Python classes, and variables are objects of these Python classes.

Variables are used to store different types of data. Also, different data types can do different things.

There are many built-in data types available in the Python programming language. These data types can be categorized as follows.

  1. Text data type :- str
  2. Numeric data types :- int, float, complex
  3. Boolean data type :- bool
  4. Sequence data types :- list, tuple, range
  5. Mapping data type :- dict
  6. Set data types :- set, frozenset
  7. Binary data types :- bytes, byteArray, memoryView
  8. None data type :- NoneType

 

Python Getting the Data Type

To get the data type of any object, You can use the type() function.

The type() is a built-in function of the Python programming language. Also, This function is mostly used for debugging purposes.

This function returns the data type of the object passed to it as an argument.

For example -:

#Print the data type of the variables x and y.

x = "UXPython Tutorials"
print(type(x)) 

y = 225
print(type(y))

This program produces the following result -:

<class 'str'>
<class 'int'>

 

Python Setting the Data Type

Python is a dynamically-typed language. Therefore, When you create variables and functions, you do not need to specify the data type.

Also, when you assign a value to a variable, the data type is automatically set in the Python programming language.

For example -:

# Assign a 'str' data type value to variable x
x = "Hello UXPython"

# Display x variable
print(x)

# Display the data type of x
print(type(x)) 


# Assign a 'int' data type value to variable x
x = 200

print(x)
print(type(x)) 

# Assign a 'float' data type value to variable x
x = 101.5

print(x)
print(type(x))


# Assign a 'complex' data type value to variable x
x =2j

print(x)
print(type(x))
	
# Assign a 'list' data type value to variable x
x = ["apple", "banana", "cherry"]

print(x)
print(type(x))	

# Assign a 'tuple' data type value to variable x
x = ("apple", "banana", "cherry")

print(x)
print(type(x))

# Assign a 'range' data type value to variable x	
x = range(6)

print(x)
print(type(x))

# Assign a 'dict' data type value to variable x	
x = {"name" : "Nirosh", "age" : 27}

print(x)
print(type(x))

# Assign a 'set' data type value to variable x
x = {"apple", "banana", "cherry"}

print(x)
print(type(x))

# Assign a 'frozenset' data type value to variable x
x = frozenset({"apple", "banana", "cherry"})

print(x)
print(type(x))	

# Assign a 'bool' data type value to variable x
x = True

print(x)
print(type(x))	

# Assign a 'bytes' data type value to variable x
x = b"UXPython"

print(x)
print(type(x))

# Assign a 'bytearray' data type value to variable x
x = bytearray(6)	

print(x)
print(type(x))

# Assign a 'memoryview' data type value to variable x
x = memoryview(bytes(6))

print(x)
print(type(x))

# Assign a 'NoneType' data type value to variable x	
x = None

print(x)
print(type(x))

This program produces the following result -:

Hello UXPython
<class 'str'>

200
<class 'int'>

101.5
<class 'float'>

2j
<class 'complex'>

['apple', 'banana', 'cherry']
<class 'list'>

('apple', 'banana', 'cherry')
<class 'tuple'>

range(0, 6)
<class 'range'>

{'name': 'Nirosh', 'age': 27}
<class 'dict'>

{'banana', 'apple', 'cherry'}
<class 'set'>

frozenset({'banana', 'apple', 'cherry'})
<class 'frozenset'>

True
<class 'bool'>

b'UXPython'
<class 'bytes'>

bytearray(b'\x00\x00\x00\x00\x00\x00')
<class 'bytearray'>

<memory at 0x000001D5D6ACF1C0>
<class 'memoryview'>

None
<class 'NoneType'>

 

Setting the Specific Data Type

Although Python is a dynamically-typed language, If you need to specify the data type of a variable, you can use the following built-in functions.

# Setting the specific 'str' data type value to variable x
x = str("Hello UXPython")

print(x)
print(type(x)) 

# Setting the specific 'int' data type value to variable x
x = int(200)

print(x)
print(type(x)) 

# Setting the specific 'float' data type value to variable x
x = float(101.5)

print(x)
print(type(x))


# Setting the specific 'complex' data type value to variable x
x = complex(2j)

print(x)
print(type(x))
	
# Setting the specific 'list' data type value to variable x
x = list(["apple", "banana", "cherry"])

print(x)
print(type(x))	

# Setting the specific 'tuple' data type value to variable x
x = tuple(("apple", "banana", "cherry"))

print(x)
print(type(x))

# Setting the specific 'range' data type value to variable x	
x = range(6)

print(x)
print(type(x))

# Setting the specific 'dict' data type value to variable x	
x = dict({"name" : "Nirosh", "age" : 27})

print(x)
print(type(x))

# Setting the specific 'set' data type value to variable x
x = set({"apple", "banana", "cherry"})

print(x)
print(type(x))

# Setting the specific 'frozenset' data type value to variable x
x = frozenset(("apple", "banana", "cherry"))

print(x)
print(type(x))	

# Setting the specific 'bool' data type value to variable x
x = bool(True)

print(x)
print(type(x))	

# Setting the specific 'bytes' data type value to variable x
x = bytes(7)

print(x)
print(type(x))

# Setting the specific 'bytearray' data type value to variable x
x = bytearray(7)	

print(x)
print(type(x))

# Setting the specific 'memoryview' data type value to variable x
x = memoryview(bytes(7))

print(x)
print(type(x))

This program produces the following result -:

Hello UXPython
<class 'str'>

200
<class 'int'>

101.5
<class 'float'>

2j
<class 'complex'>

['apple', 'banana', 'cherry']
<class 'list'>

('apple', 'banana', 'cherry')
<class 'tuple'>

range(0, 6)
<class 'range'>

{'name': 'Nirosh', 'age': 27}
<class 'dict'>

{'cherry', 'banana', 'apple'}
<class 'set'>

frozenset({'apple', 'banana', 'cherry'})
<class 'frozenset'>

True
<class 'bool'>

b'\x00\x00\x00\x00\x00\x00\x00'
<class 'bytes'>

bytearray(b'\x00\x00\x00\x00\x00\x00\x00')
<class 'bytearray'>

<memory at 0x0000027C573401C0>
<class 'memoryview'>

 

Summary

This tutorial discusses Python data types, getting any variable type, assigning data to variables, and specifying data types using built-in Python functions. Also, you can obtain further details about these data types in future tutorials.

You found this tutorial / article valuable? Need to show your appreciation? Here are some options:

01. Spread the word! Use following buttons to share this tutorial / article link on your favorite social media sites.

02. Follow us on Twitter, GitHub ,and Facebook.