27
Lesson 11 of 44

Functions

In this lesson, you will learn how to develop functions in Python programming. You will discover how to use and create various types of functions, from methods and functions with parameters to functions that return values. Enhance your skills through practical exercises and master the art of functions in programming.

  • 8 theory articles
  • 19 practice exercises
Start lesson

Fundamentals concepts 8

What is a function

A function is a named block of code that you can run as many times as you want. You have already used functions without knowing…

Theory
Read more

Parameters and arguments

A function that always does the same is not very useful. With parameters, you can give it different data each time.

Theory
Read more

Returning values with return

Until now functions showed things with print . The most usual thing, however, is for a function to calculate a result and return…

Theory
Read more

Functions with texts, numbers and lists

The exercises in the lesson follow a few patterns. We review them with an example function for each.

Theory
Read more

Variable scope and documentation

A variable created inside a function only exists inside it. Understanding where each variable lives avoids many mistakes.

Theory
Read more

main, command-line arguments and exit codes

The last two exercises of the lesson take the program away from the keyboard: they receive the data from the command line and…

Theory
Read more

Common mistakes with functions

These are the most frequent mistakes when starting with functions.

Theory
Read more

Documenting functions, default values and pure functions

Writing a function that works is the first step; the second is that other people (and you in a month) can use it without reading…

Theory
Read more

Practice exercises 19

Function greeting and farewell

Create a Python program that defines two functions: one to greet and another to say goodbye. Then, call both functions from the…

Exercise Easy
Solve it

Greet and bid farewell with parameters

Create a Python program that defines two functions to greet and say goodbye. The exercise is similar to *Function greeting and…

Exercise Easy
Solve it

Replace character in a string

Create a Python program that defines a function called change_letter() to modify a character of a text. The function receives as…

Exercise Medium
Solve it

Check alphabetical string

Create a Python program that defines the function is_alphabetic() to check whether a text entered by the user contains only…

Exercise Medium
Solve it

Check numeric string

Create a Python program with a function called is_number() that receives a text and determines whether it represents a number or…

Exercise Medium
Solve it

Function count spaces of a text

Develop a Python program with a function called count_spaces that receives a text and returns the total number of spaces it…

Exercise Medium
Solve it

Function to sum the digits of a number

Develop a Python program that asks the user for a number and stores it as text. Then, implement a function called sum_digits that…

Exercise Medium
Solve it

Return value from Main

Create a new version of the program *Calculator with command-line parameters* that finishes with the following exit codes:

Exercise Medium
Solve it

Function to write format text

Create a Python program with a function called write_centered_underlined that receives a text and displays it centered and…

Exercise Medium
Solve it

Find the minimum and maximum in an array

Create a Python program with a function called find_min_max() that receives a list of real numbers and returns its minimum and…

Exercise Medium
Solve it

Function to add an array of integers

Create a Python program that asks the user for 5 integers and stores them in a list. Then, implement a function called…

Exercise Medium
Solve it

Function to show an inverted string

Create a Python program with a function called reverse_string that receives a text and returns it in reverse order.

Exercise Medium
Solve it

Function to check palindrome

Create a Python program with a function called is_palindrome() that checks whether a text is a palindrome. A palindrome is a text…

Exercise Medium
Solve it

Search the greatest value in an array

Create a Python program with a function called find_largest_value that receives a list of 5 real numbers requested from the user…

Exercise Medium
Solve it

Function to calculate the power of a number

Create a Python program with a function called calculate_power() that calculates the result of raising an integer to the power of…

Exercise Medium
Solve it

Function to write text with title format

Create a Python program with a function called write_title_format() that writes a text centered on the screen, in uppercase, with…

Exercise Medium
Solve it

Interest with default values

A parameter can have a default value : if that argument is not given when calling the function, the default is used. Arguments…

Exercise Easy
Solve it

Seconds to hours and minutes

A function can return several values at once: it actually returns a tuple, which can be unpacked into several variables when…

Exercise Easy
Solve it

Function composition

Functions are values: they can be stored in a dictionary, passed to another function and returned from a function. With that…

Exercise Medium
Solve it