A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Standard Libraries & Collections

Python Libraries Part 1

Easy

Python's motto is 'batteries included', and Part 1 covers the batteries you will reach for daily: the built-in functions that replace whole loops (sum, sorted, enumerate, zip), the math module, and the essential methods of lists, dicts, sets and strings. The theme is simple — in Python the built-in is almost always both shorter and faster than the loop you were about to write, because it runs in C underneath.

Built-ins that replace loops

Learn these until they are reflexes:

sum(iterable), min, max — totals and extremes without an accumulator loop. sorted(iterable, key=..., reverse=True) — returns a new sorted list; key is a function extracting what to sort by (e.g. key=len, or key=lambda p: p[1]). enumerate(iterable) — yields (index, value) pairs, ending the awkward range(len(...)) dance. zip(a, b) — walks two sequences in lockstep; zip(*pairs) unzips. any / all — short-circuit checks over a condition. abs, round, pow(base, exp, mod) — pow with a modulus does fast modular exponentiation for free.

Writing these by hand is not wrong, but in an interview the built-in shows fluency — and it removes whole categories of off-by-one bugs.

The math module

import math unlocks the standard toolkit: math.gcd(a, b) (Euclid, done for you), math.lcm, math.factorial, math.isqrt(n) — the exact integer square root, essential for prime and divisor loops where float sqrt can round wrongly on huge inputs — math.sqrt for floats, math.ceil / math.floor, math.log2 and math.log10, math.comb(n, r) for combinations, and constants math.pi / math.inf. Prefer math.isqrt over int(math.sqrt(n)) whenever the result must be exact.

import math print(math.gcd(48, 18)) # 6 print(math.lcm(4, 6)) # 12 print(math.isqrt(50)) # 7 (exact) print(math.factorial(5)) # 120 print(math.comb(5, 2)) # 10

List, dict and set essentials

Lists: append/pop from the end are O(1); insert(0, x) and pop(0) are O(n) because everything shifts — if you need front operations, that is what deque (Part 2) exists for. list.sort() sorts in place; slicing a[::-1] builds a reversed copy; list comprehensions ([x*x for x in nums if x > 0]) are the idiomatic filter-map in one line.

Dicts preserve insertion order and give O(1) average lookup; d.get(key, default) reads safely, d.items() iterates pairs. Sets give O(1) membership (x in s) and one-line uniqueness: len(set(nums)) counts distinct values. The membership test is the quiet superpower — replacing 'x in list' (O(n)) with a set turns many O(n^2) beginner solutions into O(n).

String essentials and fast input

Strings are immutable sequences: s.split() splits on whitespace, sep.join(list) assembles, s.strip() trims ends, s.lower() / s.upper() change case, s.count(sub) and s.find(sub) search, and s.isdigit() / s.isalpha() classify characters. Reversal is s[::-1]; building via ''.join(pieces) beats += in loops.

For input at scale, replace input() with sys.stdin.readline, or read everything once via data = sys.stdin.read().split() and walk an index — the standard Python speed fix on judges.

Key points

  • Built-ins (sum, sorted with key, enumerate, zip, any/all) are shorter and C-fast — reach for them before loops.
  • math gives exact tools: gcd, lcm, factorial, comb, and isqrt for precise integer square roots.
  • List end operations are O(1); front insert/pop are O(n) — that is deque's job (Part 2).
  • 'x in set' is O(1) vs O(n) for lists — converting to a set kills many accidental O(n^2) solutions.
  • Strings: split/join/strip/find; assemble with ''.join, reverse with [::-1].
  • Fast input: sys.stdin.readline, or one bulk sys.stdin.read().split().

Suggested questions

Sum of Array Elements ↗ One built-in: solve it with sum() and feel the difference from the loop version.
Reverse an array ↗ Slicing [::-1] vs the in-place two-pointer method — know both, use each deliberately.
Valid Anagram ↗ sorted() on both strings is the brute-force baseline you will later beat with counting.
Basic Arrays The concept lesson behind lists-as-arrays: access costs, traversal patterns, in-place reversal.

Source note: this lesson follows the structure of Striver's public A2Z DSA course (takeUforward). Each problem links to its original platform, and all problems and content belong to their respective owners — STUDY-BUDDY does not claim ownership of the sheet or its problems, and no permission beyond public linking is claimed.