0% found this document useful (0 votes)
6 views

Unit-3

The document provides an overview of Python functions, including their definition, advantages, built-in and user-defined functions, and anonymous functions. It also covers concepts like pass by value vs pass by reference, recursion, variable scope and lifetime, as well as modules and packages in Python. Examples are provided throughout to illustrate the concepts discussed.

Uploaded by

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

Unit-3

The document provides an overview of Python functions, including their definition, advantages, built-in and user-defined functions, and anonymous functions. It also covers concepts like pass by value vs pass by reference, recursion, variable scope and lifetime, as well as modules and packages in Python. Examples are provided throughout to illustrate the concepts discussed.

Uploaded by

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

🟢 Python Functions

✅ 1. Functions in Python

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

● Functions avoid code repetition and make programs easier to


read.

● You can call a function multiple times, reducing effort.

🔎 Example:

def greet(name):

print(f"Hello, {name}!")

greet("Arunima") # Output: Hello, Arunima!

✅ 2. Advantages of Functions

● Code Reusability: Functions avoid duplication.

● Modular Programming: Code is divided into small, manageable


functions.

● Easy Debugging: Functions make debugging simpler.

● Efficient Development: Teams can work on separate functions.

✅ 3. Built-in Functions

Python provides many functions like:

● print(), len(), sum(), max(), min(), type()

● Example:

print(len("Python")) # Output: 6

print(max(10, 20, 30)) # Output: 30

✅ 4. User-Defined Functions

You can define your own functions using the def keyword.

🔎 Example:

def add_numbers(a, b):


return a + b

print(add_numbers(5, 10)) # Output: 15

✅ 5. Anonymous Functions (Lambda Functions)

● Lambda Functions are single-line functions without a name.

● Syntax: lambda arguments: expression

● Useful for short operations.

🔎 Example:

square = lambda x: x * x

print(square(5)) # Output: 25

🟢 Pass by Value vs Pass by Reference

● Pass by Value: Copy of the variable is passed, changes inside the


function won’t affect the original.

● Pass by Reference: The actual variable is passed, changes affect


the original.

🔎 Example:

# Pass by Value (Immutable Data like int, str, tuple)

def modify_value(x):

x = 10

print("Inside function:", x)

a=5

modify_value(a)

print("Outside function:", a) # Output: 5

# Pass by Reference (Mutable Data like list, dict)

def modify_list(lst):

lst.append(100)
print("Inside function:", lst)

my_list = [1, 2, 3]

modify_list(my_list)

print("Outside function:", my_list) # Output: [1, 2, 3, 100]

🟢 Recursion in Python

● Recursion means a function calling itself.

● It solves complex problems by breaking them into smaller


subproblems.

● Every recursion needs a base case to avoid infinite calls.

🔎 Example (Factorial Calculation):

def factorial(n):

if n == 0:

return 1

else:

return n * factorial(n-1)

print(factorial(5)) # Output: 120

🟢 Scope and Lifetime of Variables

● Scope refers to where a variable can be accessed.

o Local Scope: Inside a function.

o Global Scope: Outside all functions.

● Lifetime means how long a variable exists.

o Variables in local scope die after function execution.

🔎 Example:

x = 10 # Global variable
def my_function():

y = 5 # Local variable

print("Inside function:", x, y)

my_function()

print("Outside function:", x)

# print(y) # Error: y is not defined outside the function

🟢 Python Modules

✅ 1. What is a Module?

● A module is a file containing Python code (functions, variables,


classes).

● It helps organize large programs.

🔎 Example:

● Save this as my_module.py:

def greet(name):

return f"Hello, {name}!"

import my_module

print(my_module.greet("Arunima")) # Output: Hello, Arunima!

✅ 2. Need for Modules

● Reusability of code across projects.

● Easier maintenance.

● Better code organization.

✅ 3. Creating and Importing a Module

● Create a .py file with functions or classes.

● Import it using import module_name.

● Use from module_name import function_name for specific functions.


🔎 Example:

python

CopyEdit

# Importing entire module

import math

print(math.sqrt(16)) # Output: 4.0

# Importing specific functions

from math import pow

print(pow(2, 3)) # Output: 8.0

✅ 4. Path Searching of a Module

● Python looks for modules in directories using sys.path.

🔎 Example:

python

CopyEdit

import sys

print(sys.path)

✅ 5. Module Reloading

● If you modify a module, you can reload it using:

python

CopyEdit

import importlib

importlib.reload(my_module)

🟢 Python Packages

● A Package is a collection of modules organized in directories.

● It contains a special file __init__.py.


● Example structure:

markdown

CopyEdit

my_package/

├── __init__.py

├── module1.py

└── module2.py

🔎 Example:

python

CopyEdit

# Inside module1.py

def say_hello():

return "Hello from module1!"

# Using the package

from my_package import module1

print(module1.say_hello()) # Output: Hello from module1!

You might also like