Functions and Modules

This lesson introduces you to functions and modules in Python, fundamental concepts for writing organized and reusable code. You will learn how to define and use functions to perform specific tasks, and how to leverage modules to access pre-built functionalities.

Learning Objectives

  • Define and call functions with and without parameters.
  • Understand the concept of scope (local vs. global variables) within functions.
  • Import and utilize modules to access pre-written Python code.
  • Explain the benefits of using functions and modules for code organization and reusability.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to Functions

Functions are blocks of reusable code that perform a specific task. They make your code more organized, readable, and efficient by breaking down complex problems into smaller, manageable pieces.

Defining a Function:
Use the def keyword followed by the function name, parentheses (), and a colon :. Code to be executed is indented.

def greet():
    print("Hello, world!")

Calling a Function:
To execute a function, simply use its name followed by parentheses ().

greet() # Output: Hello, world!

Functions with Parameters:
You can pass values (parameters/arguments) to functions to make them more flexible.

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

greet_name("Alice") # Output: Hello, Alice!

Return Values and Scope

Functions can return values using the return keyword. This allows you to get results back from the function.

def add_numbers(x, y):
    sum = x + y
    return sum

result = add_numbers(5, 3)
print(result) # Output: 8

Scope:
Scope refers to the accessibility of variables. Variables defined inside a function (local variables) are only accessible within that function. Variables defined outside functions (global variables) are accessible throughout the entire program.

global_variable = 10  # Global variable

def my_function():
    local_variable = 5  # Local variable
    print(global_variable) # Accessing global variable is ok
    print(local_variable)

my_function()
#print(local_variable) # This will cause an error (NameError: name 'local_variable' is not defined)

Introduction to Modules

Modules are pre-written Python files that contain functions, classes, and variables. They allow you to reuse code and extend Python's functionality.

Importing Modules:
Use the import keyword followed by the module name.

import math

print(math.sqrt(16)) # Output: 4.0

Using Module Functions:
Access functions and variables within a module using the dot notation (module_name.function_name).

import random

random_number = random.randint(1, 10)
print(random_number) # Output: a random number between 1 and 10

Importing Specific Functions:
You can import specific functions from a module using from module_name import function_name.

from math import sqrt

print(sqrt(25)) # Output: 5.0

Importing all functions (Use with Caution): You can use from module_name import *. However, it’s generally not recommended because it can lead to namespace conflicts.

Progress
0%