0% found this document useful (0 votes)
9 views9 pages

Python Functions and Modules Guide

The document explains functions in Python, detailing their definition, types (built-in and user-defined), syntax, and key concepts such as parameters, arguments, and return statements. It also introduces modules, describing their purpose, types (built-in, user-defined, and third-party), and how to import and use them with practical examples. Additionally, it provides code snippets to illustrate the use of functions and modules in Python programming.

Uploaded by

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

Python Functions and Modules Guide

The document explains functions in Python, detailing their definition, types (built-in and user-defined), syntax, and key concepts such as parameters, arguments, and return statements. It also introduces modules, describing their purpose, types (built-in, user-defined, and third-party), and how to import and use them with practical examples. Additionally, it provides code snippets to illustrate the use of functions and modules in Python programming.

Uploaded by

arayan56213
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Functions in Python

Theoretical Details
 A function is a block of reusable code
that performs a specific task.
 Functions help to break our program into
smaller and modular chunks.
Types of Functions
 Built-in functions – Already provided by
Python (e.g., print(), len(), range()).
 User-defined functions – Created by
the programmer using the def keyword.
Functions in Python

Function Syntax
 def function_name(parameters):
statement(s)
return [expression]
Key Concepts
 Parameters vs Arguments:
 Parameters are variables in the function definition.
 Arguments are the actual values passed during a
function call.
 Return Statement – Sends a result back to the
caller.
 Scope – Variables inside functions are local by default.
Functions in Python

Practical Examples
➤ Example 1: A simple function
 def greet():
print("Hello, Python Learner!")
greet()
➤ Example 2: Function with parameters
 def add(a, b):
return a + b
result = add(5, 3)
print("Sum:", result)
Functions in Python

➤ Example 3: Function with default


parameter
 def greet(name="Guest"):
print(f"Hello, {name}!")
greet()
greet("Alice")
➤ Example 4: Function with return value
 def square(x):
return x * x
print("Square of 4 is:", square(4))
Modules in Python

What is a Module?
 A module is simply a Python file (.py)
containing definitions and statements.
Why Use Modules?
 To organize code into logical
components.
 To reuse code across multiple
programs.
 To encapsulate functionality.
Modules in Python

Types of Modules
 Built-in Modules – Pre-installed with Python.
Example: math, os, sys
 User-defined Modules – Created by users to
modularize their code.
 Third-party Modules – Installed using package
managers like pip.
Example: numpy, pandas, requests
Importing Modules
 import module_name
 from module_name import function_name
 import module_name as alias
Modules in Python

Practical Examples
1. Built-in Module Example: math
 import math
 print([Link](16)) # Output:
4.0
 print([Link])
# Output: 3.141592653589793
Modules in Python
2. User-defined Module
Step 1: Write the module to a file
 # Create [Link] dynamically in Colab or Jupyter
 with open("[Link]", "w") as file:
[Link]("""
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b == 0:
return "Error: Division by zero"
return a / b """)
Modules in Python
Step 2: Now import and use it
 import calculator
 print("Simple Calculator")
 print("Choose operation: add, subtract, multiply, divide")
 choice = input("Enter operation: ").lower()
 a = float(input("Enter first number: "))
 b = float(input("Enter second number: "))
 if choice == "add":
print("Result:", [Link](a, b))
elif choice == "subtract":
print("Result:", [Link](a, b))
elif choice == "multiply":
print("Result:", [Link](a, b))
elif choice == "divide":
print("Result:", [Link](a, b))
else:
print("Invalid operation")

You might also like