05 - Python Functions
Python Functions
Functions
Reusable blocks of code to divide complex tasks into manageable, simple steps. This brings re-usability, organization, and repeatable, standard processes into our programs.
User defined functions
Function definition syntax:
def function_name (parameters):
code block here
Example:
# Your code here
# Step 1: Define the function
def greet():
print("Hello, Python Coder!")
greet()
# Step 2: Add variables for names and times
def greet_personalized(name, time_of_day = "day"):
print(f"Good {time_of_day.capitalize()}, {name}")
greet_personalized("Alice")
# Step 3: Loop through names and times & print the function output
names = ["Alice", "Bob", "Charlie"]
times = ["morning", "afternoon", "evening"]
for i in range(len(names)):
# print(names[i])
greet_personalized(names[i],times[i])
Generators
A special type of Python function that generates values one at a time, only when called, using the yield keyword instead of the return keyword. They're extremely memory efficient for handling large amounts of data in streams or chunks.
A generator "yields" one item at a time and then pauses until the next item is requested. Generators keep their state in memory so it knows where it was in the generation process after the prior yield.
To get the next item from a generator, we use the next(generator_object) function to create or retrieve it.
Example:
# define function
def bake_cookies(batch_size, max_batches=None):
batch_count = 0
total_cookies = 0
# loop through batches
while max_batches is None or batch_count < max_batches:
total_cookies += batch_size
yield total_cookies
batch_count += 1
# retrieve data from generator function\
# bake_cookies_generator_object = bake_cookies(100)
next(bake_cookies_generator_object)
Managing exceptions
Exceptions are used to handle unexpected errors in our code so that our program isn't disrupted.