O'Reilly logo

Python Recipes Handbook: A Problem-Solution Approach by Joey Bernard

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

© Joey Bernard 2016

Joey Bernard, Python Recipes Handbook, 10.1007/978-1-4842-0241-8_6

6. Functions

Joey Bernard

(1)Fredericton, New Brunswick, Canada

Any modern programming language needs to have some way of combining functionality into reusable chunks of code. In Python, one of the most basic units of reusable code is the function. In this chapter, you will look at how functions work within Python, as well as some places where they may do something surprising.

6-1. Creating Basic Functions

Problem

You want to create a basic function to handle a simple task, such as finding the square of two.

Solution

Within Python, you can use the built-in def keyword to create a new function.

How It Works

Listing 6-1 shows an example.

Listing 6-1. Defining a Basic Function ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required