Tag: python functions
Recursive Functions in Python
This tutorial will teach you about Python recursive functions and how to use them to simplify your code. A recursive function is one that calls itself until it stops. The fn() function below is a recursive function because it calls itself: The #… in the fn() function denotes other code. A recursive function must also have a condition that causes it to stop calling itself….
Functions in Python
In this tutorial, you will learn how to create Python functions using the def keyword. What is Function in Python? A named code block that performs a task or returns a value is referred to as a function. In some programs, you may need to repeat a task numerous times. You also don’t want to duplicate the code for the same task in several places….
Lambda Expressions in Python
This tutorial will teach you about Python lambda expressions and how to use them to write anonymous functions. Sometimes you’ll need to write a single-expression function. This function, however, must only be used once. It will also be obsolete to use the def keyword to declare that function. Python lambda expressions are useful in this situation. What are Lambdas in Python? Anonymous functions can be…
Default Parameters in Python
This tutorial will teach you how to use Python’s default parameters to make function calls easier. You can give a default value for each argument when you define a function. The following syntax is used to specify default values for parameters: The assignment operator (=) is used to specify default values (value2, value3,…) for each parameter in this syntax. When you call a function and…
Docstrings Functions in Python
This tutorial will teach you how to add documentation to a function using docstrings. Help() is a Python built-in function that displays a function’s documentation. The print() function documentation is demonstrated in the following example: The help() function can be used to display documentation for modules, classes, functions, and keywords. This tutorial only covers function documentation. Documenting functions with docstrings in Python You can use…
Keyword Arguments in Python
This tutorial will teach you about Python keyword arguments and how to use them to make function calls more visible. Let’s begin with a basic method for calculating the net price from the selling price and discount: The price and discount parameters are passed to the get net price() function. The following example explains how to use the get net price() function to get the…