Types of Function Arguments in Python
In Python, function arguments are the values passed to a function when it is called. Python
supports several types of arguments, which provide flexibility in function calls.
1. Positional Arguments
● These are the most common type of arguments.
● Values are passed to the function in the same order as the parameters are defined.
● The number and position of arguments must match the function definition.
Eg:
def add(a, b):
return a + b
add(10, 20)
2. Keyword Arguments
● Arguments are passed using parameter names.
● Order of arguments does not matter.
● Improves readability and avoids confusion.
Eg:
def student(name, age):
print(name, age)
student(age=20, name="Anu")
3. Default Arguments
● Default values are assigned to parameters.
● If no value is provided during function call, the default value is used.
● Helps reduce the number of arguments passed.
Eg:
def greet(name, msg="Good Morning"):
print(msg, name)
greet("Rahul")
4. Variable-Length Arguments (*args)
● Allows a function to accept any number of positional arguments.
● Represented using an asterisk (*).
● Arguments are stored as a tuple.
Eg:
def total(*numbers):
return sum(numbers)
result=total(10, 20, 30)
print(result)
● *numbers allows the function to accept any number of positional arguments.
● All passed values are collected into a tuple named numbers.
total(10, 20, 30)
● The arguments 10, 20, 30 are packed as:
● numbers = (10, 20, 30)
● sum(numbers) adds all the values.
Scope and Lifetime of Variables
Scope of Variables
Scope refers to the region of the program where a variable can be accessed or used.
Python follows the LEGB rule to determine variable scope:
1. Local Scope
● Variables declared inside a function.
● Accessible only within that function.
Example:
def func():
x = 10
print(x)
func()
2. Enclosing Scope
● Variables in the outer function of a nested function.
● Accessible inside inner functions.
Example:
def outer():
y = 20
def inner():
print(y)
inner()
outer()
3. Global Scope
● Variables declared outside all functions.
● Accessible throughout the program.
● Can be accessed inside functions using the global keyword if modification is needed.
Example:
x = 30 x = 30
def show(): def show():
global x
x = 40 x=40
show() show()
print(x) output 40 print(x) output 30
4. Built-in Scope
● Contains Python’s predefined names like print(), len(), sum().
Example:
print(len("Python"))
Lifetime of Variables
Lifetime refers to the duration for which a variable exists in memory during program
execution.
1. Local Variable Lifetime
● Created when the function is called.
● Destroyed when the function finishes execution.
Example:
def demo():
a = 5
print(a)
demo()
2. Global Variable Lifetime
● Created when the program starts.
● Exists until the program terminates.
Example:
b = 10
print(b)
Difference Between Scope and Lifetime
Scope Lifetime
Determines where a variable can be Determines how long a variable
accessed exists
Related to memory duration
Related to program structure
Types of Functions in Python
A function is a reusable block of code that performs a specific task. Based on their behavior and
usage, Python functions are classified into the following types:
1. Built-in Functions
● Predefined functions provided by Python.
● Used directly without defining them.
Examples:
print("Python")
len("Hello")
sum([1, 2, 3])
2. User-Defined Functions
● Functions created by the programmer using the def keyword.
● Improve modularity and reusability.
Example:
def add(a, b):
return a + b
3. Recursive Functions
● A function that calls itself to solve a problem.
● Must include a base condition to stop recursion.
Example:
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
4. Functions with More Than One Return Value
● A function can return multiple values using a tuple.
● Useful when multiple results are needed from a function.
Example:
def calculate(a, b):
return a + b, a - b
sum_val, diff_val = calculate(10, 5)
5. Void Functions
● Functions that do not return any value.
● They perform an action such as printing output.
● Technically return None in Python.
Example:
def display(msg):
print(msg)
display("Hello Python")
6. Anonymous (Lambda) Functions
● Functions without a name.
● Defined using the lambda keyword.
● Used for short operations.
Example:
square = lambda x: x * x
print(square(4))
Package in Python
A package is a collection of related modules stored inside a directory. Packages help in
organizing large programs into smaller, manageable parts.
Example of a Package (Full Program)
Step 1: Package Structure
mathpack/
├── __init__.py
├── [Link]
└── [Link]
Step 2: Module inside the Package
File: [Link]
def add(a, b):
return a + b
File: [Link]
def sub(a, b):
return a - b
Step 3: Main Program (Using the Package)
File: [Link]
from mathpack import add, sub
print("Addition:", [Link](10, 5))
print("Subtraction:", [Link](10, 5))
Output
Addition: 15
Subtraction: 5
Explanation
● mathpack is the package (folder).
● [Link] and [Link] are modules inside the package.
● __init__.py tells Python that the folder is a package.
● The from mathpack import add, sub statement imports modules from the package.
How to Create a Package in Python
A package is created by placing multiple Python modules inside a folder.
Step 1: Create a Folder (Package Name)
Create a folder with the package name.
mypackage
Step 2: Create __init__.py File
Inside the folder, create a file named:
__init__.py
● This file indicates that the folder is a package.
● It can be empty.
Step 3: Create Module Files
Create Python files (modules) inside the package.
File: [Link]
def add(a, b):
return a + b
File: [Link]
def mul(a, b):
return a * b
Step 4: Package Structure
mypackage/
├── __init__.py
├── [Link]
└── [Link]
Step 5: Create Main Program to Use Package
File: [Link]
from mypackage import add, mul
print("Addition:", [Link](10, 5))
print("Multiplication:", [Link](10, 5))
Output
Addition: 15
Multiplication: 50
Important Exam Points
● A package is a directory of modules.
● __init__.py marks the directory as a package.
● Packages improve code organization and reuse.
● Packages are imported using the import or from … import statements.
Modules and Packages in Python
A module is a file containing Python code such as functions, variables, and classes. A package
is a collection of related modules organized in directories.
1. Built-in Modules
● Modules that are predefined in Python.
● Provide ready-made functions and tools.
● Imported using the import statement.
Common Built-in Modules:
● math – mathematical operations
● random – random number generation
● sys – system-specific parameters
● os – operating system interaction
● datetime – date and time handling
Example:
import math
print([Link](25))
2. User-Defined Modules
● Modules created by the programmer.
● Saved as a .py file.
● Used to organize large programs and improve reusability.
Example:
File: [Link]
def add(a, b):
return a + b
Main Program:
import mymodule
print([Link](10, 20))