What data types does Python have?

Python data types are essential for representing, processing and using data. By utilizing different data types, you can store information in an efficient manner and optimize the performance of your application.

What are Python data types?

Python data types are value categories that are used to represent different kinds of data. They dictate how information can be stored and manipulated. Python provides a range of different data types, including integers (whole numbers), floats (decimal numbers) and strings (character strings). More advanced types include lists, tuples, dictionaries and sets. Data types facilitate data structuring and processing, making them pivotal to Python programming.

The primary objective of Python data types is to structure data according to predefined rules so the needs of diverse applications can be fulfilled. Each data type possesses distinct functions and properties. For instance, lists maintain items in an ordered sequence, while dictionaries employ key-value pairs for targeted data retrieval. Selecting suitable data types for your data will make your program flexible and easier to maintain.

What kinds of data types are there in Python?

Python has various built-in data types, including:

  • Numeric data types: int, float, complex
  • Strings: str
  • Sequential data types: list, tuple, range
  • Binary types: bytes, bytearray, memoryview
  • Dictionaries: dict.
  • Boolean data types: bool.
  • Sets: set, frozenset

Numerical data types

There are several Python data types that you can use to work with numbers:

  1. Integer (int): The integer type represents whole numbers without decimal places.
  2. Long (long): Long is used for integers with an unlimited length. As of Python 3, long and int are merged.
  3. Float (float): The float type includes numbers with decimal places.
  4. Complex (complex): The complex type includes complex numbers with a real part and an imaginary part, indicated by the suffix j.
a=3 
 
# Variable with float value. 
b=3.17 
 
# Variable with complex value. 
c=50+7j
python

Strings

A Python string (str) represents a sequence of characters. You can mark them with single, double or triple quotation marks.

# Single quotes 
str1 = 'Hello World!' 
 
# Double quotes 
str2 = "This is a string." 
 
# Triple quotes for multiline strings 
str3 = '''This is a multiline string.'''
python

In Python, strings are immutable, meaning they cannot be changed once created. However, strings support numerous methods and operations for manipulation, concatenation and analysis. You can store the results in variables to get new strings.

Examples of string operations:

  • Length of a string: len(str)
  • Slicing: str[start:end]
  • Concatenate strings: str1 + str2

Sequential data types

Sequential data types in Python are data structures that store an ordered collection of elements. They allow access to elements based on their position within the sequence. There are several sequential Python data types:

Lists (list): Python lists are modifiable sequential data types that represent an ordered collection of elements. You can change, add and remove elements in a list. Lists are created using square brackets and contain elements of different data types.

my_list = [1, 2, 3, 'Hello', 'World']
python

Tuple (tuple): Tuples are unchangeable sequential data types which, like lists, display an ordered collection of elements. In contrast to lists, tuples cannot be changed afterwards. Use round brackets for tuples.

my_tuple = (4, 5, 6, 'Python')
python

Range (range): This is a special Python data type utilized for generating sequences of numbers, often used in loops and iterations. The range data type creates a sequence of integers within a specified range. The range object generates numbers on demand rather than storing them as a complete list in memory, enhancing efficiency, particularly with large number sequences.

# Range from 0 to 4 
my_range = range(4) 
for i in my_range: 
    print(i) 
# Output: 0, 1, 2, 3
python

Binary types

Bytes (bytes): The data type bytes represents an unchangeable sequence of bytes. Bytes can be created using the bytes() constructor or the prefix b.

my_bytes = b'Hello'
python

bytearray (bytearray): In contrast to bytes, bytearray belongs to the modifiable Python data types, representing a sequence of bytes. This means that you can modify the values after declaration.

my_bytearray = bytearray(b'Python')
python

Dictionaries

In Python, a dictionary (dict) is a data structure that stores an unordered collection of elements in the form of key-value pairs. Unlike lists or tuples, which contain an ordered sequence of elements, unique keys are used to access elements in a dictionary.

my_dict = {
    "name": "Max",
    "age": 25,
    "city": "Berlin"
}
python

Boolean data types

Boolean Python data types represent truth values that can be either true (True) or false (False). This data is crucial for logical evaluations and decisions within a program.

a = True
b = False
result_1 = (a and b) # returns False
result_2 = (a or b) # returns True
result_3 = (not a) # returns False
python

Sets

A set is an unordered collection of unique values that doesn’t allow duplicates. You can use it to store multiple elements where each element is unique.

my_set = {1, 2, 3, 4, 5}
python

A frozenset is an unchangeable version of a set. Once created, elements cannot be added, removed or changed.

my_set = {1, 2, 3, 4, 5}
frozen_set = frozenset(my_set)
python
Web Hosting
Fast, scalable hosting for any website
  • 99.9% uptime
  • PHP 8.3 with JIT compiler
  • SSL, DDoS protection, and backups
Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top