Python Data Types with Examples

Any value you write in Python-number, text, list, anything-has a type.

Data types basically tell Python: "What is this thing?"

  • Is it a number?
  • Is it text?
  • Is it a list of items?

If you understand data types, you'll make far fewer mistakes when writing code.

What is a Data Type?

A data type classifies the kind of value a variable can hold. It tells the Python interpreter how to store and manage the data. This means you can perform mathematical operations on numbers but not on text, which prevents unexpected errors. For example, Python recognizes 42 as a number and "Python" as text.

Want to master Python fundamentals? This lesson is part of our Free Python course covering variables, data types, functions, and more.

Explore this free Python course →

Python's Built-in Data Types

Python provides several standard data types ready for you to use. These types fall into several categories:

1. Text Type: str

The string (str) data type stores sequences of characters as text. You create a string by enclosing characters in single (') or double (") quotes.

Example: Defining a String

Define a variable my_language as a string and print it.

my_language = "Python" my_text_number = "10" print(my_language) print(my_text_number)
my_language = "Python"
print(my_language)
Your output will appear here...

2. Numeric Types: int, float, complex

Numeric types represent different kinds of numbers. Python supports three distinct types for various mathematical needs.

  • Integer (int): Whole numbers without decimals.
  • Float (float): Numbers that contain one or more decimals.
  • Complex (complex): Numbers with an imaginary part, written with a "j".

Example: Define examples of each numeric type.

my_integer = 100 my_float = 20.5 my_complex = 1j print(my_integer + my_float)
my_integer = 100
my_float = 20.5
my_complex = 1j

print(my_integer + my_float)
Your output will appear here...

3. Sequence Types: list, tuple, range

Sequence types store ordered collections of items. You can access any item by its position (index) in the sequence.

  • List (list): An ordered and changeable collection.
  • Tuple (tuple): An ordered and unchangeable collection.
  • Range (range): An immutable sequence of numbers, often used in loops.

Example: Sequence Types

Define a list and access its first element.

my_list = ["apple", "banana", "cherry"] my_tuple = ("apple", "banana", "cherry") my_range = range(3) print(my_list[0])
my_list = ["apple", "banana", "cherry"]
my_tuple = ("apple", "banana", "cherry")
my_range = range(3)

print(my_list[0])
Your output will appear here...

4. Mapping Type: dict

A dictionary (dict) stores data in key-value pairs. Dictionaries are ordered (since Python 3.7), changeable, and do not allow duplicate keys.

Example: Defining a Dictionary

Access a dictionary's value using its key.

my_car = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(my_car["model"])
my_car = {
    "brand": "Ford",
    "model": "Mustang",
    "year": 1964
}

print(my_car["model"])
Your output will appear here...

5. Set Types: set, frozenset

A set is a collection of unique items. Sets ensure each item appears only once in the collection.

  • Set (set): A mutable and unordered collection of unique items.
  • Frozenset (frozenset): An immutable and unordered collection of unique items.

Example: Working with Sets and Frozensets

Define a set with a duplicate item, and define a frozenset.

# A standard set (mutable) my_set = {"apple", "banana", "cherry", "apple"} # A frozen set (immutable) my_frozenset = frozenset(["apple", "banana", "cherry"]) print(my_set) print(my_frozenset)
# A standard set (mutable)
my_set = {"apple", "banana", "cherry", "apple"}

# A frozen set (immutable)
my_frozenset = frozenset(["apple", "banana", "cherry"])

print(my_set)
print(my_frozenset)
Your output will appear here...
Analysis: Look at your output for my_set. Notice that "apple" only appears once? Sets automatically detect and remove duplicates for you. Also, the order of items might change every time you run the code because sets are unordered.

6. Boolean Type: bool

The boolean (bool) type represents one of two values: True or False. It often results from comparisons and helps control your program's flow.

Example: Boolean Values

Use a comparison operator to produce a boolean value.

x = 10 y = 5 is_greater = x > y print(is_greater)
x = 10
y = 5

is_greater = x > y
print(is_greater)
Your output will appear here...

7. None Type: NoneType

The NoneType has only one value: None. It represents the absence of a value, such as when a function does not explicitly return anything.

Example: None Value

Assign None to a variable.

my_variable = None print(my_variable)
my_variable = None

print(my_variable)
Your output will appear here...

You're making great progress! Ready to tackle more Python challenges with real-world Exercise?

Python Exercise

How to Check a Variable's Data Type

You can find the data type of any object with the built-in type() function. This function is essential for debugging and understanding how Python manages your data.

  1. Define a variable with a value.
  2. Pass the variable to the type() function.
  3. Use print() to display the result.

Exercise: Using the type() function

In the code editor below, use the type() function to determine the data type of the variable price and print the result.

price = 19.99 # TODO: Print the data type of the 'price' variable # print( ... )
price = 19.99

print(type(price))
Your output will appear here...

Python Uses Dynamic Typing

Python is a dynamically typed language. This means you don't have to declare a variable's data type when you create it. The interpreter determines the type automatically at runtime based on the assigned value. This gives you flexibility, as you can reassign a variable to a different data type without causing an error.

Example: Dynamic Reassignment

Observe how the same variable my_var changes its type from an integer to a string when a new value is assigned.

my_var = 10 # Integer print("First Type:", type(my_var)) my_var = "Text" # String print("Second Type:", type(my_var))
my_var = 10
print("First Type:", type(my_var))

my_var = "Text"
print("Second Type:", type(my_var))
Your output will appear here...
Analysis: As you can see, the data type of my_var changed instantly when we assigned a new value. In many other languages (like Java or C++), this would cause a crash, but Python handles it automatically.

Setting a Specific Data Type

If you need to set a specific data type for a variable, you can use constructor functions.

Data Type Constructor Function Example
str str() x = str("Hello World")
int int() x = int(20)
float float() x = float(20.5)
complex complex() x = complex(1j)
list list() x = list(("apple", "banana", "cherry"))
tuple tuple() x = tuple(("apple", "banana", "cherry"))
range range() x = range(6)
dict dict() x = dict(name="John", age=36)
set set() x = set(("apple", "banana", "cherry"))
frozenset frozenset() x = frozenset(("apple", "banana", "cherry"))
bool bool() x = bool(5)
bytes bytes() x = bytes(5)
bytearray bytearray() x = bytearray(5)
memoryview memoryview() x = memoryview(bytes(5))

Final Challenge: Mixed Data Type Identification

Identify the correct data type for each variable below, covering the main categories (Numeric, Sequence, Mapping, Boolean, Text).

Your Task:

  1. Use the type() function to determine the type of a, b, c, d, and e.
  2. Print the type for each variable.
a = 42 b = [1, 2, 3] c = "Data" d = {"key": 1} e = 10.5 # TODO: Find and print the type of a (int) # TODO: Find and print the type of b (list) # TODO: Find and print the type of c (str) # TODO: Find and print the type of d (dict) # TODO: Find and print the type of e (float)
a = 42
b = [1, 2, 3]
c = "Data"
d = {"key": 1}
e = 10.5

print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
Your output will appear here...
🏆

Lesson Completed

You have successfully learned about Python's built-in data types, how to check them, and how dynamic typing works.

📘

Full Python Course

Master Python with 11+ hours of content, 50+ exercises, and real-world projects.

Enroll Now
📝

Next Lesson

Continue with the next lesson on Python Operators.

Next Lesson ->
Scroll to Top