Table of contents
1.
Introduction
2.
1. Numeric Data Type
3.
2. String Data Type
4.
3. List Data Type
5.
4. Tuple Data Type
5.1.
5. Boolean Data Type
6.
6. Set Data Type
7.
7. Dictionary Data Type
8.
Frequently Asked Questions
8.1.
What is data type category in Python? 
8.2.
How many data types are there in Python? 
8.3.
What is the largest data type in Python? 
8.4.
What is the smallest data type in Python? 
9.
Conclusion
Last Updated: Aug 10, 2024
Easy

Python Data Types

Author APURV RATHORE
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Python is the programming language with which one can quickly begin their programming journey because of its readability and simplicity. However, to master any programming language, not just Python, but any programming language, we must first have a solid and deep comprehension of its core concepts. 

Python Data Types

Different types of data types supported by Python are discussed below. 

  1. Numeric data types
  2. String data types
  3. List data types
  4. Tuple data types
  5. Boolean type
  6. Set data types
  7. Dictionary data type

You can also read about the Multilevel Inheritance in Python, Swapcase in Python

1. Numeric Data Type

Numeric data types in Python represent data with a numeric value. Python supports three types of numeric data types:- integers, floats, and complex. 

  • Integers: Integers are represented as int in Python. Integers can be any length; their length is only limited by the amount of memory available in the system. This means that integers contain all numbers, positive and negative, but they don’t contain decimal points.
  • Float: They are represented as float in Python. Floats are real numbers with a decimal point to distinguish between fractional and integer portions. It is accurate up to 15 decimal places. 
  • Complex Numbers: They are represented as complex in Python. Complex numbers can be represented as (x+yj), in which x represents the real part and y represents the imaginary part. 

Additionally, we can use the type() function to determine the data type of a variable. 

# integer
var_int = 50
print(type(var_int))
# float
var_float = 50.51
print(type(var_float))
#complex
var_complex = 1 + 2j
print(type(var_complex))

 

Output:

<class 'int'>
<class 'float'>
<class 'complex'>

2. String Data Type

Strings are arrays of bytes in Python that represent Unicode characters. A string is made up of one or more characters enclosed in a single, double, or triple quotation. Multiline strings can be created using triple quotation marks. Python has no character data type; a character is a one-length string. The str class is used to represent it. 

Strings can be of any length. The only limitation is the memory of the system. 

Strings are immutable in Python, which means that string does not support modification or deletion. If you try to modify the string, Python will throw an exception. 

Individual characters of the string can be accessed through indexing in Python. 

s = "This is a string"
print("This is a normal string:",s)

s = '''This is
another  string'''
print("This is a multiline string:",s)
You can also try this code with Online Python Compiler
Run Code

 

Output:

This is a normal string: This is a string
This is a multiline string: This is
another  string
You can also try this code with Online Python Compiler
Run Code

3. List Data Type

The list is a collection of items in an ordered sequence. It's one of Python's most popular data types, and it's pretty versatile. All items in a list don’t have to be of the same type. One of the best properties of the List is that it can hold the number of multiple elements simultaneously while still maintaining their order.

The difference between list and array in Python is that lists can store elements of different data types, while the data types of elements in an array should be the same. 

List can be created by placing the sequence inside square brackets. 

l1 = [1,2,3]
print("List containing integers:\n",l1)

l2 = ["string1", "string2", "string3"]
print("List containing strings:\n",l2)

l3 = ["string1",2,"string3"]
print("List containing both integers and string:\n",l3)

List containing integers:
 [1, 2, 3]
List containing strings:
 ['string1', 'string2', 'string3']
List containing both integers and string:
 ['string1', 2, 'string3']
You can also try this code with Online Python Compiler
Run Code

 

The index number can be used to access the list items. To go to a specific item in a list, use the index operator []. Negative sequence indexes in Python represent positions from the array's end. It is sufficient to write List[-2] instead of needing to compute the offset as in List[len(list)-2]. Negative indexing indicates starting at the end, with -1 being the last item, -2 denoting the second-to-last item, and so on.

Also see, Python Operator Precedence

4. Tuple Data Type

The tuple is an ordered collection of Python objects, similar to a list. The sole difference between a tuple and a list is that tuples are immutable, meaning that once generated, they cannot be changed. The tuple class is used to represent it.

They can be created by placing the sequence of elements separated by a comma inside a parenthesis. They can contain any data type or combination of data types. 

The element inside a tuple can be accessed with an indexing method, similar to the list. 

t1 = (1,2,3)
print("tuple of integers:",t1)
t1 = (1,"string2",3)
print("tuple of integers and string:",t1)
print("type of t1:",type(t1))
You can also try this code with Online Python Compiler
Run Code

 

Output:

tuple of integers: (1, 2, 3)
tuple of integers and string: (1, 'string2', 3)
type of t1: <class 'tuple'>
You can also try this code with Online Python Compiler
Run Code

5. Boolean Data Type

Boolean data types in Python can have two values: True or False. Non boolean objects can also be evaluated and determined to be True or False in a boolean context. 

a = True
print(type(a))
a = False
print(type(a))
You can also try this code with Online Python Compiler
Run Code

 

Output:

<class 'bool'>
<class 'bool'>
You can also try this code with Online Python Compiler
Run Code

6. Set Data Type

Set is an unordered data type collection that is iterable, mutable, and has no duplicate elements in Python. The sequence in which elements of a set appear is undefined.

Set can be created by placing the sequence of elements inside curly braces {}, or by passing an iterable inside the built-in set() function. 

The set elements cannot be indexed by indexing since sets are an unordered collection of items. 

s = set([1,2,3])
print("set:",s)
s = {1,2,3,4}
print("set:",s)
print(type(s))
You can also try this code with Online Python Compiler
Run Code

 

Output:

set: {1, 2, 3}
set: {1, 2, 3, 4}
<class 'set'>
You can also try this code with Online Python Compiler
Run Code

7. Dictionary Data Type

In Python, a dictionary is an unordered collection of data values, similar to a map, used to store data values. Unlike other Data Types, which only contain a single value as an element, Dictionary holds a key-value pair. A colon separates each key-value pair in a Dictionary, whereas a comma separates each key.

Dictionary can be created by placing a sequence of key-value pairs separated by colon within curly braces. It can also be made using the built-in function dict().

The value of the key in the dictionary can be updated or indexed using square brackets. Get function can also be used to get the value of a key. 

d = {1:2,2:3,3:4}
print(type(d))
d = dict()
d[1]=2
print(d.get(1))

>>>
<class 'dict'>
2
You can also try this code with Online Python Compiler
Run Code

You can try it on online python compiler.
 

Check out Escape Sequence in Python

Frequently Asked Questions

What is data type category in Python? 

Data type categories in Python include numeric types (integers, floats), sequence types (strings, lists, tuples), mapping types (dictionaries), set types (sets), and boolean types.

How many data types are there in Python? 

Python has several built-in data types, including integers, floats, strings, lists, tuples, sets, dictionaries, and booleans. This core set covers a wide range of data representations.

What is the largest data type in Python? 

The largest data type in Python is int, which can handle arbitrarily large values, limited only by available memory. Python integers are not bound by fixed sizes.

What is the smallest data type in Python? 

The smallest data type in Python is bool, which represents boolean values True and False. It occupies minimal space compared to other data types.

Conclusion

In conclusion, Python's diverse data types provide flexibility and efficiency for various programming needs. From basic types like integers and floats to complex types like lists and dictionaries, Python supports both simple and sophisticated data management. The language's dynamic typing system and rich set of built-in types facilitate robust and adaptable coding, making it suitable for a wide range of applications.

Recommended Reading:

Live masterclass