From the course: Certified Entry-Level Python Programmer (PCEP-30-02) Cert Prep

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Modularize your code with functions

Modularize your code with functions

- [Presenter] Here, we have a code that is calculating the area of a rectangle. This is the length and the width of that rectangle, and it's calculating the area and then printing the area. And after that, it's calculating the area of a circle using the radius and an approximation of the pi value. If we think about this code, we can find two very specific problems that we are solving that we can modularize with functions. So first, what's a function? A function in Python, it's just a block of code that we group with a name. And to create a function, we use the keyword def. We are defining a block of code with a name and then the name. For example, I can use my_function as a name and always after the identifier of the function that follows very similar rules to the identifiers of variables. I'm going to use a colon. And as we know, every time we have a colon, the next line should start with a code block, and that…

Contents