Functions and Modular Programming.

Functions and Modular Programming in Python

In this lab, you will learn about functions and modular programming in Python. Functions allow you to encapsulate code for reuse, making your programs more organized and easier to manage. You can perform the exercises in any of the following environments:

  • Visual Studio Code (VS Code)
  • Google Colab
  • Jupyter Notebook
  • PyCharm
  • Anaconda

Setup Instructions

1. Visual Studio Code (VS Code)

  • Installation: Ensure Python is installed and the Python extension is added to VS Code.
  • Create a New File: Open VS Code and create a new Python file (e.g., functions_lab.py).
  • Run the Code: Use the editor’s terminal or run button to execute your code.

2. Google Colab

  • Access Colab: Go to Google Colab.
  • Create a New Notebook: Click on “New Notebook.”
  • Run Cells: Write your code in a cell and run it by clicking the play button.

3. Jupyter Notebook

  • Installation: Install Jupyter Notebook via Anaconda or pip.
  • Start Jupyter: Open your terminal and type jupyter notebook to launch.
  • Create a New Notebook: Create a new Python notebook and write your code in the cells.

4. PyCharm

  • Installation: Download and install PyCharm.
  • Create a New Project: Start a new project and create a new Python file.
  • Run the Code: Write your code and run it using the built-in run configuration.

5. Anaconda

  • Launch Anaconda Navigator: Open Anaconda Navigator.
  • Choose an Environment: Launch either Jupyter Notebook or Spyder.
  • Create a New File/Notebook: Write your code in a new notebook or script.

Exercises

Exercise 1: Defining a Function

Task:

  1. Define a function that takes two numbers as arguments and returns their sum.

Example Code:

# Define a function to add two numbers
def add_numbers(a, b):
    return a + b

# Test the function
result = add_numbers(5, 3)
print("Sum:", result)

Exercise 2: Function with Default Arguments

Task:

  1. Define a function that greets a user. If no name is provided, it should default to “Guest.”

Example Code:

# Define a function to greet a user
def greet_user(name="Guest"):
    print(f"Hello, {name}!")

# Test the function
greet_user("Alice")
greet_user()

Exercise 3: Function with Variable Number of Arguments

Task:

  1. Define a function that takes any number of numbers and returns their average.

Example Code:

# Define a function to calculate the average of multiple numbers
def calculate_average(*args):
    return sum(args) / len(args) if args else 0

# Test the function
avg = calculate_average(10, 20, 30, 40)
print("Average:", avg)

Exercise 4: Modular Programming

Task:

  1. Create a module named math_operations.py that contains functions for addition and multiplication. Then, import this module in another script and use its functions.

Example Code for math_operations.py:

# math_operations.py

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

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

Example Code for Main Script:

# main.py

# Import the custom module
import math_operations as mo

# Use the functions from the module
sum_result = mo.add(5, 7)
product_result = mo.multiply(5, 3)

print("Sum:", sum_result)
print("Product:", product_result)

Exercise 5: Lambda Functions

Task:

  1. Create a lambda function that squares a number and use it to square a list of numbers.

Example Code:

# Define a lambda function to square a number
square = lambda x: x ** 2

# Square a list of numbers
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(square, numbers))

print("Squared Numbers:", squared_numbers)

Running the Code

  1. Copy the Code: Copy the code examples provided above into your Python file.
  2. Run the Program: Execute the program in your IDE. You can usually do this by clicking a “Run” button or using a keyboard shortcut (like Shift + F10 in PyCharm).
  3. Interact with the Program: Follow the prompts in the console to input your data if applicable.

Conclusion

This lab helps you understand functions and modular programming in Python. By completing these exercises, you will gain practical experience in defining functions, using default arguments, handling variable arguments, creating modules, and using lambda functions. This knowledge is essential for writing clean, reusable, and organized code.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top