Category: 4. Functions
-
Python Function Docstrings
Summary: in this tutorial, you’ll learn about how to use docstrings to add documentation to a function. Introduction to the help() function Python provides a built-in function called help() that allows you to show the documentation of a function. The following example shows the documentation of the print() function: Output: Note that you can use the help() function to show the documentation…
-
Python Lambda Expressions
Summary: in this tutorial, you’ll learn about Python lambda expressions and how to use them to write anonymous functions. Sometimes, you need to write a simple function that contains one expression. However, you need to use this function once. And it’ll unnecessary to define that function with the def keyword. That’s where the Python lambda expressions come into play.…
-
Python Recursive Functions
Summary: in this tutorial, you’ll learn about Python recursive functions and how to use them to simplify your code. Introduction to recursive functions A recursive function is a function that calls itself until it doesn’t. The following fn() function is a recursive function because it has a call to itself: In the fn() function, the #… means other code. Also, a recursive function…
-
Python Keyword Arguments
Summary: in this tutorial, you’ll learn about the Python keyword arguments, and how to use them to make function calls more obvious. Introduction to the Python keyword arguments Let’s start with a simple function that calculates the net price from the selling price and discount: The get_net_price() function has two parameters: price and discount. The following shows how to call the get_net_price() function to…
-
Python Default Parameters
Summary: in this tutorial, you’ll learn about the Python default parameters to simplify function calls. Introduction to Python default parameters When you define a function, you can specify a default value for each parameter. To specify default values for parameters, you use the following syntax: In this syntax, you specify default values (value2, value3, …) for each…
-
Python Functions
Summary: in this tutorial, you’ll learn to develop Python functions by using the def keyword. What is a function A function is a named code block that performs a job or returns a value. Why do you need functions in Python Sometimes, you need to perform a task multiple times in a program. And you don’t want…