Python Data Types: A Comprehensive Guide
Table of Contents
1. [Introduction](#introduction) 2. [Numeric Types](#numeric-types) 3. [Text Type](#text-type) 4. [Boolean Type](#boolean-type) 5. [Sequence Types](#sequence-types) 6. [Mapping Type](#mapping-type) 7. [Set Types](#set-types) 8. [None Type](#none-type) 9. [Type Conversion](#type-conversion) 10. [Best Practices](#best-practices)Introduction
Python data types are fundamental building blocks that define the kind of data a variable can store and the operations that can be performed on that data. Understanding data types is crucial for effective Python programming as they determine memory allocation, available methods, and behavior of variables.
Python is a dynamically typed language, meaning you don't need to explicitly declare the type of a variable. The interpreter automatically determines the type based on the assigned value. However, understanding these types helps in writing efficient and error-free code.
Data Types Overview Table
| Category | Data Type | Description | Mutable | Example | |----------|-----------|-------------|---------|---------| | Numeric | int | Integer numbers | No | 42, -17, 0 | | Numeric | float | Floating-point numbers | No | 3.14, -2.5, 1.0 | | Numeric | complex | Complex numbers | No | 3+4j, 2-5j | | Text | str | String of characters | No | "Hello", 'Python' | | Boolean | bool | True or False values | No | True, False | | Sequence | list | Ordered collection | Yes | [1, 2, 3] | | Sequence | tuple | Ordered collection | No | (1, 2, 3) | | Sequence | range | Sequence of numbers | No | range(10) | | Mapping | dict | Key-value pairs | Yes | {"a": 1, "b": 2} | | Set | set | Unordered unique elements | Yes | {1, 2, 3} | | Set | frozenset | Immutable set | No | frozenset({1, 2, 3}) | | None | NoneType | Represents absence of value | No | None |
Numeric Types
Integer (int)
Integers are whole numbers without decimal points. Python 3 has unlimited precision for integers, meaning they can be as large as memory allows.
Key Characteristics: - Immutable - Unlimited precision - Support various number bases
Commands and Methods:
`python
Creating integers
age = 25 negative_num = -100 zero = 0Type checking
print(type(age)) #Number base conversions
binary_num = 0b1010 # Binary (10 in decimal) octal_num = 0o12 # Octal (10 in decimal) hex_num = 0xFF # Hexadecimal (255 in decimal)Built-in functions
print(bin(10)) # '0b1010' - convert to binary print(oct(10)) # '0o12' - convert to octal print(hex(255)) # '0xff' - convert to hexadecimal print(abs(-5)) # 5 - absolute value print(pow(2, 3)) # 8 - power function`Mathematical Operations:
| Operation | Symbol | Example | Result | |-----------|--------|---------|--------| | Addition | + | 5 + 3 | 8 | | Subtraction | - | 5 - 3 | 2 | | Multiplication | | 5 3 | 15 | | Division | / | 5 / 3 | 1.6666... | | Floor Division | // | 5 // 3 | 1 | | Modulus | % | 5 % 3 | 2 | | Exponentiation | | 5 3 | 125 |
Float (float)
Floating-point numbers represent real numbers with decimal points. They are implemented using double precision in C.
Key Characteristics: - Immutable - Limited precision (approximately 15-17 decimal digits) - Can represent very large and very small numbers
`python
Creating floats
price = 19.99 scientific = 1.5e-4 # Scientific notation (0.00015) infinity = float('inf') not_a_number = float('nan')Float methods
num = 3.14159 print(num.is_integer()) # False - check if float is integer print((4.0).is_integer()) # TruePrecision handling
import math print(math.isfinite(price)) # True print(math.isinf(infinity)) # True print(math.isnan(not_a_number)) # TrueRounding
print(round(3.14159, 2)) # 3.14 print(math.floor(3.7)) # 3 print(math.ceil(3.2)) # 4`Complex (complex)
Complex numbers have real and imaginary parts, useful in mathematical computations and signal processing.
`python
Creating complex numbers
z1 = 3 + 4j z2 = complex(2, -3) # 2 - 3j z3 = complex('5+2j')Accessing components
print(z1.real) # 3.0 - real part print(z1.imag) # 4.0 - imaginary part print(abs(z1)) # 5.0 - magnitude print(z1.conjugate()) # (3-4j) - complex conjugateOperations
print(z1 + z2) # (5+1j) print(z1 * z2) # (18-1j)`Text Type
String (str)
Strings are sequences of Unicode characters used to represent text data. They are immutable in Python.
Key Characteristics: - Immutable - Unicode support - Rich set of methods for manipulation
String Creation:
`python
Different ways to create strings
single_quote = 'Hello' double_quote = "World" triple_quote = """Multi-line string example""" raw_string = r"C:\new\path" # Raw string (ignores escape sequences) formatted = f"Value: {42}" # f-string formattingString concatenation
full_greeting = single_quote + " " + double_quote`Common String Methods:
| Method | Description | Example | Result | |--------|-------------|---------|--------| | len() | String length | len("Hello") | 5 | | upper() | Convert to uppercase | "hello".upper() | "HELLO" | | lower() | Convert to lowercase | "HELLO".lower() | "hello" | | strip() | Remove whitespace | " text ".strip() | "text" | | split() | Split into list | "a,b,c".split(",") | ['a', 'b', 'c'] | | replace() | Replace substring | "hello".replace("l", "x") | "hexxo" | | find() | Find substring index | "hello".find("ll") | 2 | | startswith() | Check prefix | "hello".startswith("he") | True | | endswith() | Check suffix | "hello".endswith("lo") | True | | isdigit() | Check if digits | "123".isdigit() | True |
String Formatting:
`python
Various formatting methods
name = "Alice" age = 30% formatting (old style)
message1 = "Name: %s, Age: %d" % (name, age)str.format() method
message2 = "Name: {}, Age: {}".format(name, age) message3 = "Name: {name}, Age: {age}".format(name=name, age=age)f-strings (Python 3.6+)
message4 = f"Name: {name}, Age: {age}" message5 = f"Next year: {age + 1}"Advanced f-string formatting
pi = 3.14159 print(f"Pi rounded: {pi:.2f}") # "Pi rounded: 3.14"`String Indexing and Slicing:
`python
text = "Python Programming"
Indexing (0-based)
print(text[0]) # 'P' - first character print(text[-1]) # 'g' - last character print(text[-2]) # 'n' - second to lastSlicing [start:end:step]
print(text[0:6]) # 'Python' - characters 0 to 5 print(text[7:]) # 'Programming' - from index 7 to end print(text[:6]) # 'Python' - from start to index 5 print(text[::2]) # 'Pto rgamn' - every second character print(text[::-1]) # 'gnimmargorP nohtyP' - reverse string`Boolean Type
Boolean (bool)
Boolean type represents truth values and is a subclass of integers where True equals 1 and False equals 0.
`python
Boolean values
is_active = True is_complete = FalseBoolean operations
print(True and False) # False print(True or False) # True print(not True) # FalseComparison operations return booleans
print(5 > 3) # True print(10 == 10) # True print("a" in "cat") # TrueTruthiness in Python
print(bool(1)) # True print(bool(0)) # False print(bool("")) # False - empty string print(bool("text")) # True - non-empty string print(bool([])) # False - empty list print(bool([1, 2])) # True - non-empty list`Truthy and Falsy Values:
| Type | Falsy Values | Truthy Values | |------|--------------|---------------| | Numbers | 0, 0.0, 0j | Any non-zero number | | Strings | "" (empty string) | Any non-empty string | | Collections | [], {}, () (empty) | Any non-empty collection | | None | None | N/A |
Sequence Types
List (list)
Lists are ordered, mutable collections that can store items of different types.
Key Characteristics: - Mutable (can be modified) - Ordered (maintains insertion order) - Allows duplicates - Dynamic size
`python
Creating lists
numbers = [1, 2, 3, 4, 5] mixed = [1, "hello", 3.14, True] nested = [[1, 2], [3, 4], [5, 6]] empty_list = [] from_range = list(range(5)) # [0, 1, 2, 3, 4]List operations
numbers.append(6) # Add to end numbers.insert(0, 0) # Insert at index numbers.remove(3) # Remove first occurrence popped = numbers.pop() # Remove and return last item numbers.extend([7, 8, 9]) # Add multiple items`List Methods:
| Method | Description | Example | Result | |--------|-------------|---------|--------| | append(x) | Add item to end | [1,2].append(3) | [1, 2, 3] | | insert(i, x) | Insert at index | [1,3].insert(1, 2) | [1, 2, 3] | | remove(x) | Remove first x | [1,2,2].remove(2) | [1, 2] | | pop(i) | Remove at index | [1,2,3].pop(1) | Returns 2, list becomes [1, 3] | | index(x) | Find index of x | [1,2,3].index(2) | 1 | | count(x) | Count occurrences | [1,2,2,3].count(2) | 2 | | sort() | Sort in place | [3,1,2].sort() | [1, 2, 3] | | reverse() | Reverse in place | [1,2,3].reverse() | [3, 2, 1] | | clear() | Remove all items | [1,2,3].clear() | [] |
List Comprehensions:
`python
Basic list comprehension
squares = [x2 for x in range(10)][0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
With condition
even_squares = [x2 for x in range(10) if x % 2 == 0][0, 4, 16, 36, 64]
Nested comprehension
matrix = [[i*j for j in range(3)] for i in range(3)][[0, 0, 0], [0, 1, 2], [0, 2, 4]]
`Tuple (tuple)
Tuples are ordered, immutable collections similar to lists but cannot be modified after creation.
Key Characteristics: - Immutable (cannot be modified) - Ordered - Allows duplicates - Can be used as dictionary keys
`python
Creating tuples
coordinates = (10, 20) single_item = (42,) # Note the comma for single-item tuple empty_tuple = () mixed_tuple = (1, "hello", 3.14)Tuple unpacking
x, y = coordinates print(f"x: {x}, y: {y}") # x: 10, y: 20Named tuples (from collections module)
from collections import namedtuple Point = namedtuple('Point', ['x', 'y']) p = Point(10, 20) print(p.x, p.y) # 10 20`Tuple Methods:
| Method | Description | Example | Result | |--------|-------------|---------|--------| | count(x) | Count occurrences | (1,2,2,3).count(2) | 2 | | index(x) | Find index | (1,2,3).index(2) | 1 |
Range (range)
Range objects represent immutable sequences of numbers, commonly used in loops.
`python
Creating ranges
r1 = range(10) # 0 to 9 r2 = range(1, 11) # 1 to 10 r3 = range(0, 20, 2) # 0, 2, 4, ..., 18 r4 = range(10, 0, -1) # 10, 9, 8, ..., 1Converting to list to see values
print(list(r1)) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(list(r3)) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]Range properties
print(len(r1)) # 10 print(5 in r1) # True print(r1[5]) # 5`Mapping Type
Dictionary (dict)
Dictionaries store data in key-value pairs and are unordered (insertion-ordered as of Python 3.7+).
Key Characteristics: - Mutable - Unordered (but insertion-ordered in Python 3.7+) - Keys must be immutable and unique - Values can be any type
`python
Creating dictionaries
student = {"name": "Alice", "age": 20, "grade": "A"} empty_dict = {} dict_from_pairs = dict([("a", 1), ("b", 2)]) dict_comprehension = {x: x2 for x in range(5)}Accessing and modifying
print(student["name"]) # Alice print(student.get("age", 0)) # 20 (with default) student["email"] = "alice@email.com" # Add new key-value student["age"] = 21 # Update existing value del student["grade"] # Remove key-value pair`Dictionary Methods:
| Method | Description | Example | Result | |--------|-------------|---------|--------| | keys() | Get all keys | {"a":1, "b":2}.keys() | dict_keys(['a', 'b']) | | values() | Get all values | {"a":1, "b":2}.values() | dict_values([1, 2]) | | items() | Get key-value pairs | {"a":1, "b":2}.items() | dict_items([('a', 1), ('b', 2)]) | | get(key, default) | Safe key access | {"a":1}.get("b", 0) | 0 | | pop(key, default) | Remove and return | {"a":1, "b":2}.pop("a") | 1, dict becomes {"b":2} | | update(other) | Merge dictionaries | d1.update(d2) | Adds d2's items to d1 | | clear() | Remove all items | {"a":1}.clear() | {} |
Dictionary Iteration:
`python
student = {"name": "Alice", "age": 20, "grade": "A"}
Iterate over keys
for key in student: print(key)Iterate over values
for value in student.values(): print(value)Iterate over key-value pairs
for key, value in student.items(): print(f"{key}: {value}")`Set Types
Set (set)
Sets are unordered collections of unique elements, useful for mathematical set operations.
Key Characteristics: - Mutable - Unordered - No duplicates - Elements must be immutable
`python
Creating sets
numbers = {1, 2, 3, 4, 5} from_list = set([1, 2, 2, 3, 3, 4]) # {1, 2, 3, 4} empty_set = set() # Note: {} creates empty dict, not set from_string = set("hello") # {'h', 'e', 'l', 'o'}Set operations
numbers.add(6) # Add single element numbers.update([7, 8, 9]) # Add multiple elements numbers.remove(5) # Remove element (raises KeyError if not found) numbers.discard(10) # Remove element (no error if not found)`Set Operations:
| Operation | Symbol | Method | Description | Example | |-----------|--------|--------|-------------|---------| | Union | \| | union() | All elements from both sets | {1,2} \| {2,3} = {1,2,3} | | Intersection | & | intersection() | Common elements | {1,2} & {2,3} = {2} | | Difference | - | difference() | Elements in first, not second | {1,2} - {2,3} = {1} | | Symmetric Difference | ^ | symmetric_difference() | Elements in either, not both | {1,2} ^ {2,3} = {1,3} |
`python
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
print(set1 | set2) # {1, 2, 3, 4, 5, 6} - union print(set1 & set2) # {3, 4} - intersection print(set1 - set2) # {1, 2} - difference print(set1 ^ set2) # {1, 2, 5, 6} - symmetric difference
Set relationships
print(set1.issubset(set2)) # False print(set1.issuperset({1, 2})) # True print(set1.isdisjoint({7, 8})) # True`Frozen Set (frozenset)
Frozen sets are immutable versions of sets, useful when you need a set that cannot be modified.
`python
Creating frozen sets
fs1 = frozenset([1, 2, 3, 4]) fs2 = frozenset("hello")Frozen sets can be dictionary keys (unlike regular sets)
dict_with_frozenset_keys = { frozenset([1, 2]): "value1", frozenset([3, 4]): "value2" }All set operations work, but no modification methods
print(fs1 | fs2) # Union worksfs1.add(5) # This would raise AttributeError
`None Type
NoneType (None)
None represents the absence of a value and is Python's equivalent of null in other languages.
`python
None usage
result = None data = [1, 2, None, 4, 5]Functions return None by default
def no_return(): print("This function returns None")return_value = no_return() print(return_value) # None
Checking for None
if result is None: print("No result available")None in boolean context
print(bool(None)) # FalseCommon pattern: default parameter
def greet(name=None): if name is None: name = "World" print(f"Hello, {name}!")greet() # Hello, World!
greet("Alice") # Hello, Alice!
`
Type Conversion
Python provides built-in functions to convert between different data types.
Explicit Type Conversion
`python
To integer
print(int(3.14)) # 3 print(int("123")) # 123 print(int("1010", 2)) # 10 (binary to decimal) print(int(True)) # 1To float
print(float(42)) # 42.0 print(float("3.14")) # 3.14 print(float(True)) # 1.0To string
print(str(123)) # "123" print(str(3.14)) # "3.14" print(str(True)) # "True" print(str([1, 2, 3])) # "[1, 2, 3]"To boolean
print(bool(1)) # True print(bool(0)) # False print(bool("")) # False print(bool("text")) # TrueTo list
print(list("hello")) # ['h', 'e', 'l', 'l', 'o'] print(list(range(5))) # [0, 1, 2, 3, 4] print(list((1, 2, 3))) # [1, 2, 3]To tuple
print(tuple([1, 2, 3])) # (1, 2, 3) print(tuple("hello")) # ('h', 'e', 'l', 'l', 'o')To set
print(set([1, 2, 2, 3])) # {1, 2, 3} print(set("hello")) # {'h', 'e', 'l', 'o'}`Type Conversion Table
| From/To | int | float | str | bool | list | tuple | set | |---------|-----|-------|-----|------|------|-------|-----| | int | - | float(x) | str(x) | bool(x) | [x] | (x,) | {x} | | float | int(x) | - | str(x) | bool(x) | [x] | (x,) | {x} | | str | int(x) | float(x) | - | bool(x) | list(x) | tuple(x) | set(x) | | bool | int(x) | float(x) | str(x) | - | [x] | (x,) | {x} | | list | N/A | N/A | str(x) | bool(x) | - | tuple(x) | set(x) | | tuple | N/A | N/A | str(x) | bool(x) | list(x) | - | set(x) | | set | N/A | N/A | str(x) | bool(x) | list(x) | tuple(x) | - |
Type Checking and Introspection
`python
type() function
print(type(42)) #isinstance() function (preferred)
print(isinstance(42, int)) # True print(isinstance(3.14, float)) # True print(isinstance("hello", str)) # True print(isinstance([1, 2], (list, tuple))) # True (check multiple types)hasattr() - check if object has attribute
print(hasattr("hello", "upper")) # True print(hasattr(42, "upper")) # Falsedir() - list all attributes and methods
print(dir(str)) # Lists all string methods`Best Practices
Choosing the Right Data Type
1. Use integers for whole numbers and counting
`python
count = 10
user_id = 12345
`
2. Use floats for decimal numbers and mathematical calculations
`python
price = 19.99
pi = 3.14159
`
3. Use strings for text data
`python
name = "Alice"
message = "Hello, World!"
`
4. Use lists for ordered, mutable collections
`python
shopping_list = ["apples", "bananas", "oranges"]
numbers = [1, 2, 3, 4, 5]
`
5. Use tuples for ordered, immutable collections
`python
coordinates = (10, 20)
rgb_color = (255, 128, 0)
`
6. Use dictionaries for key-value relationships
`python
student = {"name": "Alice", "age": 20, "grade": "A"}
config = {"host": "localhost", "port": 8080}
`
7. Use sets for unique collections and set operations
`python
unique_tags = {"python", "programming", "tutorial"}
allowed_users = {"admin", "user1", "user2"}
`
Performance Considerations
| Operation | List | Tuple | Set | Dict | |-----------|------|-------|-----|------| | Access by index | O(1) | O(1) | N/A | N/A | | Access by key | N/A | N/A | N/A | O(1) | | Search | O(n) | O(n) | O(1) | O(1) | | Insert at end | O(1) | N/A | O(1) | O(1) | | Insert at beginning | O(n) | N/A | O(1) | N/A | | Delete | O(n) | N/A | O(1) | O(1) |
Memory Usage Tips
1. Use tuples instead of lists when data won't change 2. Use sets for membership testing instead of lists 3. Use generators for large sequences that don't need to be stored entirely in memory 4. Consider using slots in classes to reduce memory overhead
Common Pitfalls and How to Avoid Them
1. Mutable default arguments
`python
# Wrong
def add_item(item, target_list=[]):
target_list.append(item)
return target_list
# Correct
def add_item(item, target_list=None):
if target_list is None:
target_list = []
target_list.append(item)
return target_list
`
2. Modifying a list while iterating
`python
# Wrong
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num % 2 == 0:
numbers.remove(num)
# Correct
numbers = [1, 2, 3, 4, 5]
numbers = [num for num in numbers if num % 2 != 0]
`
3. Using == instead of is for None comparison
`python
# Wrong
if value == None:
pass
# Correct
if value is None:
pass
`
Understanding Python data types is fundamental to writing effective Python code. Each data type has its specific use cases, methods, and performance characteristics. By choosing the appropriate data type for your specific needs, you can write more efficient, readable, and maintainable code. Remember that Python's dynamic typing gives you flexibility, but understanding these types helps you make informed decisions about data structure and algorithm choices in your programs.