Book Image

Python Fundamentals

By : Ryan Marvin, Mark Ng’ang’a, Amos Omondi
Book Image

Python Fundamentals

By: Ryan Marvin, Mark Ng’ang’a, Amos Omondi

Overview of this book

<p>After a brief history of Python and key differences between Python 2 and Python 3, you'll understand how Python has been used in applications such as YouTube and Google App Engine. As you work with the language, you'll learn about control statements, delve into controlling program flow and gradually work on more structured programs via functions.</p> <p>As you settle into the Python ecosystem, you'll learn about data structures and study ways to correctly store and represent information. By working through specific examples, you'll learn how Python implements object-oriented programming (OOP) concepts of abstraction, encapsulation of data, inheritance, and polymorphism. You'll be given an overview of how imports, modules, and packages work in Python, how you can handle errors to prevent apps from crashing, as well as file manipulation.</p> <p>By the end of this book, you'll have built up an impressive portfolio of projects and armed yourself with the skills you need to tackle Python projects in the real world.</p>
Table of Contents (12 chapters)
Python Fundamentals
Preface

Anonymous Functions


Anonymous functions in Python are also called lambda functions. This is because they use the keyword lambda in their definition.

Anonymous functions are so called because, unlike all of the other functions that we have looked at up to this point, they do not require to be named in their definition. The functions are usually throwaway, meaning that they are only required where they are defined, and are not to be called in other parts of the codebase.

The syntax of an anonymous function is as follows:

lambda argument_list: expression

The argument list consists of a comma-separated list of arguments, and the expression is an arithmetic expression that uses these arguments. You can assign the function to a variable to give it a name.

Exercise 20: Creating a Lambda Function

Let's look at a practical application of a lambda function. Suppose that you want to simply sum up two numbers in a function. There is no need to define a whole user-defined function for this if you are only...