Python Code
Samples
Copy-ready Python snippets by topic and difficulty — short, focused, and runnable in the browser editor.
How to Reverse a List in Place Without Using reverse() in Python
A two-pointer while loop swaps elements from both ends toward the center to reverse a list in place without creating a copy.
def reverse_list_in_place(lst):
left = 0
right = len(lst) - 1
while left < right:
lst[left], lst[right] = lst[right], lst[left]
left += 1
right -= 1
if __name__ == "__main__":
my_list = [1, 2, 3, 4, 5]
print("Original:", my_list)
reverse_list_in_place(my_list)
prin…
How to Sort a List in Python in Ascending and Descending Order
This code demonstrates three ways to sort a list in Python: returning a new sorted list with sorted(), reversing the sort order, and sorting a list in place with the list.sort() method.
def get_sorted_data(numbers):
"""Return a new list sorted in ascending order."""
return sorted(numbers)
def reverse_sort(data):
"""Return a new list sorted in descending order."""
return sorted(data, reverse=True)
def sort_in_place(data):
"""Sort the given list in place (modifies original)."""
…
How to Heapify a List into a Min Heap with heapq in Python
Convert any list into a valid min heap in-place using Python's heapq.heapify(), then pop the smallest element to verify heap order.
import heapq
data = [5, 3, 8, 1, 9, 2, 7, 4, 6]
print("Original list:", data)
heapq.heapify(data)
print("Min heap:", data)
popped = heapq.heappop(data)
print("Smallest element popped:", popped)
print("Heap after pop:", data)
How to Sort Colors (Dutch National Flag) in Python
In-place sorting of a list of 0s, 1s, and 2s using the Dutch National Flag algorithm with O(n) time and O(1) space.
def sort_colors(nums):
low, mid, high = 0, 0, len(nums) - 1
while mid <= high:
if nums[mid] == 0:
nums[low], nums[mid] = nums[mid], nums[low]
low += 1
mid += 1
elif nums[mid] == 1:
mid += 1
else: # nums[mid] == 2
nums[mid], n…
Merge Two Sorted Arrays Without Extra Space in Python
Merge two sorted arrays in-place from the end, using the trailing zeros in the first array to avoid extra space.
def merge_sorted(arr1, arr2):
m, n = len(arr1), len(arr2)
i, j = m - 1, n - 1
while j >= 0:
if i >= 0 and arr1[i] > arr2[j]:
arr1[i + j + 1] = arr1[i]
i -= 1
else:
arr1[i + j + 1] = arr2[j]
j -= 1
return arr1
if __name__ == "__main__":
…
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.
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)
…
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
- Pick a topic section — strings, lists, files, functions, and more
- Open a sample, read How it works, and copy the code block
- 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.