Functions, Modules and Packages

Functions, Modules and Packages

What are functions?

In programming, a function is a reusable block of code that performs a specific task or a set of tasks. Functions allow you to break down a program into smaller, more manageable pieces, making your code modular, organized, and easier to understand. Functions can take input parameters, perform computations, and return results.

Here are some key concepts related to functions:

Function definition

  • A function is defined using the def keyword in Python.

  • Example:

def greet(name):
    print("Hello, " + name + "!")

greet("John")
# Output: Hello, John!

Parameters

  • Functions can accept input values called parameters.

  • Parameters are specified in the function definition and act as placeholders for the actual values passed when the function is called.

  • Example:

def add_numbers(a, b):
    result = a + b
    return result

sum_result = add_numbers(5, 3)
print("Sum:", sum_result)
# Output: Sum: 8

Return Statement

  • Functions can return values using the return statement.

  • The return statement ends the function's execution and sends a result back to the caller.

  • Example:

def multiply(a, b):
    result = a * b
    return result

product = multiply(4, 7)
print("Product:", product)
# Output: Product: 28

Function call

  • To execute a function, you call it by its name followed by parentheses.

  • Arguments passed in the parentheses are values for the function's parameters.

  • Example:

def square(num):
    return num ** 2

squared_result = square(3)
print("Square:", squared_result)
# Output: Square: 9

Real time example

Let us look at a real time example of using Python from DevOps point of view.

Let's say you are given a task to create S3 bucket and EC2 instance. You will be writing functions in Python using a module called boto3.

What are Modules?

In programming, a "module" is a file containing Python code that defines functions, classes, and variables. Modules serve as a way to organize code into reusable and logically grouped units. They provide a means to structure large programs and promote code reuse.

Their main purpose is to serve re-usability. By default, every python program is a module.

We can say that it is a collection of functions.

Example:

Suppose you have a Python file named my_module.py:

# my_module.py
def square(x):
    return x ** 2

pi = 3.14159265

You can use this module in another script:

import my_module

result = my_module.square(5)
print(result)
print(my_module.pi)

# Output:
# 25
# 3.14159265

In this case, my_module is a Python module containing the square function and a variable pi.

We can also import only one function from a module. Here is how:

# File: my_module.py

def addition(x, y):
    return x + y

def subtraction(x, y):
    return x - y
# File: main_program.py

from my_module import addition

# Call the addition function
result = addition(5, 3)
print(result)

What are Packages?

A package is a collection of modules organized in directories. Packages help you organize related modules into a hierarchy. They contain a special file named __init__.py, which indicates that the directory should be treated as a package.

Example:

Suppose you have a package structure as follows:

my_package/
    __init__.py
    module1.py
    module2.py

You can use modules from this package as follows:

from my_package import module1

result = module1.function_from_module1()

In this example, my_package is a Python package containing modules module1 and module2.

A package can also be called as a library.

Now, you must we wondering where does all the python packages and modules reside?

They are at PyPI (Python Package Index).

Virtual Environment

In Python, a virtual environment is a self-contained directory tree that contains a Python interpreter and a set of additional packages.

It allows you to create an isolated environment for your Python projects, ensuring that dependencies for one project do not interfere with dependencies for another. Virtual environments help manage project-specific dependencies, avoid conflicts, and provide a clean and reproducible environment for development.

Suppose, you are working with two development teams and they want Jira version latest and another want Jira version 1.9.88. In this case, you will create two virtual environments.

Here are some commands related to virtual environments in Python.

To install virtual environment,

pip install virtualenv

To create a virtual environment,

python -m venv venv_name

To activate the virtual environment you created,

# On Windows,
.\venv_name\Scripts\activate

# On Linux/Mac,
source venv_name/bin/activate

To deactivate the virtual environment,

deactivate

What's next?

In the next article in this blog series we will look into Command line arguments and environment variables.

Stay tuned.