How is a function declared in Python? | Basics

There 2 ways you can declare the function in Python. The first way is using the def keyword and another way is using keyword lambda.

def functionname(parameters):
   return [expression]

But They are also known as lambda functions. These functions are special functions that are used when we need to pass a function itself as an argument.

lambda arguments : expression

Example How is a function declared in Python

Simple examples.

Using def function_name():

In Python a function is defined using the def keyword:

def my_function():
    print("Hello from a function")



How is a function declared in Python

Define Anonymous functions

Lambda functions can take any number of arguments:

x = lambda a, b: a * b

print(x(5, 6))

Output: 30

Do comment if you have any doubts and suggestions on this Python basic topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.