Data Types in Python

Discover the essential Python data types—integers, strings, lists, dictionaries, and more. Learn how to handle and manipulate data efficiently in Python with this beginner-friendly guide.
Dec 4, 2024
12 min read

Introduction 

Ever wondered how Python stores all the different kinds of information you throw at it? From numbers to words, lists to dictionaries, Python's got a bunch of different ways to handle data, and these are called datatypes. Think of datatypes as different containers that hold specific kinds of data, each with its own quirks and superpowers.

In this article, we’re going to break down Python’s most common datatypes—like integers, strings, lists, and more—so you can start understanding how Python works under the hood. Whether you're just getting started or need a quick refresher, this guide will help you make sense of Python’s core data handling tools. Let's dive in!

What are Python data types?


Data types are basically the categories that tell Python what kind of value you're working with. Whether it's a number, some text, or a more complex structure like a list, each data type has its own way of behaving. Knowing about data types helps Python understand how to handle and store the data you give it, making sure everything runs smoothly. From basic stuff like numbers to more advanced things like dictionaries, each type serves a different purpose in your code.

Before we get further into the various python data types, here’s a list of some words you might be hearing often, and what they mean.

Mutability

Mutability refers to whether an object’s value can be changed after it’s created. In Python, some data types are mutable, meaning you can modify them in place, while others are immutable, meaning once they’re created, their value cannot be changed.

Orderability

Orderability refers to whether or not a data type supports is stored in a set sequence

Iterability 

Iterability refers to the ability of an object to be looped over, or iterated through, one element at a time. In other words, an iterable is any object that can return its elements one by one, allowing you to use loops like for loops to process each element sequentially.

A grey rectangular with white textDescription automatically generated

The above image shows how an iterable is iterated with an iterator.

Hashability 

Hashability refers to whether an object of a certain data type is hashable i.e. if it can be used as a key in a dictionary or as an element in a set. In short, a hashable data type can be assigned a unique “hash” value which does not change in its lifetime.

This requires an object to be immutable. So, if you were wondering why objects were immutable in the first place, it is for this reason.

A mutable item has the benefit of being changeable, while an immutable item has better data integrity, faster and more efficient data retrieval and can be uniquely identified.

Setting a data type 

By default, Python is a high-level data language. This means that when you write code in it, you don’t have to worry about its architecture, memory management, or structure of code. As a result, the data type is automatically set when you simply assign a value to a variable.

Example

Data Type

x = "Hello World"

str

x = 20

int

x = 20.5

float

x = 1j

complex

x = ["apple", "banana", "cherry"]

list

x = ("apple", "banana", "cherry")

tuple

x = range(6)

range

x = {"name" : "John", "age" : 36}

dict

x = {"apple", "banana", "cherry"}

set

x = frozenset({"apple", "banana", "cherry"})

frozenset

x = True

bool

x = b"Hello"

bytes

x = bytearray(5)

bytearray

x = memoryview(bytes(5))

memoryview

x = None

NoneType

However, you can also specify the data type if you like

Example

Data Type

x = str("Hello World")

str

x = int(20)

int

x = float(20.5)

float

x = complex(1j)

complex

x = list(("apple", "banana", "cherry"))

list

x = tuple(("apple", "banana", "cherry"))

tuple

x = range(6)

range

x = dict(name="John", age=36)

dict

x = set(("apple", "banana", "cherry"))

set

x = frozenset(("apple", "banana", "cherry"))

frozenset

x = bool(5)

bool

x = bytes(5)

bytes

x = bytearray(5)

bytearray

x = memoryview(bytes(5))

memoryview

Categories

 

Illustrated in this chart is a comprehensive list of the built-in data types in Python.

Numeric 

A screenshot of a black and white numberDescription automatically generated

Numeric data types in python are used to represent numbers. Here are the 3 numeric data types you’ll encounter

In Python, numerical data types are used to represent numbers. Here are the main numerical data types you'll encounter:

  1. int (Integer): Represents whole numbers, both positive and negative, without any decimal points. For example, 5, -3, and 42 are integers.
    Syntax:

my_int = 10  # Positive integer

my_negative_int = -5  # Negative integer

  1. float (Floating-point): Represents numbers that have a decimal point. They can be positive or negative. For example, 3.14, -0.001, and 2.71828 are floats.

    Syntax:

my_float = 3.14  # Positive float

my_negative_float = -2.718  # Negative float

  1. complex (Complex): Represents numbers that have a real part and an imaginary part. The imaginary part is indicated with a j (or J). For example, 3 + 4j is a complex number with a real part of 3 and an imaginary part of 4.

    Syntax: 

my_complex = 2 + 3j # Complex number with real part 2 and imaginary part 3j

Numeric data functions

Here are some numeric data functions in Python

Text 

In Python, the text data type is primarily represented by the str (string) type. A string is a sequence of characters enclosed in quotes (single, double, or triple quotes). Here’s a brief overview of the str type:

Characteristics of str:

  • Immutable: Once a string is created, it cannot be changed. Operations that seem to modify a string actually create a new string.
  • Iterable: You can loop through a string, accessing each character individually.
  • Hashable: Strings can be used as dictionary keys or set elements, provided that the string itself is not modified.

Here are some of the commonly used built-in functions for strings

Function

Description

Syntax

capitalize()

Capitalizes the first character of a string

string.capitalize()

casefold()

Converts string to lowercase (more aggressive than lower())

string.casefold()

center()

Centers the string, padding it with a specified character

string.center(width, fillchar)

count()

Counts occurrences of a substring

string.count(substring)

encode()

Encodes the string into a specific encoding

string.encode(encoding)

endswith()

Checks if the string ends with a specified suffix

string.endswith(suffix)

expandtabs()

Replaces tabs with spaces

string.expandtabs(tabsize)

find()

Finds the first occurrence of a substring

string.find(substring)

format()

Formats string using placeholders

string.format(*args, **kwargs)

format_map()

Formats string using a dictionary

string.format_map(mapping)

index()

Finds the first occurrence of a substring (raises error if not found)

string.index(substring)

isalnum()

Checks if all characters are alphanumeric

string.isalnum()

isalpha()

Checks if all characters are alphabetic

string.isalpha()

isascii()

Checks if all characters are ASCII

string.isascii()

isdecimal()

Checks if all characters are decimal digits

string.isdecimal()

isdigit()

Checks if all characters are digits

string.isdigit()

isidentifier()

Checks if the string is a valid Python identifier

string.isidentifier()

islower()

Checks if all characters are lowercase

string.islower()

isnumeric()

Checks if all characters are numeric

string.isnumeric()

isprintable()

Checks if all characters are printable

string.isprintable()

isspace()

Checks if the string contains only whitespace

string.isspace()

istitle()

Checks if the string follows title case

string.istitle()

isupper()

Checks if all characters are uppercase

string.isupper()

join()

Joins elements of an iterable with the string as a separator

string.join(iterable)

ljust()

Left-justifies the string, padding it with a specified character

string.ljust(width, fillchar)

lower()

Converts all characters to lowercase

string.lower()

lstrip()

Removes leading characters (whitespace by default)

string.lstrip(chars)

maketrans()

Returns a translation table to be used with translate()

string.maketrans(x, y, z)

partition()

Splits string at first occurrence of a separator

string.partition(separator)

replace()

Replaces occurrences of a substring with another substring

string.replace(old, new)

rfind()

Finds the last occurrence of a substring

string.rfind(substring)

rindex()

Finds the last occurrence of a substring (raises error if not found)

string.rindex(substring)

rjust()

Right-justifies the string, padding it with a specified character

string.rjust(width, fillchar)

rpartition()

Splits string at last occurrence of a separator

string.rpartition(separator)

rsplit()

Splits string from the right

string.rsplit(separator)

rstrip()

Removes trailing characters (whitespace by default)

string.rstrip(chars)

split()

Splits string into a list of substrings

string.split(separator)

splitlines()

Splits string by line breaks

string.splitlines()

startswith()

Checks if the string starts with a specified prefix

string.startswith(prefix)

strip()

Removes leading and trailing characters (whitespace by default)

string.strip(chars)

swapcase()

Swaps case (lowercase to uppercase and vice versa)

string.swapcase()

title()

Converts string to title case

string.title()

translate()

Translates the string using a translation table

string.translate(table)

upper()

Converts all characters to uppercase

string.upper()

zfill()

Pads the string with zeros on the left

string.zfill(width)

Sequence

Sequence data types are types that represent ordered collections of items. These items can be of any type, and sequences allow you to access them using indices.

A black screen with white textDescription automatically generated

List 

  • A list is an ordered, mutable collection of items.
  • A list can contain elements of different types, including other sequences.
  • A list uses square brackets. 
  • E.g

 my_list = [1, 2, 3, 'four', [5, 6]]

Tuple

  • A tuple is also an ordered collection of items, but is immutable.
  • Tuples can contain elements of different types.
  • Tuples use round brackets. 
  • E.g 

my_tuple = (1, 2, 3, 'four', (5, 6))

Range

  • A range is an immutable sequence of numbers, commonly used in loops to iterate over a sequence of integers.
  • A range object can be created using the range() function.
  • Syntax:some text
    1. One argument: r = range(5)   (Represents numbers from 0 to 4)
    2. Two arguments: r = range(2, 5)  (Represents numbers from 2 to 4)
    3. Three arguments: r = range(1, 10, 2)  (Represents numbers from 1 to 9 with a step of 2)
  • The range function in Python, when called, creates a range object, which is a special type of iterable that represents a sequence of numbers. If you try to print or directly view a range object without iteration, it will show you its representation rather than the sequence of numbers it generates. 

Input

Output

print(r)

range(1, 5)

print(list(r))

[1, 2, 3, 4]

Common Sequence Functions/Methods

Function/Method

Description

Applies to

Syntax

len()

Returns the length of the sequence

list, tuple, range

len(sequence)

max()

Returns the largest item in the sequence

list, tuple, range

max(sequence)

min()

Returns the smallest item in the sequence

list, tuple, range

min(sequence)

sum()

Returns the sum of elements (only for numbers)

list, tuple, range

sum(sequence)

sorted()

Returns a sorted version of the sequence

list, tuple, range

sorted(sequence)

reversed()

Returns an iterator that iterates in reverse order

list, tuple, range

reversed(sequence)

index()

Returns the index of the first occurrence of a value

list, tuple, range

sequence.index(value)

count()

Returns the number of occurrences of a value

list, tuple

sequence.count(value)

in

Checks if an element is in the sequence

list, tuple, range

value in sequence

+ (concatenation)

Concatenates two sequences

list, tuple

sequence1 + sequence2

* (repetition)

Repeats the sequence a given number of times

list, tuple

sequence * n

slice

Retrieves a slice (subsequence)

list, tuple, range

sequence[start:end:step]

Here’s the updated table of common sequence functions and methods in Python, including the list, tuple, and range data types. String-specific methods are excluded.

List-Specific Methods

Method

Description

Applies to

Syntax

append()

Adds an element to the end of the list

list

list.append(element)

extend()

Extends the list by appending elements from another list or iterable

list

list.extend(iterable)

insert()

Inserts an element at a specific index

list

list.insert(index, element)

remove()

Removes the first occurrence of a value

list

list.remove(value)

pop()

Removes and returns an element at a specific index

list

list.pop(index)

clear()

Removes all elements from the list

list

list.clear()

reverse()

Reverses the elements of the list in place

list

list.reverse()

sort()

Sorts the list in place

list

list.sort()

Tuple-Specific Operations

  • Tuples are immutable, so methods that modify elements (like append() or remove()) do not apply to tuples. However, tuples support most common sequence operations, including:some text
    • count()
    • index()
    • Slicing, concatenation, and repetition

Range-Specific Properties

Property

Description

Applies to

Syntax

start, stop, step

Properties that define the range’s behavior

range

range(start, stop, step)

  • A range is an immutable sequence of numbers commonly used in loops. It does not support methods that modify content but can be sliced and iterated over.

Mapping

In Python, a mapping type refers to a data structure that stores data in key-value pairs. A mapping type allows you to associate a unique key with a value, enabling fast lookups, insertions, and deletions based on the key. The only built-in mapping data type in Python is dict(dictionary).

Python view dictionary Keys and Values - Data Science Parichay

Dictionary

  • Stores data as key-value pairs 
  • Each key in the dictionary must be unique
  • Dictionaries themselves are mutable. You can change, add or remove key-value pairs after creation.
  • Order: Prior to Python 3.7, dictionaries were unordered. This means that the insertion order of keys is not guaranteed. From Python 3.7 onwards, dictionaries maintain the order of insertion.
  • Syntaxsome text
    • Creating a dictionary: 

my_dict = {

    "name": "Alice",

    "age": 25,

    "city": "New York"

}

  • Accessing values by key:

print(my_dict["name"])  # Output: Alice

  • Modifying a value:
    my_dict["age"] = 26

Set

A set in python is an unordered collection of unique elements. 

  • A set does not maintain an order of elements. You cannot rely on the order in which items are inserted
  • Unique elements:  Sets automatically eliminate duplicates
  • Sets are mutable. You can add and remove elements after a set is created
  • No indexing/slicing: Because sets are unordered, they do not support indexing or slicing like lists or tuples.
  • Only immutable items can be added to a set
  • Syntax: A set can be created in 2 wayssome text
    • By using the set() function with lists or tuples
      thisset = set(("apple", "banana", "cherry")) # from a Tuple

thisset = set(["apple", "banana", "cherry"]) # from a List

  • By declaring a set
    thisset = {"apple", "banana", "cherry"}
    print(thisset)

Set Syntax and Methods

Method/Operation

Description

Syntax

set()

Creates a new set

set(iterable)

add()

Adds an element to the set

set.add(element)

clear()

Removes all elements from the set

set.clear()

copy()

Returns a shallow copy of the set

set.copy()

difference()

Returns a new set with elements in the set that are not in the others

set.difference(*others)

difference_update()

Removes elements found in another set from the set

set.difference_update(*others)

discard()

Removes an element from the set if it is present

set.discard(element)

intersection()

Returns a new set with elements common to the set and others

set.intersection(*others)

intersection_update()

Updates the set with elements common to the set and others

set.intersection_update(*others)

isdisjoint()

Returns True if the set has no elements in common with another set

set.isdisjoint(other)

issubset()

Returns True if the set is a subset of another set

set.issubset(other)

issuperset()

Returns True if the set is a superset of another set

set.issuperset(other)

pop()

Removes and returns an arbitrary element from the set

set.pop()

remove()

Removes an element from the set; raises a KeyError if the element is not present

set.remove(element)

union()

Returns a new set with all elements from the set and all others

set.union(*others)

update()

Updates the set with elements from another set or iterable

set.update(*others)

Set Operations

Operation

Description

Syntax

` (Union)

Combines elements from both sets

Set1 ` Set2

& (Intersection)

Gets common elements from both sets

set1 & set2

- (Difference)

Gets elements in the first set but not in the second

set1 - set2

^ (Symmetric Difference)

Gets elements in either set but not both

set1 ^ set2

Frozenset 

The frozenset data type is simply an immutable version of the set data type. This means you cannot add, remove or edit elements after creating a frozenset data type.

Binary

Think of binary data as a way of representing information using only two symbols: 0 and 1. Computers use this binary system because they work with electrical signals that can be either on (1) or off (0). The 3 built-in binary data types in Python are:

  • Bytes
  • Bytearray
  • Memoryview

Let us look into these in detail:

Bytes 

Bytes are a sequence of numbers where each number is between 0 and 255. Because there are 8 bits in a byte, you can have 2^8 or 256 different combinations of 0s and 1s. Each number is like a tiny piece of information. Bytes are used for handling binary data, such as reading/writing files, network communication, or encoding/decoding data. Bytes are immutable.

my_bytes = b'hello'  # Creates a sequence of bytes that represents the word 'hello'

print(my_bytes)      # Output: b'hello'

Bytearray

Bytearrays are similar to bytes, but are mutable. We use this to modify binary data in place, such as when processing binary files or network protocols.

my_bytearray = bytearray(b'hello')  # Create a mutable sequence of bytes

my_bytearray[0] = 72  # Change the first byte

print(my_bytearray)  # Output: bytearray(b'Hello')

Memoryview

A memoryview type to create a “view” of memory containing binary data. It doesn’t store the data itself but provides a view into the memory where the data is stored. These are handy for efficiently manipulating large amounts of data without copying it. Memory views are typically used in advanced scenarios where direct memory manipulation is required, such as in high-performance applications.

data = bytearray(b'hello')

view = memoryview(data)  # Create a view of the data

print(view[0])  # Output: 104 (ASCII value for 'h')

view[0] = 72  # Modify the first byte through the view

print(data)    # Output: bytearray(b'Hello')

None 

None is a special constant in Python that represents the absence of a value or a null value.

The none data type is a singleton. That means there is only one possible instance of None in a python program.

Checking a data type

Checking data types in Python is essential for ensuring that variables and values are of the expected type before performing operations on them. In Python, the type() function is used, which outputs the data type of a variable.

x = 10

print(type(x))  # Output: <class 'int'>

y = "Hello"

print(type(y))  # Output: <class 'str'>

Conclusion

So there you have it! Python's data types might seem like a lot to take in at first, but they're pretty straightforward once you get the hang of them. From the basic types like integers and strings to more advanced ones like bytes, bytearrays and memoryviews, each type has its own special role.

Remember, choosing the right data type is crucial for writing efficient and bug-free code. Whether you're dealing with numbers, text, collections of items, or even binary data, Python has got you covered with a rich set of tools to make your life easier.

So, next time you're coding, take a moment to think about what kind of data you're working with and pick the type that fits best. It’ll make your code cleaner, faster, and just plain better. If you’re interested in learning Python, check out our free python course.

Resources:
Python data types – Official documentation 

Understanding data types in Python – A comprehensive guide

SIMILAR BLOGS

Interested in Writing for Us?

Share your expertise, inspire others, and join a community of passionate writers. Submit your articles on topics that matter to our readers. Gain visibility, grow your portfolio, and make an impact.
Join Now