def return_42(): return 42 # An explicit return statement print(return_42()) # The caller code gets 42 num = return_42() print(num) print(return_42() * 2) print(return_42() + 5) def get_even(numbers): even_nums = [num for num in numbers if not num % 2] return even_nums num = get_even([1, 2, 3, 4, 5, 6]) print(num) def mean(sample): return sum(sample) / len(sample) num = mean([1, 2, 3, 4]) print(num) def add_one(x): # No return statement at all result = x + 1 value = add_one(5) print(value) return_value = print("Hello, World") print(return_value) def print_greeting(): print("Hello, World") print_greeting() def return_greeting(): return "Hello, World" print(return_greeting()) def add(a, b): result = a + b return result print(add(2, 2)) import statistics as st def describe(sample): return st.mean(sample), st.median(sample), st.mode(sample) sample = [10, 2, 4, 7, 9, 3, 9, 8, 6, 7] mean, median, mode = describe(sample) print(mean) print(median) print(mode) desc = describe(sample) print(desc) print(type(desc)) print(divmod(15, 3)) print(divmod(8, 3)) def omit_return_stmt(): # Omit the return statement pass print(omit_return_stmt()) def bare_return(): # Use a bare return return print(bare_return()) def return_none_explicitly(): # Return None explicitly return None print(return_none_explicitly()) def variance(data, ddof=0): mean = sum(data) / len(data) return sum((x - mean) ** 2 for x in data) / (len(data) - ddof) print(variance([3, 4, 7, 5, 6, 2, 9, 4, 1, 3])) def variance(data, ddof=0): n = len(data) mean = sum(data) / n total_square_dev = sum((x - mean) ** 2 for x in data) return total_square_dev / (n - ddof) print(variance([3, 4, 7, 5, 6, 2, 9, 4, 1, 3])) counter = 0 def increment(): global counter counter += 1 increment() print(counter) counter = 0 def increment(var): return var + 1 increment(counter) print(counter) # Explicitly assign a new value to counter counter = increment(counter) print(counter) def my_abs(number): if number > 0: return number elif number < 0: return -number print(my_abs(-15)) print(my_abs(15)) print(my_abs(0)) def my_abs(number): if number > 0: return number elif number < 0: return -number else: return 0 print(my_abs(-15)) print(my_abs(15)) print(my_abs(0)) def my_abs(number): if number < 0: return -number return number print(my_abs(-15)) print(my_abs(15)) print(my_abs(0)) def is_divisible(a, b): if not a % b: return True return False print(is_divisible(4, 2)) print(is_divisible(7, 4)) def is_divisible(a, b): return not a % b print(is_divisible(4, 2)) print(is_divisible(7, 4)) def both_true(a, b): return a and b print(both_true(1, 2)) def both_true(a, b): if a and b: return True return False print(both_true(1, 2)) print(both_true(1, 0)) def both_true(a, b): return True if a and b else False print(both_true(1, 2)) print(both_true(1, 0)) def both_true(a, b): return bool(a and b) print(both_true(1, 2)) print(both_true(1, 0)) def my_any(iterable): for item in iterable: if item: # Short-circuit return True return False print(my_any([0, 0, 1, 0, 0])) print(my_any([0, 0, 0, 0, 0])) def dead_code(): return 42 # Dead code print("Hello, World") print(dead_code()) def no_dead_code(condition): if condition: return 42 print("Hello, World") print(no_dead_code(True)) print(no_dead_code(False)) import statistics as st from collections import namedtuple def describe(sample): Desc = namedtuple("Desc", ["mean", "median", "mode"]) return Desc( st.mean(sample), st.median(sample), st.mode(sample), ) sample = [10, 2, 4, 7, 9, 3, 9, 8, 6, 7] stat_desc = describe(sample) print(stat_desc) # Get the mean by its attribute name print(stat_desc.mean) # Get the median by its index print(stat_desc[1]) # Unpack the values into three variables mean, median, mode = describe(sample) print(mean) print(mode) def by_factor(factor): def multiply(number): return factor * number return multiply double = by_factor(2) print(double(3)) print(double(4)) triple = by_factor(3) print(triple(3)) print(triple(4)) def by_factor(factor): return lambda number: factor * number double = by_factor(2) print(double(3)) print(double(4)) import time def my_timer(func): def _timer(*args, **kwargs): start = time.time() result = func(*args, **kwargs) end = time.time() print(f"Execution time: {end - start}") return result return _timer @my_timer def delayed_mean(sample): time.sleep(1) return sum(sample) / len(sample) print(delayed_mean([10, 2, 4, 7, 9, 3, 9, 8, 6, 7])) class Circle: def __init__(self, radius): self.radius = radius # Class implementation... class Square: def __init__(self, side): self.side = side # Class implementation... def shape_factory(shape_name, *args, **kwargs): shapes = {"circle": Circle, "square": Square} return shapes[shape_name](*args, **kwargs) circle = shape_factory("circle", radius=20) print(type(circle)) print(circle.radius) square = shape_factory("square", side=10) print(type(square)) print(square.side) def func(value): try: return float(value) except ValueError: return str(value) finally: print("Run this before returning") print(func(9)) print(func("one")) def gen(): yield 1 yield 2 return 3 g = gen() print(g) print(next(g)) print(next(g)) print(next(g))