Functions

Python FundamentalsBasicsFree Lesson

Advertisement

Introduction

Functions are reusable blocks of code that perform a specific task.

Defining Functions

def greet(name):
    """Greet a person by name."""
    return f"Hello, {name}!"

# Default parameters
def power(base, exponent=2):
    return base ** exponent

# Multiple return values
def solve(a, b):
    sum_ = a + b
    product = a * b
    return sum_, product

Args and Kwargs

def flexible(*args, **kwargs):
    print("Args:", args)
    print("Kwargs:", kwargs)

flexible(1, 2, name="John", age=25)

Practice Problems

  1. Create a function to check prime numbers
  2. Implement a Fibonacci sequence generator
  3. Build a calculator with multiple operations
  4. Create a function that accepts variable arguments
  5. Write a decorator to measure function execution time

Advertisement

Need Expert Python Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement