This Python DSA lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.
Data structures decide how values are stored. Algorithms decide the steps that search or reorder them.
Python lists, dicts, and deques cover the first structures. Later lessons build a stack, a queue, and a few sorts.
Example 1: Start here
Pick a structure for the operation you do most often, not for the one that looks advanced.
items = ["Python", "SQL", "Git"] # a list; the values stay in this order
print(items[0])
print("SQL" in items) # in is True when the value is found
Example 2: Walk the values
Python DSA starts by visiting one value at a time. Read the printed lines from top to bottom.
values = [13, 3, 11, 7] # a list; the values stay in this order
for value in values: # repeat the indented lines once per value
print(value)
Example 3: Keep the largest
Many data-structure lessons reduce a list to one answer. This one remembers the largest number seen so far.
values = [13, 3, 11, 7] # a list; the values stay in this order
best = values[0] # the first value
for value in values: # repeat the indented lines once per value
if value > best: # run the indented lines only when this is true
best = value # another name for the same value
print(best)
Example 4: Search for a target
A search asks one yes-or-no question: is the target in the collection? Python DSA builds on that question.
values = [3, 13, 11] # a list; the values stay in this order
target = 13 # a whole number
print(target in values) # in is True when the value is found
