Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Python’s data type determines what you can do with a value. Use lists for ordered, changeable sequences; tuples for sequences whose slots should stay fixed; sets for unique items; and dictionaries for looking up values by key. The examples below show how common built-in types behave and how to choose among them.

What a Python data type tells you

Python represents data as objects, and every object has an identity, a type, and a value. Its type determines which operations it supports. For example, a string supports text operations, while a list supports operations that change its contents. See the Python data model.

These are common built-in types, not a complete list of every type available in Python:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Numbers: int, float, complex, and bool.
  • Text and sequences: str, list, tuple, and range.
  • Binary data: bytes and bytearray.
  • Collections: set, frozenset, and dict.

The built-in types reference describes their behavior and supported operations in detail: Python built-in types.

Common Python types in code

These assignments create values of several everyday types:

count = 12                 # int
price = 3.5                # float
active = True              # bool
name = "Ada"               # str
scores = [8, 9, 10]         # list
point = (2, 5)              # tuple
unique_tags = {"python", "beginner"}  # set
profile = {"name": "Ada", "active": True}  # dict
empty_set = set()          # {} would instead be an empty dict

Python’s three built-in numeric types are integers, floating-point numbers, and complex numbers. Integers have unlimited precision; floats are floating-point values; and complex numbers have real and imaginary components. bool is a subtype of int, so True and False are Boolean values that also participate in the integer type relationship.

A str is an immutable sequence of text. Python does not have a separate character type: even a one-character value is a string. A range represents an arithmetic progression as a sequence rather than storing a list of every value.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

bytes holds immutable binary data, while bytearray holds mutable binary data. memoryview is another built-in for viewing binary data. These are especially relevant when working with files, encodings, or network data.

How mutability affects values

A mutable object can be changed after creation; an immutable object cannot. Lists and dictionaries are mutable. Strings, numbers, and tuples are immutable.

scores = [8, 9, 10]
scores.append(11)
print(scores)  # [8, 9, 10, 11]

name = "Ada"
name[0] = "E"  # TypeError: strings do not support item assignment

A tuple’s slots cannot be reassigned, but a tuple can contain a mutable object. In that case, the nested object can still change:

record = ("Ada", [8, 9])
record[1].append(10)
print(record)  # ('Ada', [8, 9, 10])

The tuple’s second slot still refers to the same list; it is the list’s contents that changed. The distinction matters when multiple parts of a program share references to an object.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

List, tuple, set, or dictionary: which should you use?

Choose based on how you organize and access the data, not on a claim that one collection is always best. The Python data structures tutorial covers these collections and their operations.

Type Organization Can contents change? Typical access Duplicates
list Ordered sequence Yes Index, slice, or iteration Allowed
tuple Ordered sequence No slot reassignment Index, slice, or iteration Allowed
set Unique elements A set can change; a frozenset cannot Membership and set operations; no indexing Not allowed
dict Key-to-value associations Yes Lookup by key Keys are unique

Use a list for an ordered sequence you will change

Lists preserve sequence order, allow repeated values, and support operations such as appending, removing, and replacing items. They are a natural fit for a queue of tasks, a set of scores where duplicates matter, or any sequence that needs editing.

Use a tuple when the sequence slots should stay fixed

Tuples are ordered sequences whose slots cannot be reassigned after creation. They can contain repeated values and can hold values of different types. A tuple is useful when the sequence’s structure should not change, such as a pair of coordinates.

Use a set for uniqueness and membership

A set contains unique elements and is unordered, so it does not support indexing. Use one when you need to remove duplicates, test membership, or compute a union, intersection, difference, or symmetric difference. Use set() to create an empty set: {} creates an empty dictionary instead.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a dictionary for key-to-value lookup

A dictionary is a mutable mapping of unique keys to values. Retrieve a value by its key, as in profile["name"], rather than by a numeric sequence position. Current Python dictionaries preserve insertion order, but their central purpose is association and lookup.

Dictionary keys must be hashable, which means they can be used as stable keys. Lists and dictionaries are mutable and cannot be used as keys. Values that compare equal can refer to the same dictionary entry: for example, 1 and 1.0 compare equal as numeric keys.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check a value’s type and use truth values

Call type(value) to see an object’s type. For a type check that should also accept subclasses, use isinstance(value, SomeType):

value = [1, 2, 3]
print(type(value))                 # <class 'list'>
print(isinstance(value, list))    # True

Python objects can be tested in conditions. By default, objects are true unless their class defines false behavior through __bool__() or a zero __len__(). Empty strings and collections are false, while non-empty ones are true. None is a distinct built-in singleton commonly used to represent the absence of a value.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Start practicing with the official tutorial

If you are new to Python, try these examples in the interpreter, then change the values and observe which operations work. The free data structures tutorial is a focused next step for lists, tuples, sets, and dictionaries.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.