0% found this document useful (0 votes)
19 views4 pages

Module 2

The document explains four types of function arguments in Python: default arguments, keyword arguments, positional arguments, and arbitrary arguments (*args and **kwargs). It also covers recursion, global variables, and modules, providing examples for each concept. Additionally, it includes a prompt to write a Python program that accepts ten values and returns the maximum using *args.

Uploaded by

Leroy Golconda
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)
19 views4 pages

Module 2

The document explains four types of function arguments in Python: default arguments, keyword arguments, positional arguments, and arbitrary arguments (*args and **kwargs). It also covers recursion, global variables, and modules, providing examples for each concept. Additionally, it includes a prompt to write a Python program that accepts ten values and returns the maximum using *args.

Uploaded by

Leroy Golconda
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

1.

Four Types of Function Arguments in Python

Python functions support four types of arguments:

a) Default Arguments

These are values provided in the function definition.

If the user does not provide a value for that argument, the default is used.

Example:

def greet(name, message="Hello"):


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

greet("Alice") # Uses default message


greet("Bob", "Good morning") # Overrides default message

---

b) Keyword Arguments

The caller provides the argument names explicitly.

Order doesn't matter when using keyword arguments.

Example:

def describe_pet(animal, name):


print(f"I have a {animal} named {name}.")

describe_pet(name="Buddy", animal="dog") # Order doesn't matter

---

c) Positional Arguments

Values are assigned to parameters in the order they are provided.

The number and order must match the function definition.


Example:

def add(x, y):


print(x + y)

add(10, 5) # x = 10, y = 5

---

d) Arbitrary Arguments (*args and **kwargs)

Used when you don't know beforehand how many arguments might be passed.

*args (Non-keyword variable-length arguments):

def total(*numbers):
print(sum(numbers))

total(1, 2, 3, 4) # Outputs 10

**kwargs (Keyword variable-length arguments):

def print_info(**info):
for key, value in [Link]():
print(f"{key}: {value}")

print_info(name="Alice", age=25)

---

2. Recursion, Global Variables, and Modules in Python

---

a) Recursion

A function calling itself to solve a smaller instance of a problem.


Requires a base case to avoid infinite loops.

Example: Factorial using recursion

def factorial(n):
if n == 0:
return 1 # base case
else:
return n * factorial(n - 1)

print(factorial(5)) # Output: 120

Note: Use recursion carefully to avoid RecursionError.

---

b) Global Variables

Variables declared outside a function.

Can be accessed from within functions, but to modify them, you must use the global keyword.

Example:

count = 0 # Global variable

def increment():
global count
count += 1

increment()
print(count) # Output: 1

---

c) Modules

A module is a file containing Python definitions and functions.

Helps organize and reuse code across programs.


Creating a module:

# In a file named `[Link]`


def add(x, y):
return x + y

Using the module:

# In another file or Python shell


import mymodule

result = [Link](3, 4)
print(result) # Output: 7

You can also use:

from mymodule import add (import only specific functions)

import mymodule as mm (aliasing)

[Link] a program in Python that accepts ten values and returns the maximum of them. (Use
*args)

You might also like