Mastering Python Functions in Less Than 5 Minutes

You will know everything about Python Functions after reading this article.

Alain Saamego
4 min readMay 7, 2022

A function is a block of code that takes an input, does some processing, and then returns an output. Functions are written in the same language as the code that calls them.

In Python, a function is defined by a keyword, the keyword def.

def my_function():
print(“Hello, world!”)
my_function()

When you run the code above, the output will be:

Hello, world!

The code above defines a function named my_function. The function doesn’t take any arguments, and when called, it just prints Hello, world!.

Let’s look at another example:

def my_function(x):
return x * 2
print(my_function(3))print(my_function(5))print(my_function(9))

When you run the code above, the output will be:

6
10
18

--

--