Updated on Jul 20, 2025

Python data types: A visual guide for beginners

#python #beginner #data types
Image for Python data types: A visual guide for beginners

Python comes with nine core data types that cover almost everything you’ll need. Choosing the right one makes your code clearer, faster, and safer. This guide shows how each type works, when to use it, and what to watch out for. For a quick overview, also check our basics guide.

Diagram of Python built-in data type categories and mutability.

1. Numbers – int, float, complex

Python has three types for working with numbers, each designed for different situations.

Basic examples first

# Integers - whole numbers
age = 25
score = 100
negative = -10

# Floats - numbers with decimals
price = 19.99
temperature = 98.6
pi = 3.14159

# Complex - numbers with real and imaginary parts (advanced math)
z = 2 + 3j  # j represents the imaginary unit in Python
TypeTypical literalKey traitsWhen to useHandy methods/ops
int42, 0xFFWhole numbers that can be as large as your memory allows; supports binary 0b, octal 0o, hex 0x literalsCounting, indexing, storing whole quantitiesbit_length(), to_bytes()
float3.14, 1.2e3Numbers with decimal points; may have small rounding errors in calculationsMeasurements, averages, scientific calculations.is_integer(), .hex(), math module
complex2+3j, complex(a,b)Numbers with real and imaginary parts (used in advanced math); .real, .imag, .conjugate()Advanced math, engineering, signal processingAll arithmetic, abs() gives magnitude

More examples

# Working with integers
items = 5
total_items = items * 3  # 15

# Working with floats
radius = 2.5
area = 3.14159 * radius * radius  # 19.634...

# Be careful with float precision
result = 0.1 + 0.2  # 0.30000000000000004 (not exactly 0.3!)

# Complex numbers (you probably won't need these as a beginner)
z = complex(2, 3)     # Same as 2+3j
magnitude = abs(z)    # 3.605...

2. Strings – str

Strings hold text and are one of the most important types you’ll use. For more detailed string operations, see our manipulating strings guide.

Basic examples

# Creating strings
name = "Alice"
message = 'Hello world'
long_text = """This is a
multi-line string"""

# Basic operations
greeting = "Hello, " + name  # "Hello, Alice"
repeated = "Ha" * 3          # "HaHaHa"
length = len(message)        # 11

Key characteristics:

  • Immutable: once created, you can’t change them (but you can create new ones)
  • Use single ' or double " quotes
  • Triple quotes """ for multi-line text
  • Support Unicode (emojis, international characters)

Common string operations

text = "  Python Programming  "

# Useful methods
clean_text = text.strip()           # "Python Programming"
words = clean_text.split()          # ["Python", "Programming"]
joined = "-".join(words)            # "Python-Programming"
upper_text = clean_text.upper()     # "PYTHON PROGRAMMING"
replaced = clean_text.replace("Python", "Java")  # "Java Programming"

# Checking content
starts_with_p = clean_text.startswith("Python")  # True
has_gram = "gram" in clean_text                   # True

# Modern formatting (recommended)
age = 25
formatted = f"I am {age} years old"  # "I am 25 years old"

3. Booleans – bool

Booleans represent True or False values - essential for making decisions in your code. Learn more about the bool() function.

Basic examples

# Simple boolean values
is_student = True
is_graduated = False

# Boolean operations
has_degree = is_student or is_graduated  # True
ready_to_work = is_graduated and not is_student  # False

Truthiness: Python treats many values as True or False in conditions:

# These are "truthy" (act like True)
if "hello":     # non-empty strings
if [1, 2, 3]:   # non-empty lists
if 42:          # non-zero numbers

# These are "falsy" (act like False)
if "":          # empty string
if []:          # empty list
if 0:           # zero
if None:        # None value

Practical examples

items = []
if items:
    print("We have items!")
else:
    print("No items found")  # This will print

score = 85
passed = score >= 60  # True

4. None Type – NoneType

None represents “nothing” or “no value” - you’ll see it everywhere in Python.

Basic examples

# Variables that don't have a value yet
result = None
user_input = None

# Functions return None by default
def greet(name):
    print(f"Hello, {name}")

return_value = greet("Alice")  # return_value is None

# Checking for None
if result is None:
    print("No result yet")

# Common pattern: optional values
def find_user(username):
    # ... search logic ...
    if user_found:
        return user_data
    else:
        return None  # Nothing found

Important: Always use is and is not when comparing with None, not ==:

# Correct
if value is None:
    # do something

# Incorrect (but works)
if value == None:
    # do something

5. Lists – list

Lists store multiple items in order and let you change them after creation. For comprehensive list operations, see our lists and tuples guide.

Basic examples

# Creating lists
fruits = ["apple", "banana", "orange"]
numbers = [1, 2, 3, 4, 5]
mixed = ["hello", 42, True, None]  # Lists can hold different types
empty = []

# Accessing items (starts at index 0)
first_fruit = fruits[0]      # "apple"
last_fruit = fruits[-1]      # "orange"

Common operations

shopping_list = ["milk", "bread"]

# Adding items
shopping_list.append("eggs")         # ["milk", "bread", "eggs"]
shopping_list.insert(0, "butter")    # ["butter", "milk", "bread", "eggs"]
shopping_list.extend(["cheese", "ham"])  # Add multiple items

# Removing items
shopping_list.remove("milk")         # Remove first occurrence
last_item = shopping_list.pop()      # Remove and return last item
first_item = shopping_list.pop(0)    # Remove and return first item

# Useful operations
length = len(shopping_list)
has_bread = "bread" in shopping_list

When to use lists

  • When you need ordered data that might change
  • Building collections item by item
  • When you need to access items by position
# Good use cases
grades = [85, 92, 78, 96]
todo_items = ["wash dishes", "walk dog", "study Python"]

# Processing lists
total = sum(grades)
average = total / len(grades)

for item in todo_items:
    print(f"Task: {item}")

6. Dictionaries – dict

Dictionaries store data as key-value pairs, like a real dictionary where you look up words (keys) to find definitions (values). For comprehensive dictionary operations, see our dictionaries guide.

Basic examples

# Creating dictionaries
person = {"name": "Alice", "age": 30, "city": "New York"}
grades = {"math": 85, "english": 92, "science": 78}
empty = {}

# Accessing values
name = person["name"]        # "Alice"
age = person.get("age")      # 30 (safer way)
height = person.get("height", "unknown")  # "unknown" if key doesn't exist

Common operations

student = {"name": "Bob", "grade": 85}

# Adding/updating values
student["age"] = 20           # Add new key-value pair
student["grade"] = 90         # Update existing value

# Useful methods
keys = student.keys()         # dict_keys(['name', 'grade', 'age'])
values = student.values()     # dict_values(['Bob', 90, 20])
items = student.items()       # dict_items([('name', 'Bob'), ...])

# Checking for keys
if "name" in student:
    print(f"Student name: {student['name']}")

# Removing items
age = student.pop("age")      # Remove and return value
student.pop("height", None)   # Safe removal (no error if key missing)

When to use dictionaries

  • When you need to look up values by a unique identifier
  • Storing structured data (like records)
  • Counting things
  • Caching/memoization
# Good use cases
inventory = {"apples": 50, "bananas": 30, "oranges": 25}
user_profile = {
    "username": "alice123",
    "email": "alice@example.com",
    "is_premium": True
}

# Counting example
text = "hello world"
char_count = {}
for char in text:
    char_count[char] = char_count.get(char, 0) + 1
# Result: {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1}

7. Tuples – tuple

Tuples are like lists but can’t be changed after creation. Think of them as “locked” lists. Learn more about tuples in our lists and tuples guide.

Basic examples

# Creating tuples
coordinates = (10, 20)
rgb_color = (255, 0, 128)
single_item = (42,)    # Note the comma for single-item tuples
empty = ()

# Parentheses are often optional
point = 5, 10          # Same as (5, 10)
name_age = "Alice", 25 # Same as ("Alice", 25)

# Accessing items (same as lists)
x = coordinates[0]     # 10
y = coordinates[1]     # 20

Unpacking tuples

# Unpacking is very useful
point = (100, 200)
x, y = point          # x=100, y=200

# Swapping values
a = 5
b = 10
a, b = b, a           # Now a=10, b=5

# Function returning multiple values
def get_name_age():
    return "Bob", 25

name, age = get_name_age()

When to use tuples

  • When you have a fixed collection that won’t change
  • Returning multiple values from functions
  • As dictionary keys (since they’re immutable)
  • Representing coordinates, RGB values, etc.
# Good use cases
WINDOW_SIZE = (800, 600)        # Constants
DEFAULT_COLOR = (255, 255, 255) # RGB white

# Dictionary with tuple keys
locations = {
    (0, 0): "origin",
    (1, 1): "northeast",
    (-1, -1): "southwest"
}

8. Sets – set

Sets store unique items with no duplicates and no particular order. Great for membership testing and removing duplicates. For comprehensive set operations, see our sets guide.

Basic examples

# Creating sets
colors = {"red", "green", "blue"}
numbers = {1, 2, 3, 4, 5}
empty = set()  # Note: {} creates an empty dict, not set!

# From lists (removes duplicates)
mixed_list = [1, 2, 2, 3, 3, 3]
unique_numbers = set(mixed_list)  # {1, 2, 3}

Common operations

tags = {"python", "programming", "beginner"}

# Adding items
tags.add("tutorial")
tags.update(["coding", "learning"])  # Add multiple items

# Removing items
tags.remove("beginner")     # Error if item doesn't exist
tags.discard("advanced")    # No error if item doesn't exist

# Membership testing (very fast!)
if "python" in tags:
    print("This is about Python!")

# Set operations
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union = set1 | set2         # {1, 2, 3, 4, 5}
intersection = set1 & set2   # {3}
difference = set1 - set2     # {1, 2}

When to use sets

  • Removing duplicates from a collection
  • Fast membership testing
  • Mathematical set operations
  • Tracking unique visitors, IDs, etc.
# Good use cases
unique_visitors = set()
unique_visitors.add("user123")
unique_visitors.add("user456")
unique_visitors.add("user123")  # Won't add duplicate
print(len(unique_visitors))     # 2

# Remove duplicates from list
items = ["apple", "banana", "apple", "orange", "banana"]
unique_items = list(set(items))  # ['apple', 'banana', 'orange']

Picking the right type – real-world tips

ScenarioBest typeRationale
Storing user’s ageintWhole numbers, no decimals needed
Product pricefloatNeeds decimal places
User login statusboolSimple true/false value
User’s middle name (might not exist)str or NoneCould be absent
Shopping cart itemslistOrdered, can add/remove items
User profile datadictKey-value pairs (name, email, etc.)
GPS coordinatestupleFixed pair that won’t change
Unique product categoriessetNo duplicates, fast lookup

Cheat-sheet for mutability & hashability

TypeMutable?Hashable?Can be dict key?
int, float, complexNoYes
strNoYes
boolNoYes
NoneNoYes
tuple (with immutable items)NoYes
listYesNo
dictYesNo
setYesNo

Key takeaways for beginners

  1. Start simple: Use int for whole numbers, float for decimals, str for text, bool for true/false, and None for “no value.”

  2. Collections made easy: Use list when you need to change things, tuple when you don’t, dict for key-value pairs, and set for unique items.

  3. When in doubt: list and dict cover most collection needs for beginners.

  4. Practice with simple examples before moving to complex use cases.

  5. Remember: Python is forgiving – you can always convert between compatible types when needed.

Subscribe to pythoncheatsheet.org

Join 16,702+ Python developers in a two times a month and bullshit free publication , full of interesting, relevant links.