Python programs

Python Programs with Output

Complete programs, each explained step by step with its output. Run them here, open them in the editor, or step through them with Visualize.

Numbers

Even or odd, primes, factorials, Fibonacci, Armstrong numbers and other school programs.

Patterns

Star, number and Pascal triangle patterns printed with loops.

Strings

Palindromes, vowels, word counts, anagrams and other text programs.

Searching and sorting

Linear and binary search, and the classic sorting algorithms step by step.

Data structures

Stacks, queues, linked lists, trees, heaps and techniques built on lists.

Graphs

Breadth-first and depth-first search, shortest paths, ordering and connected groups.

Dynamic programming

Problems solved by remembering the answers to smaller problems.

Backtracking

Trying every choice and undoing the ones that fail: permutations, subsets, N-Queens.