Learn programming concepts interactively
Sum of two numbers
Base raised to exponent power
Average of all numbers
# Python Functions - Interactive Examples# Learn function definition, parameters, and lambda expressions# === CALCULATOR BUILDER ===# Examples of different parameter typesdef add(a, b):"""Sum of two numbers"""return a + bdef power(base, exponent=2):"""Base raised to exponent power"""return base ** exponentdef calculate_average(*numbers):"""Average of all numbers"""return sum(numbers) / len(numbers) if numbers else 0# === TEXT PROCESSOR ===# Examples of *args and **kwargsdef process_text(*args, **kwargs):"""Flexible text processing with *args and **kwargs"""# args contains all positional argumentstext = ' '.join(str(arg) for arg in args)# kwargs contains all keyword argumentsif kwargs.get('capitalize'):text = text.upper()if kwargs.get('reverse'):text = text[::-1]return textdef format_names(*names, **options):"""Format multiple names with optional title and case"""formatted = []for name in names:result = nameif 'title' in options:result = f"{options['title']} {result}"if options.get('uppercase'):result = result.upper()formatted.append(result)return ', '.join(formatted)# === LAMBDA WORKSHOP ===# Lambda expressions and functional programmingsquare = lambda x: x ** 2# Square a numberis_even = lambda x: x % 2 == 0# Check if number is evenstring_length = lambda s: len(s)# Get string length# Using lambdas with built-in functionsnumbers = [1, 2, 3, 4, 5]squared = list(map(lambda x: x ** 2, numbers))evens = list(filter(lambda x: x % 2 == 0, numbers))# Higher-order function exampledef apply_operation(func, values):"""Apply a function to a list of values"""return [func(x) for x in values]# Using with lambdaresults = apply_operation(lambda x: x * 3, [1, 2, 3])# Results: [3, 6, 9]# Key Concepts:# - def keyword for function definition# - Positional, keyword, and default parameters# - *args for variable positional arguments# - **kwargs for variable keyword arguments# - lambda expressions for simple functions# - Higher-order functions and functional programming
No operations yet
Start by adding or modifying elements