Functions and methods in Python

Functions and methods are used to design or make a logical unit of code that can be reused throughout the course of your script or other scripts. Functions actually form the basis of code reuse and bring modularity to the code structure. They keep the code clean and easier to modify.

It is advisable to always try to break our logic into small units of code, each of which is a function. We should try to keep the size of the method small in terms of the lines of code whenever possible.

The following code represents the basic syntax of defining methods in Python:

def print_message(message):    print(message)    statement 2    statement 

Python methods do not have a return type in their definition as you might have seen ...

Get Hands-On Penetration Testing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.