Reference library

Python Code Samples

Copy-ready Python snippets by topic and difficulty — short, focused, and runnable in the browser editor.

2 matches
Algorithms & data structures easy

Move Zeroes to End in Python Maintaining Order

In-place algorithm that moves all zeroes to the end of a list while preserving the relative order of non-zero elements.

two-pointers in-place array
Python
def move_zeroes(nums):
    non_zero_index = 0
    for i in range(len(nums)):
        if nums[i] != 0:
            nums[non_zero_index], nums[i] = nums[i], nums[non_zero_index]
            non_zero_index += 1
    return nums

if __name__ == "__main__":
    example = [0, 1, 0, 3, 12]
    result = move_zeroes(example)
  …
14 0 Open
Algorithms & data structures medium

Set Matrix Zeroes in Python: Markers List Grid Demo

Given a matrix, this code finds all rows and columns that contain a zero and sets every element in those rows and columns to zero, using boolean marker arrays.

matrix arrays algorithm
Python
def set_zeroes(matrix):
    rows, cols = len(matrix), len(matrix[0])
    row_markers = [False] * rows
    col_markers = [False] * cols

    # First pass: record which rows and columns contain zeros
    for i in range(rows):
        for j in range(cols):
            if matrix[i][j] == 0:
                row_markers[i] …
14 0 Open

Browse by section

Each section groups closely related Python snippets.

Guide: free Python code samples library

Copy-ready Python snippets for learners and developers

PythonSkillset code samples are short, focused examples organised by topic and difficulty. Every snippet is server-rendered HTML — readable by search engines and easy to copy. Open any sample, read the notes, copy the code, then press Try in editor to run it in the browser with Pyodide.

How to use this library

  1. Pick a topic section — strings, lists, files, functions, and more
  2. Open a sample, read How it works, and copy the code block
  3. Run it in the IDE, tweak values, then take a related quiz or tutorial lesson

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.