Table of Contents

What Will You Learn
You will learn about the built-in data types in Python and how to work with them. The course covers numbers, strings, lists, tuples, sets, and dictionaries. You will understand the characteristics of each type, when and why to use them, and how to determine a variable's type using the built-in function type().


Data types are the foundation of any program. Without understanding them, it's impossible to correctly store, process, and transmit information. Programming languages use a strict yet flexible type system that allows efficient work with numbers, strings, collections, logic, and byte streams. If you're just starting out in development, exploring data types should be an essential part of your foundational training. This topic is considered crucial for a confident start.

What are the data types in Python?

In programming languages, variables don't exist independently—they always store values of a specific type. The data type determines how the information is interpreted, what operations are permissible, and how it interacts with other objects.

For example, numbers can undergo arithmetic operations, strings can be concatenated, and boolean values can be compared. If you don't understand the data type a variable is working with, it's easy to make mistakes, like trying to add a string and a number. That's why studying data types is a mandatory part of learning syntax.

Python uses dynamic typing, which means you don't need to explicitly specify a variable's type when creating it. The interpreter determines it based on the assigned value:

 
    x = 10 # int 
    name = "Bob" # str 
    flag = True # bool 

This simplifies code and makes it more concise, but it requires the programmer to understand the type system.

How many data types in Python?

Built-in data types are divided into several categories. They cover all the main cases: numbers, texts, boolean values, sequences, mappings, sets, and working with bytes. Each of these families has subtypes designed for specific tasks.

Below is a structured table with key data types and brief explanations for each.

Categories of built-in data types

Category Data Type Purpose
Numeric int Integer numbers (e.g., 5, -10, 0)
float Floating-point numbers (e.g., 3.14)
complex Complex numbers (e.g., 2+3j)
Sequences str Character strings
list Lists — mutable ordered collections
tuple Tuples — immutable ordered collections
Mappings dict Dictionaries — collections of key–value pairs
Sets set Unordered unique values
frozenset Immutable sets
Boolean bool Boolean values True and False
Byte bytes Sequence of bytes (immutable)
bytearray Mutable sequence of bytes
memoryview Access to memory without copying data

What are the different data types in Python?

Python employs a well-defined type system. Each data type is designed for a specific purpose: storing numbers, strings, collections of values, logical states, or byte information. To write robust code, it's essential to understand what data types exist, how they function, and how they differ from one another.

All built-in types can be grouped by their purpose. Below, we examine each category individually, from numerical values to byte-level memory operations.

Numeric

Numeric types are used to represent numbers. This category includes int (integers), float (floating-point numbers), and complex (complex numbers). These types support mathematical operations: addition, subtraction, division, exponentiation, and more. Their main differences lie in precision and how values are stored in memory.

Learn More →

Sequence Type

Sequences are ordered collections of elements. They include str (strings), list (lists), and tuple (tuples). All types support indexing, slicing, and iteration in loops. Differences: strings and tuples are immutable, while lists are mutable, affecting their usage possibilities.

Learn More →

Mapping Type

The only built-in mapping type is dict. It stores key–value pairs, where data is accessed not by index but by a unique key. Dictionaries are used for fast lookups, configurations, and storing structured information. A distinctive feature is the ability to work with associative structures and instant access to values by key.

Learn More →

Boolean

The bool type contains only two values: True and False. It's used in conditions, checks, logical expressions, and code branching. Despite its simplicity, this type plays a key role in controlling program logic. Any object in Python can be converted to a boolean value—this is the basis of logical interpretation.

Learn More →

Set Type

Sets (set and frozenset) are collections of unique and unordered elements. They don't allow duplicates and are used for set operations: union, intersection, difference. set is mutable, frozenset is immutable. Their distinguishing feature is automatic elimination of duplicates and high speed of element operations.

Learn More →

Binary Types

Binary types (bytes, bytearray, memoryview) are intended for working with low-level data—bytes. bytes is an immutable type, bytearray allows editing content, memoryview provides access to data in memory without copying. These types are often used when reading files, transmitting over networks, and processing media data. The difference from other collections is storing information in byte form.

Learn More →

What are mutable and immutable data types in Python?

Each data type is either mutable or immutable. This determines whether the contents of an object can be changed after its creation. This behavior is critical when working with lists, dictionaries, sets, and especially when passing objects to functions.

Immutable types:

  • A new object is created upon modification.
  • Safe to use as dictionary keys.
  • Attempting to change the value raises an error.

Mutable types:

  • Allow modifying the object "in place".
  • Often used to store collections.
  • Require caution when passed by reference.

Examples of mutable and immutable types:

Mutable:

  • list — elements can be added, removed, modified.
  • dict — key–value pairs can be added and modified.
  • set — elements can be added and removed.
  • bytearray — individual bytes can be changed.

Immutable:

  • int, float, complex — numbers cannot be changed.
  • str — any changes create a new string.
  • tuple — fixed content.
  • frozenset — set without the possibility of changes.
  • bytes — similar to a string, but for bytes.

Understanding the differences between mutable and immutable types is especially important when writing functions and working with collections. It affects performance, code logic, and prevents side effects.

Frequently Asked Questions

Which of the following data types is immutable in Python?

Immutable data types in Python are those whose values cannot be changed after creation. This means that any operation on such objects creates a new instance instead of modifying the original. These include:

  • int — integers;
  • float — floating-point numbers;
  • str — strings;
  • tuple — tuples;
  • frozenset — immutable sets;
  • bool — boolean values;
  • bytes — immutable byte sequences.

For example, attempting to change a character in a string will raise an error because strings are immutable structures. This behavior is essential to consider when passing such objects to functions and working with collections.

What are the two main data types in Python?

All built-in data types in Python can be broadly categorized into two main groups:

  1. Scalar types — these include int, float, bool, str, and complex. These types represent a single value.
  2. Container types — such as list, tuple, dict, set, bytes, etc. These hold collections of values or structured data.

This classification helps understand which types represent individual entities (like a number or a string) and which are capable of storing multiple values for operations like looping, filtering, sorting, and more.

What are the basic data types in Python?

Basic data types are the fundamental types you start with in Python programming. They are the building blocks for more complex structures. These include:

  • int — integer number;
  • float — floating-point number;
  • bool — boolean value (True, False);
  • str — string (text);
  • list — list of values;
  • dict — dictionary (key–value pairs);
  • tuple — tuple (immutable list);
  • set — set of unique values.

Mastering these types provides a strong foundation for writing real-world programs, from calculators and forms to web apps and parsers.

What is the difference between list and tuple in Python?

The main difference between list and tuple is mutability. list is a mutable type, meaning you can add, remove, or modify elements after creation. tuple is immutable: its contents cannot be changed once defined.

 
    a = [1, 2, 3] a[0] = 100 # allowed 
    b = (1, 2, 3) b[0] = 100 # ❌ TypeError

Lists are more commonly used when data needs to be modified. Tuples are preferable when immutability is important, such as using them as dictionary keys or set elements.

Can dictionary keys be mutable in Python?

No, dictionary keys (dict) must be immutable data types. This is due to Python’s use of hash functions for fast key access. If the object is mutable, its hash may change, making it impossible to retrieve the value.

Valid key types:

  • str
  • int
  • float
  • bool
  • tuple (if it doesn’t contain mutable elements)

Invalid key types:

  • list
  • dict
  • set

Example of an error:

 my_dict = {[1, 2]: "value"} # ❌ TypeError

Always use hashable, immutable objects as dictionary keys. This rule is essential when designing dictionaries and data structures.