1. What is a Function?

A function is a block of reusable code that performs a specific task.

  • Helps avoid repeating code
  • Makes programs more organized

Examples of built-in functions:

  • print() → display output
  • type() → check data type
  • str() → convert to string

Key Point:
Function = reusable code for a specific task


2. Built-in vs User-Defined Functions

Built-in Functions

  • Already provided by Python
  • Ready to use

User-Defined Functions

  • Created by the programmer
  • Customized for specific needs

Key Point:
You can create your own functions for flexibility


3. Defining a Function

To create a function, use the def keyword.

Structure:

def function_name(parameters):
function body

Steps:

  1. Use def
  2. Add function name
  3. Add parentheses ()
  4. Add colon :
  5. Write indented code

Key Point:
Indentation is required in Python


4. Parameters (Inputs)

Parameters are inputs to a function.

Example idea:

  • Function takes a name → prints greeting

Important:

  • Parameter names are defined when creating the function
  • Must be used consistently inside the function

Key Point:
Parameters allow functions to work with different data


5. Function Body and Indentation

  • The function body contains instructions
  • Must be indented (commonly 4 spaces)

Rule:

  • All lines inside function must align

Key Point:
Indentation defines structure in Python


6. Calling a Function

After defining a function, you must call it to execute.

Example concept:

  • Call function with argument → function runs

Key Point:
Function does nothing until called


7. Return vs Print

Print

  • Displays output
  • Does not store result

Return

  • Sends result back
  • Allows storing in variables

Example:

  • return value → can reuse later

Key Point:
Return = reusable output


8. Return Statement

  • Uses keyword return
  • Produces a result

Example idea:

  • Calculate triangle area → return value

Key Point:
Return enables further computation


9. Reusability

Functions allow:

  • Write once
  • Use multiple times

Example:

  • Calculate area for multiple triangles

Key Point:
Reusability saves time and effort


10. Combining Functions with Variables

Example workflow:

  1. Call function
  2. Store result in variable
  3. Use variable in further calculations

Key Point:
Functions + variables = powerful combination


11. Example Concept: Time Conversion

Function:

  • Inputs: hours, minutes, seconds
  • Output: total seconds

Logic:

  • Convert all to seconds
  • Return result

Key Point:
Functions can perform real-world calculations


12. Importance for Data Professionals

Functions are essential because they:

  • Simplify complex tasks
  • Improve code organization
  • Enable automation
  • Support scalable analysis

Key Point:
Functions are core tools in data workflows


13. Best Practices

  • Use clear function names
  • Keep functions focused (one task)
  • Use consistent indentation
  • Prefer return over print for reusable code

Key Point:
Clean functions = professional code


Final Summary

Functions in Python are reusable blocks of code that perform specific tasks. They can take inputs (parameters), process data, and return outputs using the return statement. Functions improve code organization, enable reusability, and make programs more efficient. Understanding functions is essential for building scalable and maintainable programs in data analysis.


Key Takeaways

  • Function = reusable code block
  • Use def to define functions
  • Parameters = inputs
  • Indentation is required
  • return stores output for reuse
  • Functions improve efficiency and scalability
  • Reusability is a key advantage