www
greetings
 

WARNING

This website contains content that is intended for a discerning audience.
You are here:

Functions and Modules in Python

Functions are reusable blocks of code that perform a specific task. They help organize code and avoid repetition.


Defining Functions

Use the def keyword to define a function:

def greet(name):
    return f"Hello, {name}!"

message = greet("World")
print(message)  # Output: Hello, World!

Function Parameters

Default Parameters

def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

*args and **kwargs

def sum_all(*args):
    return sum(args)

def print_info(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

Lambda Functions

Lambda functions are small anonymous functions:

square = lambda x: x ** 2
add = lambda a, b: a + b

Modules

Modules are files containing Python code that can be imported into other programs.

Importing Modules

import math
import random
from datetime import datetime
from os import path

Standard Library Modules

Python's standard library includes many useful modules:


Creating Your Own Modules

Save a Python file and import it:

# mymodule.py
def say_hello():
    return "Hello!"

# main.py
import mymodule
print(mymodule.say_hello())