Data Types & Collections - Python Basics

Data Types & Collections - Python Basics

Why this matters: In Python, everything is an object with a type. Knowing how numbers, strings, booleans, None, and collections like lists, tuples, sets, and dictionaries behave will make your code cleaner, faster, and far less buggy. This guide is beginner-friendly, but rich enough to be your quick reference later.

Types at a Glance

Type Category Literal/Constructor Ordered? Mutable? Allows Duplicates? Typical Use
intNumeric42YesNoYesCounting, indexes
floatNumeric3.14YesNoYesMeasurements, decimals
complexNumeric2+3jYesNoYesSignal processing, math
boolLogicalTrue/FalseYesNoN/AConditions, flags
strText"hi", 'hi'YesNoYesText data
NoneSingletonNoneNoNo value / missing
listSequence[1,2,3]YesYesYesDynamic arrays
tupleSequence(1,2,3)YesNoYesFixed records
setSet{1,2,3}NoYesNoUniqueness, membership
frozensetSetfrozenset([1,2])NoNoNoHashable set (keys)
dictMapping{"a":1}Yes*YesKeys uniqueLookups by key
rangeSequencerange(5)YesNoYesEfficient sequences
bytesBinaryb"abc"YesNoYesImmutable bytes
bytearrayBinarybytearray(b"abc")YesYesYesMutable bytes
Note: Dicts preserve insertion order in modern Python, which makes them feel “ordered” in practice.

Basic Types: int, float, complex, bool, str, None

Integers (int)

Whole numbers (positive, negative, zero). Python’s int has arbitrary precision, so it can grow very large without overflow.

# Counting and indexing
n = 7
print(n // 3, n % 3)      # integer division and remainder
print(pow(2, 10))         # 1024
print(int("42"))          # parse from string
  
Tip: Use // for integer division when you want an int result.

Floating Point (float)

Numbers with decimals. Floats may have tiny rounding errors due to binary representation.

price = 19.99
tax = 0.18
total = price * (1 + tax)
print(round(total, 2))     # monetary display
  
Gotcha: 0.1 + 0.2 != 0.3 exactly. For money, consider decimal.Decimal.

Complex (complex)

Numbers with real and imaginary parts: a + bj. Useful in signal processing, simulations, and math.

z = 2 + 3j
print(z.real, z.imag)  # 2.0 3.0
print(abs(z))          # magnitude
  

Booleans (bool)

True or False. Anything can be coerced to bool using bool(x) with Python’s “truthiness” rules.

print(bool(""))     # False for empty string
print(bool([1]))    # True for non-empty list
print(5 > 3 and 2 < 1)  # False
  

Strings (str)

Immutable sequences of Unicode characters. Support slicing, searching, formatting, and more.

name = "Asha"
print(name[:2])           # 'As'
print(name.upper())       # 'ASHA'
print(" ".join(["Py", "thon"]))  # 'Py thon'

# f-strings (recommended)
age = 26
print(f"{name} is {age} years old.")
  
String Methods (Popular)
MethodUseExampleOutput
split()Split by delimiter"a,b,c".split(",")["a","b","c"]
strip()Trim whitespace" hi ".strip()"hi"
replace()Substitute"2024-01".replace("-", "/")"2024/01"
startswith()Prefix test"python".startswith("py")True
find()First index"banana".find("na")2

None (the “no value” object)

Use None to represent “missing” or “not yet available.” Check with is / is not.

result = None
if result is None:
    print("Waiting for computation...")
  

Core Collections: list, tuple, set, dict

List — ordered & mutable

Think of lists as dynamic arrays. They maintain order and allow duplicates.

nums = [10, 20, 20, 30]
nums.append(40)          # add at end
nums.insert(1, 15)       # insert at index
nums.remove(20)          # remove first 20
print(nums)              # [10, 15, 20, 30, 40]
print(nums[0], nums[-1]) # indexing, negative index
  
Tip: Favor list comprehensions for readable transforms: [x*x for x in nums if x%2==0]

Tuple — ordered & immutable

Great for fixed “records” (like coordinates). Faster and hashable (can be dict keys if items are hashable).

point = (10, 20)
x, y = point             # unpacking
print(x, y)
  
When to use: Use tuple when you want to guarantee no mutation (safer APIs).

Set — unique & unordered

Membership tests are fast. Great for uniqueness and set math.

tags = {"python", "tips", "tips"}
print(tags)                    # {'python', 'tips'}
tags.add("beginner")
print("python" in tags)

# Set operations
a = {1,2,3}; b = {3,4}
print(a | b, a & b, a - b)     # union, intersection, difference
  
Warning: Sets are unordered. Don’t rely on index positions.

Dict — key → value mapping

Perfect for labeled data and fast lookups. Keys must be hashable (e.g., str, int, tuple of immutables).

user = {"name":"Asha", "age":26}
user["city"] = "Mumbai"           # add/update
print(user.get("role", "guest"))  # safe get with default
for k, v in user.items():
    print(k, ":", v)
  
Common Operations — Quick View
ListSetDict
append(x), extend(xs) add(x), update(xs) d[k]=v, update({...})
pop(), remove(x) discard(x), remove(x) pop(k, default), del d[k]
sort(), reverse() |, &, -, ^ keys(), values(), items()

Extra & Useful Types: range, bytes, bytearray, frozenset

range

Efficient, lazy sequence of integers; great for loops and indexing.

for i in range(2, 10, 2):
    print(i)     # 2 4 6 8
  

bytes & bytearray

bytes is immutable; bytearray is mutable. Useful for binary I/O, networking, cryptography.

data = b"PNG"                 # bytes literal
buf = bytearray(b"ABC")
buf[0] = 97                   # mutate ('a')
print(bytes(buf))             # b'aBC'
  

frozenset

Immutable set — can be used as a dict key or a member of another set.

fs = frozenset({1,2,3})
d = {fs: "groupA"}
print(d[fs])
  
Pro: Use immutable types (tuple, frozenset) to make safe keys for dicts or members of sets.

Helpful Tools from collections

Counter — frequency counting

from collections import Counter
words = "to be or not to be".split()
freq = Counter(words)
print(freq.most_common(2))      # [('to', 2), ('be', 2)]
  

defaultdict — auto-initialize missing keys

from collections import defaultdict
group = defaultdict(list)
for user, city in [("Asha","Pune"), ("Tom","Pune"), ("Mia","Goa")]:
    group[city].append(user)
print(group["Pune"])            # ['Asha','Tom']
  

deque — fast appends/pops on both ends

from collections import deque
q = deque([1,2,3])
q.appendleft(0); q.append(4)
print(q.popleft(), q.pop())     # 0 4
  

namedtuple — lightweight, readable records

from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
p = Point(10, 20)
print(p.x, p.y)                 # 10 20
  
Tip: For richer, mutable records with defaults and type hints, also explore dataclasses.dataclass.

Mutability, Aliasing & Copying

Variables hold references to objects. If two variables reference the same mutable object and one changes it, both “see” the change.

a = [1,2]
b = a
a.append(3)
print(b)     # [1,2,3]  (same list)
  

Shallow vs Deep Copy

import copy
x = [[1,2], [3,4]]

shallow = x.copy()       # or list(x)
deep    = copy.deepcopy(x)

x[0].append(99)
print(shallow)           # [[1,2,99],[3,4]]  (inner list shared)
print(deep)              # [[1,2], [3,4]]   (fully independent)
  
Warning: list.copy() is shallow. For nested structures, use copy.deepcopy() when you truly need isolation.

Comprehensions & Generator Expressions

Python’s comprehensions make transformations concise and readable.

List Comprehension

nums = [1,2,3,4,5,6]
evens_squared = [n*n for n in nums if n%2==0]
print(evens_squared)        # [4, 16, 36]
  

Set & Dict Comprehension

unique_lengths = {len(w) for w in ["aa","bbb","aa"]}   # {2,3}

index_map = {i: chr(65+i) for i in range(3)}  # {0:'A',1:'B',2:'C'}
  

Generator Expression (memory friendly)

total = sum(n*n for n in range(1_000_000))  # streams values lazily
  
Pro: Prefer generators when the result is consumed once (e.g., streaming sums, writing to files) to reduce memory usage.

Choosing the Right Collection

GoalBest ChoiceWhy
Preserve order and edit freelylistSimple, flexible, fast append
Fixed record / hashabletupleImmutable, can be dict key
Remove duplicates / test membershipsetEnforces uniqueness, O(1) in
Map names → valuesdictFast lookups, readable structure
Fast ends queue/stackdequeO(1) append/pop both ends
Count thingsCounterConvenient frequency API
Group items by keydefaultdict(list)No-KeyError grouping
Very Rough Big-O (Common Operations)
StructureAccess by Index/KeyAppend/InsertMembership Test
listO(1) / O(1)append O(1), insert O(n)O(n)
tupleO(1)— (immutable)O(n)
setO(1)O(1)
dictO(1)O(1)O(1) on keys
dequeappend/pop ends O(1)O(n)
Note: Big-O ignores constants and cache behavior; it’s a guide, not a guarantee.

Exercises

  1. Clean a list: Given data = ["py", "py", "ai", "ml", "ai"], remove duplicates while preserving an appearance order. (Hint: use a set or dict tricks)
  2. Word frequency: Build a Counter of words from a sentence and print the top 3.
  3. Invert a dict: Turn {"a":1,"b":1,"c":2} into a mapping of values to list of keys: {1:["a","b"], 2:["c"]}.
  4. Safe parse: Write to_int(s, default=None) that returns an int or the default if parsing fails.
Answers (hint-style): (1) seen set with loop or dict.fromkeys(...); (2) Counter(text.split()).most_common(3); (3) iterate items(), append to defaultdict(list); (4) try/except ValueError.

FAQ

Are Python variables typed?

Values have types; variables are names referencing those values. Python is dynamically typed and resolves types at runtime.

When should I use a tuple vs a list?

Use a tuple for fixed, read-only records (safer, hashable); use a list when the size/content can change.

Why does my float behave strangely?

Binary floats can’t exactly represent some decimals; use decimal.Decimal for currency and exact decimal math.

How do I make an unchangeable set?

Use frozenset. It’s hashable and can be a dict key.

Best Practices Summary

  • Pick the collection for the job: list for order, set for uniqueness, dict for labeled lookups, tuple for fixed records.
  • Understand mutability; copy wisely (copy.deepcopy for nested).
  • Use comprehensions for clear, compact transforms.
  • Prefer generators when streaming large data.
  • Rely on collections tools (Counter, defaultdict, deque) for common patterns.
Practical habit: Before coding, ask: “Do I need order? Uniqueness? Fast lookup? Mutability?” Choose accordingly—then your code writes itself.

Continue Learning

Author: IT Tech Guide — Practical, beginner-friendly programming tutorials. Subscribe for weekly Python posts.

Post a Comment

0 Comments