PYTHON
CONTENTS
Functions and Modules Implementation
Function syntax
argument types (positional, keyword, variable-length)
return values
lambda functions
passing functions as arguments,
creating reusable modules and importing.
FUNCTION SYNTAX
A function is a block of reusable code that performs a specific task.
Syntax:
def function_name(parameters):
# Code block
return value
Example:
def greet(name):
return f"Hello, {name}!"
TYPES OF ARGUMENTS
Positional Arguments:
Positional arguments are values passed to a function in a specific order. The order of the arguments is
based on the function's signature
Example:
def add(a, b):
return a + b
print(add(5, 10)) #output:15
KEYWORD ARGUMENTS
The Keywords are mentioned during the function call along with their corresponding values.
These keywords are mapped with the function arguements, so the function can easily identify the
corresponding values even if the order is not maintained during the function call.
Using the Keyword Arguement, the arguement passed in function call is matched with function definition on
the basis of the name of the parameter.
Example:
def greet(name, message):
return f"{message}, {name}!”
print(greet(name="Alice", message="Hi")) #Hi, Alice!
DEFAULT ARGUMENTS
When we call the function, if we are not passing any arguements the default arguements
we assigned at declaration time will be assigned by default.
Example:
def greet(name, message="Hello"):
return f"{message}, {name}!“
greet('balaji’) #'Hello, balaji!'
VARIABLE-LENGTH ARGUMENTS
This is very useful when we do not know the exact number of arguments that will be passed to a function or we can have a
design where any number of arguments can be passed based on the requirement.
Example:
def display_arguements(*var):
for i in var:
print('Variable Arguement :', i)
display_arguements()
display_arguements(10, 20, 30)
#Variable Arguement : 10
#Variable Arguement : 20
#Variable Arguement : 30
Arbitrary Keyword Arguements :
In Python Arbitrary Keyword Arguments, *args, and **kwargs can pass a variable
number of arguments to a function using special symbols. There are two special
symbols:
1. *args in Python (Non-Keyword Arguements)
2. **kwargs in Python (Keyword Arguements)
For non-keyword arguments:
def sum_all(*args):
return sum(args)
print(sum_all(1, 2, 3)) #6
For keyword arguments:
def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_info(name="Alice", age=25)
#name: Alice
#age: 25
RETURN VALUES
The value a function gives back after execution.
Syntax:
def function_name(parameters):
return value
Example:
def square(number):
return number * number
print(square(4))
#16
LAMBDA FUNCTIONS
Anonymous functions defined using the `lambda` keyword.
Often used with functions like `map()`, `filter()`, and `reduce()`
Syntax:
lambda arguments: expression
Example:
square = lambda x: x * x
print(square(5))#25
PASSING FUNCTIONS AS ARGUMENTS
Functions can be passed as arguments to other functions. It Enhances flexibility and modularity.
Example:
def apply_operation(a, b, operation):
return operation(a, b)
def add(x, y):
return x + y
print(apply_operation(5, 3, add)) #8
CREATING REUSABLE MODULES
A module is a Python file containing definitions and statements.
Steps to Create a Module:
Create a `.py` file with function definitions.
Import the module in another script.
Example:
def add(a, b):
return a + b
def subtract(a, b):
return a - b
IMPORTING MODULES
Importing math module for all mathematical operations.
import math
print(math.pi) # 3.141592653589793
print(math.e) # 2.718281828459045
print(math.sin(0.5235987755982988)) # 0.49999999999999994
print(math.pow(2, 4)) # 16.0
print(2 ** 4) # 16
print(math.radians(30)) # 0.5235987755982988
print(math.degrees(math.pi/6)) # 29.999999999999996
Importing statistics for mean,median ,mode and standard deviation operations.
import statistics
print(statistics.mean([2, 5, 6, 9])) # 5.5
print(statistics.median([1, 2, 3, 8, 9])) #3
print(statistics.mode([2, 5, 3, 2, 8, 3, 9, 4, 2, 5, 6])) # 2
print(statistics.stdev([1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]))
# Output : 1.3693063937629153
Importing Random Module for printing random variables like random int, random range etc
import random
print(random.random()) # 0.13497850303118875
print(random.randint(1, 100)) # 20
print(random.randrange(1, 10)) #2
print(random.choice('computer')) #u
print(random.choice([12, 23, 45, 67, 65, 43])) # 67
#random.shuffle() - randomly re-orders the elements in a list
numbers = [12, 23, 45, 67, 65, 43]
random.shuffle(numbers)
print(numbers) # [45, 43, 23, 65, 67, 12]
THANK YOU…