Skip to main content
Home/Blog/Python/Python Data Types | Strings Numbers Collections
Python

Python Data Types | Strings Numbers Collections

Master strings, numbers, collections, and datetime for effective programming

Python Data Types | Strings Numbers Collections

Python's core data types break down into a few clear families: text (str), numbers (int, float, complex, bool), sequences (list, tuple, range), mappings (dict), sets (set, frozenset), binary data (bytes), and the special None value. Everything in Python is an object, and every value has exactly one type that you can inspect with type(value). The single most useful distinction to learn early is mutable vs. immutable: whether a value can be changed in place after it is created — because that one property governs how assignment, copying, and function arguments behave.

That is the tidy summary an AI Overview will hand you. What it will not show you is which type to reach for in a given situation, the Python-2-era myth that trips up beginners (there is no separate "long" type anymore), or how mutability quietly changes the behavior of your code. The diagram, the decision table, and the worked examples below cover exactly that.

The Python type map at a glance

Map of Python's built-in data types grouped by category and mutability Python built-in types organized into text, numbers, sequences, mappings, sets, and none, with mutable types highlighted in blue and immutable types in amber. Python Built-in Data Types Blue = mutable (changeable in place) · Amber = immutable (fixed once created) Text str "hello", 'P', """multi""" Numbers int float complex bool is a subclass of int Sequences list tuple range ordered, index access Mappings dict key → value pairs Sets set frozenset unique items, no order Binary & None bytes bytearray None raw bytes & absence of value Why mutability matters Mutable: two names can point to the SAME object — a change through one is seen by the other. Immutable: "changing" a value actually creates a new object, leaving the original untouched.

Strings and characters

Strings are ordered, immutable sequences of Unicode characters. Python has no separate character type the way C does — a single character is just a string of length one. You can write string literals with single quotes, double quotes, or triple quotes for multi-line text; they are equivalent.

name = "Python Programming"
character = "P"
print(name)          # Python Programming
print(character)     # P
print(len(name))     # 18
print(name.upper())  # PYTHON PROGRAMMING
print(name)          # Python Programming  <- original is unchanged

Because strings are immutable, methods like .upper() and .replace() return a new string. If you try to assign into a string by index (name[0] = "J") Python raises a TypeError. To build a string efficiently in a loop, gather the parts in a list and call "".join(parts) once at the end.

Numbers

Python has three numeric types plus a boolean type. Note the common myth: Python 3 does not have a separate "long integer" type. That was a Python 2 distinction. In Python 3 the single int type has arbitrary precision, so it grows to hold numbers as large as your memory allows — no overflow, no special syntax.

integer_num = 42
float_num = 3.14
complex_num = 2 + 3j
huge = 2 ** 1000        # a 302-digit int, no overflow

print(type(integer_num))  # <class 'int'>
print(type(float_num))    # <class 'float'>
print(type(complex_num))  # <class 'complex'>
print(True + True)        # 2  <- bool is a subclass of int
TypeExamplePrecision / notesReach for it when
int42, -7, 2**1000Arbitrary precision, never overflowsCounting, indexing, whole-number math
float3.14, -2.5, 1e964-bit IEEE 754, ~15–17 significant digitsMeasurements, ratios, scientific values
complex2 + 3jReal + imaginary partsSignal processing, engineering math
boolTrue, FalseSubclass of int (1 and 0)Flags, conditions, counting truthy items
DecimalDecimal("0.1")Exact base-10 (from the decimal module)Money and anything where 0.1 + 0.2 != 0.3 matters

For currency, avoid float — 0.1 + 0.2 famously evaluates to 0.30000000000000004. Use decimal.Decimal (or integer cents) instead.

Collections

Collections store multiple items. The four built-in workhorses differ along three axes: ordered vs. unordered, mutable vs. immutable, and whether duplicates are allowed.

fruits = ["apple", "banana", "cherry"]              # list
person = {"name": "John", "age": 30, "city": "NYC"} # dict
coordinates = (10, 20)                               # tuple
unique_numbers = {1, 2, 3, 4, 5}                     # set

Which collection should I use?

CollectionSyntaxOrdered?Mutable?Duplicates?Use it when
list[1, 2, 3]YesYesYesA general-purpose, changeable sequence
tuple(1, 2, 3)YesNoYesA fixed record, or a key in a dict/set
dict{"k": "v"}Yes (3.7+)YesKeys uniqueLookups by name/key; structured records
set{1, 2, 3}NoYesNoDeduplication and fast membership tests

A quick rule of thumb: if you need to look things up by name, use a dict; if you need fast "is this in here?" checks or to remove duplicates, use a set; if order and mutation matter, use a list; if the data should never change, use a tuple. Dicts and sets rely on hashing, which is why their keys/members must themselves be immutable (you can use a tuple as a dict key, but not a list).

Date and time

Dates and times get their own types from the datetime module. You can store a date as a string, but a real datetime object gives you arithmetic, comparison, formatting, and timezone handling for free.

from datetime import datetime, timedelta

now = datetime.now()
print(now)                                  # 2026-07-18 14:05:32.115003

tomorrow = now + timedelta(days=1)          # date arithmetic
print(tomorrow)

formatted = now.strftime("%Y-%m-%d %H:%M:%S")  # format to string
print(formatted)                            # 2026-07-18 14:05:32

parsed = datetime.strptime("2026-07-18", "%Y-%m-%d")  # string back to datetime
print(parsed.year)                          # 2026

The advantage is concrete: with real datetime objects you can subtract two dates to get a timedelta, sort a list of events correctly, and convert to and from strings deliberately with strftime/strptime. Two dates stored as plain strings cannot be subtracted or reliably compared without converting them first.

Inspecting and converting types

Every value carries its type. Use type() to see it and isinstance() to test it — isinstance is preferred in real code because it also matches subclasses.

value = 42
print(type(value))              # <class 'int'>
print(isinstance(value, int))   # True

# Explicit conversion ("casting")
print(int("100"))     # 100    (str -> int)
print(str(3.14))      # '3.14' (float -> str)
print(float("2.5"))   # 2.5    (str -> float)
print(list("abc"))    # ['a', 'b', 'c']
print(set([1, 1, 2])) # {1, 2}  (dedupe)

Python is dynamically typed (a variable can be rebound to any type) but strongly typed (it will not silently add a string to an integer — "3" + 5 raises a TypeError). Knowing which built-in type you are holding, and whether it is mutable, is most of what you need to write correct, predictable Python.

Frequently Asked Questions

Find answers to common questions

Python provides built-in functions for type conversion: str() converts to strings and int() converts to integers. To convert integers to strings, use str(number), which is useful for concatenation or formatted output. To convert strings to integers, use int(string_number), but ensure the string represents a valid integer or a ValueError will be raised. Handle conversion errors with try-except blocks to gracefully manage invalid input. Best practices include: always validating input before conversion (use regular expressions to check for numeric characters); using try-except blocks when converting user input; avoiding implicit conversions between incompatible types, as Python requires explicit conversions to prevent unexpected results. For example, in a web application receiving age input as a string, wrap the conversion in a try-except block: try: age = int(age_input) to store in the database, except ValueError: handle the invalid input appropriately. This approach ensures robust applications with proper error handling and type management.

Lists and dictionaries are fundamental Python collection types serving different purposes. Lists are ordered collections defined with square brackets, indexed by position starting from zero, ideal for maintaining sequences where order matters (user inputs, task queues) and supporting iteration, sorting, and appending. Dictionaries are unordered collections of key-value pairs defined with curly braces, accessed using unique keys rather than indices, perfect for associating values with keys (user profiles, configurations, entity mappings). Key differences: lists maintain order while dictionaries preserve insertion order only from Python 3.7+; lists use numerical indexing while dictionaries use key-based access; dictionaries offer faster lookups using hash tables compared to list searching. Use lists when you need sequential access, slicing, or aggregating items without enforcing uniqueness. Use dictionaries when you need fast key-based access and structured data storage with natural mappings. For example, track user activities using a list for action timestamps and a dictionary mapping user IDs to activity data. Choose the right type based on data nature: lists for order, dictionaries for quick lookups.

Python's datetime module provides robust tools for creating, manipulating, and formatting datetime objects. Create datetime objects using datetime.now() for current time or datetime(year, month, day, hour, minute) for specific dates. Format datetime objects into readable strings using strftime() with format codes like %Y (year), %m (month), %d (day), and %H (hour). Perform arithmetic operations using timedelta objects: add or subtract time periods with now + timedelta(days=10) or now - timedelta(days=5). Calculate differences between datetime objects, which returns a timedelta object with accessible attributes like days. Best practices include: always using datetime objects instead of strings for date manipulation to leverage built-in functionality; handling timezones with the pytz library when working across multiple zones (timezone.localize(now)); implementing error handling with try-except blocks when parsing strings to catch ValueError exceptions; and storing formatted timestamps in databases using strftime(). Mastering datetime manipulation is critical for applications requiring precise date and time handling, ensuring accurate computations and proper formatting throughout your code.

Automate Your IT Operations

Leverage automation to improve efficiency, reduce errors, and free up your team for strategic work.