Data Types & Collections - Python Basics
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
- Basic Types: int, float, complex, bool, str, None
- Core Collections: list, tuple, set, dict
- Extra & Useful Types: range, bytes, bytearray, frozenset
- Helpful Tools from
collections
(Counter, defaultdict, deque, namedtuple) - Mutability, Aliasing & Copying (shallow vs deep)
- Comprehensions & Generator Expressions
- Choosing the Right Collection (cheat table + Big-O)
- Exercises & FAQ
Types at a Glance
Type | Category | Literal/Constructor | Ordered? | Mutable? | Allows Duplicates? | Typical Use |
---|---|---|---|---|---|---|
int | Numeric | 42 | Yes | No | Yes | Counting, indexes |
float | Numeric | 3.14 | Yes | No | Yes | Measurements, decimals |
complex | Numeric | 2+3j | Yes | No | Yes | Signal processing, math |
bool | Logical | True /False | Yes | No | N/A | Conditions, flags |
str | Text | "hi" , 'hi' | Yes | No | Yes | Text data |
None | Singleton | None | — | No | — | No value / missing |
list | Sequence | [1,2,3] | Yes | Yes | Yes | Dynamic arrays |
tuple | Sequence | (1,2,3) | Yes | No | Yes | Fixed records |
set | Set | {1,2,3} | No | Yes | No | Uniqueness, membership |
frozenset | Set | frozenset([1,2]) | No | No | No | Hashable set (keys) |
dict | Mapping | {"a":1} | Yes* | Yes | Keys unique | Lookups by key |
range | Sequence | range(5) | Yes | No | Yes | Efficient sequences |
bytes | Binary | b"abc" | Yes | No | Yes | Immutable bytes |
bytearray | Binary | bytearray(b"abc") | Yes | Yes | Yes | Mutable bytes |
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
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
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) | |||
---|---|---|---|
Method | Use | Example | Output |
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
[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)
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
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 | ||
---|---|---|
List | Set | Dict |
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])
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
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)
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
Choosing the Right Collection
Goal | Best Choice | Why |
---|---|---|
Preserve order and edit freely | list | Simple, flexible, fast append |
Fixed record / hashable | tuple | Immutable, can be dict key |
Remove duplicates / test membership | set | Enforces uniqueness, O(1) in |
Map names → values | dict | Fast lookups, readable structure |
Fast ends queue/stack | deque | O(1) append/pop both ends |
Count things | Counter | Convenient frequency API |
Group items by key | defaultdict(list) | No-KeyError grouping |
Very Rough Big-O (Common Operations) | |||
---|---|---|---|
Structure | Access by Index/Key | Append/Insert | Membership Test |
list | O(1) / O(1) | append O(1), insert O(n) | O(n) |
tuple | O(1) | — (immutable) | O(n) |
set | — | O(1) | O(1) |
dict | O(1) | O(1) | O(1) on keys |
deque | — | append/pop ends O(1) | O(n) |
Exercises
- Clean a list: Given
data = ["py", "py", "ai", "ml", "ai"]
, remove duplicates while preserving an appearance order. (Hint: use a set or dict tricks) - Word frequency: Build a
Counter
of words from a sentence and print the top 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"]}
. - Safe parse: Write
to_int(s, default=None)
that returns anint
or the default if parsing fails.
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.
Continue Learning
- Python Programming: The Complete Beginner’s Guide
- What is Programming? A Complete Beginner’s Guide
- Python Strings — Python Basics
- Problem Solving: Your Secret Superpower
- Python Operators
Author: IT Tech Guide — Practical, beginner-friendly programming tutorials. Subscribe for weekly Python posts.
0 Comments