The for Loop and range()
A Python for loop walks over the items of a collection directly. range() supplies numbers when you genuinely need a counter, which is less often than...
968 published notes across every category.
A Python for loop walks over the items of a collection directly. range() supplies numbers when you genuinely need a counter, which is less often than...
while repeats for as long as a condition holds. Use it when the number of repetitions is unknown, and make sure something inside the loop can make the...
break leaves a loop, continue skips to the next iteration, pass does nothing, and the loop else runs only when no break happened.
Printing shapes with loops teaches nested iteration better than any explanation. Every pattern comes from the same three questions.
A list is an ordered, changeable sequence that can hold anything. Indexing, slicing and slice assignment cover most of what you will ever do with one.
Most list methods change the list in place and return None. Knowing which ones return a value and which do not prevents the most common list bug.
Lists of lists model grids and tables, copying them needs care, and a handful of patterns cover most real list work.
A tuple is a list that cannot change. That single restriction makes it usable as a dictionary key, safe to share, and the natural way to return severa...
Unpacking pulls a sequence apart into names in one statement. It powers swapping, loops over pairs, multiple return values and the star syntax.
A set stores unique values with no order and answers membership questions almost instantly. That trade - lose order, gain speed and uniqueness - is th...
Four operations answer four everyday questions: what is in either, what is in both, what is only in the first, and what is in exactly one.
A dictionary maps keys to values and finds any value in roughly constant time. It is the most used container in Python and the structure most real dat...