Functions, Classes, and Patterns

Write reusable code with functions, classes, decorators, and functional tools like map/filter and lambdas. These patterns teach you how to structure programs for clarity and extensibility.

Python Lambda, map, filter

Use lambda with map and filter.

nums = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x * x, nums))
evens = list(filter(lambda x: x % 2 == 0, nums))
print(squares)
print(evens)

Output

[1, 4, 9, 16, 25]
[2, 4]

Python Functions

Define and call functions with default and keyword arguments.

def greet(name: str, punctuation: str = "!") -> str:
    return f"Hello, {name}{punctuation}"

print(greet("Ann"))
print(greet("Bob", punctuation="."))

Output

Hello, Ann!
Hello, Bob.

Python Classes

Create a simple class with methods and attributes.

class Counter:
    def __init__(self):
        self.value = 0
    def increment(self):
        self.value += 1
    def get(self):
        return self.value

c = Counter()
c.increment()
print(c.get())

Output

1

Python Decorators

Wrap functions to add behavior.

def log(fn):
    def wrapper(*args, **kwargs):
        print("calling", fn.__name__)
        return fn(*args, **kwargs)
    return wrapper

@log
def add(a, b):
    return a + b

print(add(2, 3))

Output

calling add
5

Python Program to Differentiate Between type() and isinstance()

Shows difference.

class B: pass
class C(B): pass
obj = C()
print(type(obj) is B)
print(isinstance(obj, B))

Output

False
True

Python Program to Represent enum

Uses Enum.

from enum import Enum
class Color(Enum):
    RED = 1
    GREEN = 2
print(Color.RED.name, Color.RED.value)

Output

RED 1

Python Program to Return Multiple Values From a Function

Returns a tuple.

def stats(a, b):
    return a+b, a*b
print(stats(3,4))

Output

(7, 12)

Python Strings Basics

Concatenate, format, and slice strings.

s = "Hello, Python"
print(s.lower())
print(s.upper())
print(s[0:5])
print(f"Length: {len(s)}")

Output

hello, python
HELLO, PYTHON
Hello
Length: 13

Python Program to Find the Square Root

Computes square root using math module.

import math
print(math.sqrt(16))

Output

4.0

Keep Practicing

Use the online Python compiler to run examples and test variations. Reinforce learning by building small scripts for each topic.

FAQ

You can use the online Python compiler linked above to run examples instantly. For larger projects, install Python locally.

Yes. Examples cover variables, control flow, data structures, functions, classes, and file I/O—ideal for beginners.

Yes. The programs are tested and produce consistent output across major platforms.

Learn Python by Practicing Examples

Hands-on practice is the fastest way to understand Python. Each example focuses on a single concept—from variables, strings, lists, and dictionaries to loops, functions, classes, exceptions, and file I/O. Modify examples, add new functions, and see how clean design leads to readable, maintainable code.

Use our tools to deepen learning: the Python Compiler to run snippets, and the Python Tutorial for structured lessons.

Beginner-Friendly

Start with variables, operators, and control flow. Build confidence with small, readable programs.

Practical Patterns

Practice lists, dicts, comprehensions, functions, classes, exceptions, and file handling.

Grow Skills

Explore generators and decorators to write expressive, reusable code.