0% found this document useful (0 votes)
10 views3 pages

PP 7

Python files for reference to understand multiple types of lab files in python. Useful to understand some libraries.

Uploaded by

Aahaan Phadnis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

PP 7

Python files for reference to understand multiple types of lab files in python. Useful to understand some libraries.

Uploaded by

Aahaan Phadnis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PYTHON LAB MANUAL

EXPERIMENT NO.4
NAME: Adwaita Patane and Dhriti Ayyalasomayajula
REGISTRATION NUMBERS: 231061003 & 231061021
AIM: To create and apply user-defined functions in Python
SOFTWARE USED: Jupyter Notebook
THEORY:
What is a Function?
A function is a reusable block of code designed to perform a specific task. Functions
help organize code, reduce redundancy, and make programs more manageable and
easier to debug.

Types of Functions in Python


1. Built-in Functions: Predefined by Python (e.g., print(), len(), etc.).
2. User-Defined Functions: Created by the programmer to perform specific tasks.

Defining a User-Defined Function


A user-defined function is created using the def keyword, followed by:
1. A function name (must adhere to Python naming rules).
2. Optional parameters to receive input.
3. An indented body containing the function logic.
4. Optionally, a return statement to provide output to the caller.

Components of a Function
1. Function Name: The identifier for calling the function.
2. Parameters: Input placeholders; optional and can have default values.
3. Docstring: A brief description of the function’s purpose, enclosed in triple
quotes.
4. Function Body: The logic or actions performed by the function.
5. Return Statement: Optional; used to send output to the caller.
Steps to Use a User-Defined Function
1. Define: Write the function with the desired logic.
2. Call: Invoke the function by its name, passing any required arguments.
3. Receive Output: Capture and utilize any returned results.

Scope of Variables
1. Local Scope: Variables declared within a function exist only within that function.
2. Global Scope: Variables declared outside all functions are accessible
throughout the program unless shadowed by a local variable.

Lambda Functions
Lambda functions are anonymous, one-liner functions defined using the lambda
keyword. They are useful for short, throwaway functions but lack a name and a return
statement.

Best Practices
1. Modularity: Keep functions focused on a single task.
2. Meaningful Names: Use descriptive identifiers for functions and parameters.
3. Documentation: Use docstrings to explain functionality.
4. Avoid Side Effects: Limit changes to global variables within functions.
5. Test Thoroughly: Ensure the function handles edge cases and exceptions

PROGRAM:
CONCUSION:
In conclusion, user-defined functions in Python provide a powerful tool for creating
modular, reusable, and efficient code. By encapsulating logic within functions,
programmers can reduce redundancy, improve code readability, and simplify
debugging. Understanding the structure and components of functions, including
parameters, scope, and return values, is essential for effective function design.
Lambda functions further enhance flexibility by enabling concise one-liner
implementations for simple tasks. Mastering these concepts equips developers to
build robust and maintainable Python programs.

You might also like