8 – Conclusion

Congratulations on completing this lesson on functions. You learned about functions, variable scope, documentation, lambda expressions, iterators, and generators. Next, you’ll learn how to write scripts in local environments with many functions pieced together. That’s a lot of stuff. It’s okay if you haven’t mastered everything yet. You’ve already accomplished a ton by getting this … Read more

7 – Iterators And Generators16

If you recall from the previous lesson, iterables are objects that can return one of its elements at a time. Lists are one of the most common iterables you have used. It turns out many of the built-in functions we’ve used so far, such as enumerate, returns something called an iterator. An iterator is an … Read more

7 – Iterators And Generators

If you recall from the previous lesson, iterables are objects that can return one of its elements at a time. Lists are one of the most common iterables you have used. It turns out many of the built-in functions we’ve used so far, such as enumerate, returns something called an iterator. An iterator is an … Read more

6 – L4 08 Lambda Expressions V3

In Python, you can use lambda expressions to create anonymous functions. That is, functions that don’t have a name. They are very helpful for creating quick functions that aren’t really needed later in your code. Later, we’ll learn about higher-order functions. Are functions that take in other functions as arguments, where lambda expressions become especially … Read more

5 – Documentation

One of the key advantages of functions is that they can help break a program down into smaller chunks. This makes code easier to write and also easier to read because the pieces of your code, the functions, are reusable. If a program needs to calculate volumes of multiple cylinders, it can call the cylinder … Read more

4 – Variable Scope

Scope refers to which parts of a program a variable can be referenced or used from. If a variable is created inside a function, it can only be used within that function. Consider these two functions, word count and nearest square. The first function uses answer to count words in a document. The second function … Read more

3 – Default Arguments

Let’s revisit the cylinder volume function, but with one modification. This function includes a default argument. If radius is not specified, then the variable radius will default to five when used in the function body. Calling the function like this would be the same thing as calling it like this, because radius is set to … Read more

2 – Defining Functions6

As our first function, we’ll write a function that calculates the volume of a cylinder. The formula for this is the cylinder’s height, multiplied by the square of its radius, multiplied by pi. Here’s what that formula looks like when defined in a function called cylinder volume. After defining the cylinder volume function, we can … Read more

2 – Defining Functions

As our first function, we’ll write a function that calculates the volume of a cylinder. The formula for this is the cylinder’s height, multiplied by the square of its radius, multiplied by pi. Here’s what that formula looks like when defined in a function called cylinder volume. After defining the cylinder volume function, we can … Read more

1 – Introduction

Welcome to this lesson on functions. Previously, we used several of Python’s built-in functions. Here, we will write functions of our own. Functions are useful chunks of code that allow you to encapsulate a task. Encapsulation is a way to carry out a whole series of steps with one simple command. For example, imagine you … Read more

3-1-4-19. Further Learning

Further Learning If you want to learn more about writing functions, check out this talk from PyCon by Jack Diederich. Diederich covers best practices for writing functions in Python that also apply to all code in Python. Here’s a great blog post about yield and generators from Jeff Knupp. https://jeffknupp.com/blog/2013/04/07/improve-your-python-yield-and-generators-explained/

3-1-4-18. Conclusion

Congratulations on completing this lesson on functions. You learned about functions, variable scope, documentation, lambda expressions, iterators, and generators. Next, you’ll learn how to write scripts in local environments with many functions pieced together. That’s a lot of stuff. It’s okay if you haven’t mastered everything yet. You’ve already accomplished a ton by getting this … Read more

3-1-4-15. [Optional] Quiz: Iterators and Generators

Quiz: Implement my_enumerate Write your own generator function that works like the built-in function enumerate. Calling the function like this: should output: Quiz: Chunker If you have an iterable that is too large to fit in memory in full (e.g., when dealing with large files), being able to take and use chunks of it at a time … Read more

3-1-4-12. Quiz: Lambda Expressions

Quiz: Lambda with Map map() is a higher-order built-in function that takes a function and iterable as inputs, and returns an iterator that applies the function to each element of the iterable. The code below uses map() to find the mean of each list in numbers to create the list averages. Give it a test run to see what happens. Rewrite … Read more

3-1-4-11. Lambda Expressions

In Python, you can use lambda expressions to create anonymous functions. That is, functions that don’t have a name. They are very helpful for creating quick functions that aren’t really needed later in your code. Later, we’ll learn about higher-order functions. Are functions that take in other functions as arguments, where lambda expressions become especially … Read more