Python Code
Samples
Easy snippets you can copy, study, and run in the browser editor.
Test a Python Pipeline with Fixture Sample Rows
Test pipeline functions with sample rows provided by a pytest fixture, verifying required keys and value constraints.
import pytest
def get_value(data: dict, key: str):
return data.get(key)
def sample_rows():
return [
{"name": "Alice", "age": 30, "city": "London"},
{"name": "Bob", "age": 25, "city": "Paris"},
{"name": "Charlie", "age": 35, "city": "Berlin"},
]
@pytest.fixture
def sample_data(…
How to Use pytest Fixtures and conftest.py for Shared Setup in Python
Learn how to define reusable pytest fixtures for shared setup and use them to keep tests clean and maintainable.
import pytest
class Calculator:
def add(self, a, b):
return a + b
def multiply(self, a, b):
return a * b
@pytest.fixture
def calc():
return Calculator()
@pytest.fixture
def sample_numbers():
return (3, 5)
def test_add(calc, sample_numbers):
a, b = sample_numbers
assert c…
Generate Fake User Data with Faker in Python
Use the Faker library to generate realistic fake user profiles with names, emails, phone numbers, and addresses for tests or demos.
from faker import Faker
fake = Faker()
def generate_user():
return {
"name": fake.name(),
"email": fake.email(),
"phone": fake.phone_number(),
"address": fake.address().replace("\n", ", "),
}
if __name__ == "__main__":
user = generate_user()
for key, value in user.ite…
How to Share Fixtures Across Tests with pytest conftest
Learn how to define pytest fixtures in conftest.py and control their scope (function, module, session) so every test in a directory reuses the same setup and teardown.
import pytest
@pytest.fixture
def sample_data():
"""Simple fixture available to all tests in this directory."""
return {"name": "Alice", "age": 30}
@pytest.fixture(scope="session")
def session_data():
"""Fixture created once per test session."""
return {"session_id": 12345}
@pytest.fixture(scope="mo…
How to Use setUp and tearDown in Python unittest TestCase
Demonstrates how to structure unit tests with setUp and tearDown methods in Python's unittest framework for reusable test fixtures.
import unittest
class ExampleTest(unittest.TestCase):
def setUp(self):
self.data = [1, 2, 3]
def tearDown(self):
self.data = None
def test_length(self):
self.assertEqual(len(self.data), 3)
def test_contains(self):
self.assertIn(2, self.data)
if __name__ == "__main…
How to Use the pytest tmp_path Fixture for Temporary Directories
Use pytest's built-in tmp_path fixture to create a unique temporary directory per test for clean file I/O testing.
import pytest
def test_write_and_read_file(tmp_path):
# tmp_path is a pytest fixture that provides a temporary directory
# unique to each test invocation
data_file = tmp_path / "data.txt"
data_file.write_text("hello world")
assert data_file.read_text() == "hello world"
def test_multiple_tmp_pat…
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.