Functions

Learn how to create and use functions in Python!

StartPython Illustration
  1. 1

    You might have considered the situation where you would like to reuse a piece of code, just with a few different values. Instead of rewriting the whole code, it's much cleaner to define a *function...

  2. 2

    Functions are defined with three components: 1. The header, which includes the [...] keyword, the name of the function, and any parameters the function requires. Here's an example: [.....

  3. 3

    After defining a function, it must be called to be implemented. In the previous exercise, [...] in the last line told the program to look for the function called [...] and execute the code in...

  4. 4

    Let's take another look at the definition of the function [...] from the previous exercise: [...] Here, [...] is a parameter of [...] . A parameter is a variable that is an input to a fu...

  5. 5

    We've seen functions that can print text or do simple arithmetic, but functions can be much more powerful than that. For example, a function can call another function: [...]

  6. 6

    Let's create a few more functions just for good measure. [...] The example above is just there to help you remember how functions are structured. Don't forget the colon at the end of your func...

  7. 7

    Remember [...] from the first exercise in this course? That was an example of importing a module. A module is a file that contains definitions—including variables and functions—that you can u...

  8. 8

    Did you see that? Python said: [...] Python doesn't know what square roots are—yet. There is a Python module named [...] that includes a number of useful variables and functions, and [...] i...

  9. 9

    Nice work! Now Python knows how to take the square root of a number. However, we only really needed the [...] function, and it can be frustrating to have to keep typing [...] . It's possible t...

  10. 10

    Great! We've found a way to handpick the variables and functions we want from modules. What if we still want all of the variables and functions in a module but don't want to have to constantly typ...

  11. 11

    Universal imports may look great on the surface, but they're not a good idea for one very important reason: they fill your program with a ton of variable and function names without the safety of ...

  12. 12

    Now that you understand what functions are and how to import modules, let's look at some of the functions that are built in to Python (no modules required!). You already know about some of the b...

  13. 13

    The [...] function takes any number of arguments and returns the largest one. ("Largest" can have odd definitions here, so it's best to use [...] on integers and floats, where the results are s...

  14. 14

    [...] then returns the smallest of a given series of arguments.

  15. 15

    The [...] function returns the absolute value of the number it takes as an argument—that is, that number's distance from [...] on an imagined number line. For instance, [...] and [...] ...

  16. 16

    Finally, the [...] function returns the type of the data it receives as an argument. If you ask Python to do the following: [...] Python will output: [...]

  17. 17

    Okay! Let's review functions. [...] Again, the example code above is just there for your reference!

  18. 18

    Good work! Now let's see what you remember about importing modules (and, specifically, what's available in the [...] module).

  19. 19

    Perfect! Last but not least, let's review the built-in functions you've learned about in this lesson. [...]

  1. 1

    Let's first quickly review functions in Python. [...] In the example above: 1. We define a function called [...] that has two arguments called [...] and [...] . 2. Then, we print out the ...

  2. 2

    When planning a vacation, it's very important to know exactly how much you're going to spend. [...] The above example is just a refresher in how functions are defined. Let's use functions to c...

  3. 3

    You're going to need to take a plane ride to get to your location. [...] 1. The example above defines the function [...] that accepts a string as the argument [...] . 2. The function returns...

  4. 4

    You're also going to need a rental car in order for you to get around. [...] In the above example, we first give the player 10 tickets for every point that the player scored. Then, we check the...

  5. 5

    Great! Now that you've got your 3 main costs figured out, let's put them together in order to find the total cost of your trip. [...] 1. We define two simple functions, [...] and [...] that...

  6. 6

    You can't expect to only spend money on the plane ride, hotel, and rental car when going on a vacation. There also needs to be room for additional costs like fancy food or souvenirs.

  7. 7

    Nice work! Now that you have it all together, let's take a trip. What if we went to Los Angeles for 5 days and brought an extra 600 dollars of spending money?

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo

Functions

StartPython Illustration