Jun 19, 2026 · 2 min read

Mastering Python Functions and Collections

Welcome to Python 2025-2026. Today, we'll explore functions and built-in collections. Learn how to write reusable functions and work with fundamental collections.

Introduction to Functions

To define a function in Python, use the def keyword followed by the function name and parameters in parentheses. Then, write the function body indented under the function definition. For example, we can define a greet function that takes a name as a parameter and prints out a greeting message.

def greet(name):
    print('Hello, ' + name)

greet('John')

Function Parameters and Return Values

Functions can take any number of parameters, including zero. They can also return values using the return statement.

def add(a, b):
    return a + b

result = add(2, 3)
print(result)  # Outputs: 5

Built-in Collections

Python's built-in collections include lists, dictionaries, and sets. These collections can be used to store and manipulate data.

Lists

Lists are ordered collections of items. You can create a list by placing items in square brackets [].

my_list = [1, 2, 3]
print(my_list[0])  # Outputs: 1

Dictionaries

Dictionaries are unordered collections of key-value pairs. You can create a dictionary by placing key-value pairs in curly brackets {}.

my_dict = {'a': 1, 'b': 2}
print(my_dict['a'])  # Outputs: 1

Sets

Sets are unordered collections of unique items. You can create a set by placing items in curly brackets {}.

my_set = {1, 2, 3}
print(len(my_set))  # Outputs: 3

Best Practices

Use functions and collections to write efficient code. Avoid using global variables and instead pass data as parameters to functions.

def calculate_area(width, height):
    return width * height

area = calculate_area(4, 5)
print(area)  # Outputs: 20

By following these best practices and mastering functions and built-in collections, you'll be able to write more efficient and effective Python code.

Related Posts

Jun 22, 2026

Packaging Made Easy: A Step-by-Step Guide to Using Setuptools and Twine for PyPI

Simplify Python project packaging with setuptools and Twine for PyPI

Jun 22, 2026

Mastering Isolated Python Environments with Venv and Pip

Learn to create isolated Python environments with venv and manage packages with pip

Jun 20, 2026

Mastering Error Handling in Python with Try/Except Blocks

Learn to use try/except blocks to handle errors in Python

Jun 20, 2026

Mastering OOP Basics in Python: Classes, Inheritance, and the Factory Pattern

Learn Python OOP fundamentals—defining classes, adding methods, inheritance, and a simple Factory pattern—in a concise, example‑driven guide.