Introduction to Functions and Modules
What is a Function?
A function is a named block of code that performs a specific task. Functions help you organize your code, avoid repetition, and make your programs easier to read and maintain.
Why use functions?
- Break big problems into smaller, manageable pieces
- Reuse code without copying and pasting
- Make your code easier to test and debug
Example:
def greet_user():
name = input("What is your name? ")
print(f"Hello, {name}!")
greet_user() # Call the function
Defining and Calling Functions
- Use the
defkeyword to define a function. - Give your function a name (use lowercase and underscores).
- Use parentheses
()for parameters (inputs). - End the definition with a colon
:. - Indent the code inside the function.
Example:
def add(a, b):
return a + b
result = add(3, 5)
print(result) # Output: 8
Parameters and Arguments
- Parameters are variables listed in the function definition (
a,babove). - Arguments are the actual values you pass when calling the function (
3,5above).
Return Values
- Use
returnto send a value back from the function. - If you don’t use
return, the function returnsNoneby default.
Example:
def square(x):
return x * x
print(square(4)) # Output: 16
Organizing Code with Functions
Functions make it easy to:
- Group related code together (e.g., all code for practicing flashcards)
- Separate different actions (e.g., showing the menu, adding a card, checking answers)
- Test parts of your program independently
What is a Module?
A module is a file containing Python code (functions, variables, classes) that you can use in other programs. Modules help you organize large projects by splitting code into multiple files.
Why use modules?
- Keep your code organized and manageable
- Reuse code across different projects
- Share code with others
Example:
Suppose you have a file called flashcard_data.py:
# flashcard_data.py
flashcards = {"Dog": "Hund", "Cat": "Katze"}
def get_flashcards():
return flashcards
You can use this module in your main program:
import flashcard_data
cards = flashcard_data.get_flashcards()
print(cards)
Importing Modules
- Use
import module_nameto bring in a module. - Use
from module_name import function_nameto import specific functions.
Examples:
import math
print(math.sqrt(16)) # Output: 4.0
from random import choice
print(choice([1, 2, 3])) # Randomly prints 1, 2, or 3
← Back to where you were | → Start the course from the beginning
Using Third-Party Libraries
A library is a collection of modules written by others that you can use in your projects. For example, tkinter is a library for building graphical user interfaces (GUIs).
Example:
import tkinter as tk
window = tk.Tk()
window.title("Flashcard App")
window.mainloop()
← Back to where you were | → Start the course from the beginning
Summary
- Functions let you organize, reuse, and test your code.
- Modules help you split your code into multiple files for better organization.
- Libraries are collections of modules you can use to add powerful features to your programs.
- Using functions and modules is essential for building larger, more maintainable Python projects.
