Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learn Python by Building Data Science Applications

You're reading from  Learn Python by Building Data Science Applications

Product type Book
Published in Aug 2019
Publisher Packt
ISBN-13 9781789535365
Pages 482 pages
Edition 1st Edition
Languages
Authors (2):
Philipp Kats Philipp Kats
Profile icon Philipp Kats
David Katz David Katz
Profile icon David Katz
View More author details

Table of Contents (26) Chapters

Preface Section 1: Getting Started with Python
Preparing the Workspace First Steps in Coding - Variables and Data Types Functions Data Structures Loops and Other Compound Statements First Script – Geocoding with Web APIs Scraping Data from the Web with Beautiful Soup 4 Simulation with Classes and Inheritance Shell, Git, Conda, and More – at Your Command Section 2: Hands-On with Data
Python for Data Applications Data Cleaning and Manipulation Data Exploration and Visualization Training a Machine Learning Model Improving Your Model – Pipelines and Experiments Section 3: Moving to Production
Packaging and Testing with Poetry and PyTest Data Pipelines with Luigi Let's Build a Dashboard Serving Models with a RESTful API Serverless API Using Chalice Best Practices and Python Performance Assessments Other Books You May Enjoy

Functions

At the end of the previous chapter, we solved a simple conversion problem, and while the problem was solved, it can be argued that the code we used wasn't exactly perfect; of course, it allowed us to change variable names (for instance, update hotel pricing), but it was still hard to read and error-prone. In addition, some particular elements of the code were repetitive, as we performed the same operations on different values.

This is exactly the opposite of one measure of code quality employed by programmers—Don't Repeat Yourself! (DRY) code—code that has no repeating parts. In other words, operations that we use multiple times should be articulated and defined once. This will allow us to keep the code short, concise, and expressive. It will be easier to maintain, debug, and change when needed. But how can this be achieved? First of all, it is...

Technical requirements

This chapter requires no additional packages to install. All the code is available in the Chapter03 folder on GitHub, https://github.com/PacktPublishing/Learn-Python-by-Building-Data-Science-Applications.

To access and run example notebooks for this and other chapters, open the folder you cloned from GitHub in your VS Code, then switch to its Terminal window (that way, you're guaranteed to be in the correct folder) and type jupyter lab. On running, the application will either open a JupyterLab Notebook in your default browser or print a link. Via this link, go to the Chapter03 folder and run the Functions notebook. Once the notebook is running, you're ready to proceed!

Understanding a function

What is a function anyway? In programming, a function is a named section of code that encapsulates a specific task and can be used relatively independently of surrounding code. Most (but not all) functions are stateless—their outcome depends solely on the function's explicit inputs.

Functions are ubiquitous in Python code. In fact, we have used some functions already; print is one example. Those functions are part of Python's default arsenal of built-in functions. There are 69 built-in functions in total in modern pandas. Before we start writing functions on our own, let's review these built-in functions first.

In the following sections, we will discuss just a handful of functions that we'll use frequently throughout the book; some others we'll discuss later. We have grouped all functions into four groups depending on the...

Defining the function

Now that we have reviewed built-in functions, you probably have some understanding of how to use them as a consumer. But how can a custom function be created? It's very simple! You just have to observe the following structure:

def function_name(arguments):
‘''Documentation string'''
# code inside, using arguments
return result

Let's break this down. Here, def is a reserved keyword that tells Python that we are going to create a function. After that comes the name of the function—following the same rules as variables. The name is followed by a parenthesis. If the function requires any argument we should state them here by name (you can think of them as new variables). If you don't anticipate any arguments, a parenthesis should be kept empty.

Functions do have access to variables and functions...

Refactoring the temperature conversion

In Chapter 2, First Steps in Coding – Variables and Data Types, we've performed a simple exercise by converting temperature from Fahrenheit to Celsius, and back. The approach was similar to how we'd use a calculator, except that we were able to store parameters beforehand, and then rerun the calculations for the new inputs. At this point, you can probably see that this is a great case for a separate function. So, let's refactor our code into a pair of functions:

def fahrenheit_to_celsius(temp):
CONST, RATIO = 32, 5/9
return (temp – CONST) * RATIO

def celsius_to_fahrenheit(temp):
CONST, RATIO = 32, 5/9
return (temp/RATIO) + CONST

Let's now test these function as follows:

>>> fahrenheit_to_celsius(100)
37.77777777777778

>>> celsius_to_fahrenheit(37.77777777777778)
100.0

>>>...

Understanding anonymous (lambda) functions

In some cases, it is convenient to declare simple one-time functions in place; for that, we can use lambdas – anonymous functions (they are anonymous in the sense that you won't store them in-memory, so there is no name for them). Lambdas use simplified syntax:

lambda <arguments>: <code> 

Note that there is no return keyword – lambda will return the result of the code automatically. It also does not accept multiline code.

Take a look at this example:

models = [
‘Bimbus-3000',
‘Nimbus-1000',
‘Timbus-2000'
]

sort(models, key=lambda x: x[-4:])

The output for the preceding code would be as follows:

>>> [‘Nimbus-1000',‘Timbus-2000', ‘Bimbus-3000']

Here, the array of strings are sorted – however, the string's alphanumeric...

Understanding recursion

Recursion is a process of internal, nested repetition. A well-known example of recursion are fractals, for example, the Sierpiński carpet – shapes repeat themselves while you keep zooming in. In the context of programming, recursion represents an ability of the function to call itself from its body. In some cases, this makes your code shorter and more expressive, as you can split complex problems into sets (Russian dolls?) of simple ones.

Consider an example of a factorial function (N! in math). A factorial of a value is a multiplicative of all numbers from 1 to the given one – for example, for 3, it will be 6: 1 * 2 * 3. The following is one way to compute a factorial through recursion:

def factorial(n):
if n == 1:
return n

return n * factorial(n-1)

Here, the factorial of 1 will always return 1. For any other positive...

Summary

In this chapter, we learned one of the main pillars of code – functions. Functions allow us to write concise and expressive code that can be reused multiple times. Functions serve as building blocks of our programs – be it report generation, interaction with the APIs, or training the model. We discussed how to declare both standard and anonymous functions, how to set arguments, and their default values, and how to use args and kwargs for more flexible interface design. We also learned how to write good quality documentation strings and add type annotations. Finally, we did rewrite our code from the previous chapter, using functions, which made it slightly more expressive and error-prone.

In the last section, we defined recursion – an approach where a function is called from within itself, and which allows us to solve rather complex tasks with simple...

Questions

  1. What are functions, and when should we use them?
  2. How can data be provided to functions?
  3. What does indentation mean? Is it required?
  4. What should be covered in the docstring function? How can I read the docstring function?
  5. When could it be useful to use type annotations?
  6. How can a function be designed if I don't know the exact number of arguments or their names beforehand?
  7. What does anonymous function mean? When should they be used?

Further reading

lock icon The rest of the chapter is locked
You have been reading a chapter from
Learn Python by Building Data Science Applications
Published in: Aug 2019 Publisher: Packt ISBN-13: 9781789535365
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}