#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
6 min read
•Question 2 of 41easy

Python Data Types

Understanding built-in data types in Python.

What You'll Learn

  • All built-in data types in Python
  • When to use each data type
  • Mutable vs immutable types
  • Type checking and conversion

Understanding Python Data Types

In Python, every value has a data type. Unlike languages like Java or C++, you don't need to declare the type explicitly - Python determines it automatically. This is called dynamic typing.

Understanding data types is crucial because:

  • Different types support different operations
  • Some types are mutable, others are immutable
  • Type choice affects performance and memory usage

Categories of Data Types

Python data types fall into these categories:

CategoryTypes
Numericint, float, complex
Sequencestr, list, tuple
Mappingdict
Setset, frozenset
Booleanbool
NoneNoneType

1. Numeric Types

Python has three numeric types for working with numbers.

Integer (int)

Whole numbers without decimal points. Python 3 integers have unlimited precision.

code.pyPython
age = 25
population = 7_900_000_000  # Underscores for readability
negative = -42
binary = 0b1010  # Binary: 10
hex_num = 0xFF   # Hexadecimal: 255

print(type(age))  # <class 'int'>

Float

Numbers with decimal points. Uses 64-bit double precision.

code.pyPython
price = 19.99
pi = 3.14159
scientific = 2.5e-3  # 0.0025

# Be aware of floating-point precision issues
print(0.1 + 0.2)  # 0.30000000000000004
print(0.1 + 0.2 == 0.3)  # False!

Complex

Numbers with real and imaginary parts.

code.pyPython
z = 3 + 4j
print(z.real)  # 3.0
print(z.imag)  # 4.0
print(abs(z))  # 5.0 (magnitude)

2. Sequence Types

Ordered collections of items.

String (str)

Immutable sequence of characters.

code.pyPython
name = "Python"
multiline = """This is
a multiline string"""

# Strings are immutable
# name[0] = "J"  # This would raise an error!

# String operations
print(name[0])      # 'P' (indexing)
print(name[1:4])    # 'yth' (slicing)
print(len(name))    # 6
print(name.upper()) # 'PYTHON'

List

Mutable, ordered collection. Most commonly used data structure.

code.pyPython
fruits = ["apple", "banana", "cherry"]
mixed = [1, "hello", 3.14, True]  # Can mix types

# Lists are mutable
fruits[0] = "apricot"
fruits.append("date")
fruits.remove("banana")

print(fruits)  # ['apricot', 'cherry', 'date']

Tuple

Immutable, ordered collection. Faster than lists.

code.pyPython
coordinates = (10, 20)
rgb = (255, 128, 0)
single = (42,)  # Single element needs comma

# Tuples are immutable
# coordinates[0] = 15  # This would raise an error!

# Use for fixed data
point = (x, y, z) = (1, 2, 3)  # Unpacking

3. Mapping Type

Dictionary (dict)

Key-value pairs. Keys must be immutable and unique.

code.pyPython
person = {
    "name": "John",
    "age": 30,
    "city": "NYC"
}

# Accessing values
print(person["name"])        # "John"
print(person.get("email"))   # None (safe access)

# Modifying
person["email"] = "john@email.com"
del person["city"]

# Dictionary methods
print(person.keys())    # dict_keys(['name', 'age', 'email'])
print(person.values())  # dict_values(['John', 30, 'john@email.com'])
print(person.items())   # dict_items([...])

4. Set Types

Unordered collections of unique elements.

Set

Mutable set with unique values.

code.pyPython
numbers = {1, 2, 3, 3, 4}  # Duplicates removed
print(numbers)  # {1, 2, 3, 4}

# Set operations
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b)  # Union: {1, 2, 3, 4, 5}
print(a & b)  # Intersection: {3}
print(a - b)  # Difference: {1, 2}

Frozenset

Immutable set. Can be used as dictionary keys.

code.pyPython
frozen = frozenset([1, 2, 3])
# frozen.add(4)  # Error! Frozensets are immutable

5. Boolean Type

Represents True or False.

code.pyPython
is_active = True
is_deleted = False

# Falsy values: None, 0, "", [], {}, False
# Everything else is truthy

print(bool(0))      # False
print(bool(""))     # False
print(bool([]))     # False
print(bool("Hi"))   # True
print(bool(42))     # True

6. None Type

Represents absence of value.

code.pyPython
result = None

def no_return():
    pass

print(no_return())  # None

Type Checking and Conversion

code.pyPython
# Check type
x = 42
print(type(x))              # <class 'int'>
print(isinstance(x, int))   # True

# Type conversion
num_str = "42"
num_int = int(num_str)      # String to int
num_float = float(num_str)  # String to float
back_str = str(num_int)     # Int to string

# Convert between collections
my_list = [1, 2, 3]
my_tuple = tuple(my_list)   # List to tuple
my_set = set(my_list)       # List to set

Mutable vs Immutable

MutableImmutable
listint, float, complex
dictstr
settuple, frozenset
bool, None

Interview Tip

When asked about Python data types:

  1. List all categories (numeric, sequence, mapping, set, boolean, none)
  2. Explain mutable vs immutable
  3. Mention that Python is dynamically typed
  4. Give examples of when to use each type