Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views7 pages

Unit II 04 Functions and Modules

The document explains the concepts of functions and modules in Python, highlighting their importance in code organization, reusability, and maintainability. Functions are defined using the 'def' keyword and can take arguments, while modules are files containing Python code that promote modular programming. Additionally, it covers useful built-in modules for statistics and random number generation, providing examples of essential functions and their applications.

Uploaded by

victor.seelan
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 views7 pages

Unit II 04 Functions and Modules

The document explains the concepts of functions and modules in Python, highlighting their importance in code organization, reusability, and maintainability. Functions are defined using the 'def' keyword and can take arguments, while modules are files containing Python code that promote modular programming. Additionally, it covers useful built-in modules for statistics and random number generation, providing examples of essential functions and their applications.

Uploaded by

victor.seelan
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/ 7

Functions and Modules

Functions
Definition:
A function is a reusable block of organized, modular code designed to perform a specific task.
Functions help break down complex problems, improve code reusability, readability, and
maintainability.
Defining a Function: Use the def keyword followed by a function name and parentheses with
optional parameters.

Functions offer several key advantages in programming. They help avoid code
repetition by allowing you to reuse blocks of code for similar tasks instead of rewriting them,
which improves efficiency and reduces errors. They also make debugging and testing easier,
since problems can be isolated and fixed within individual functions.
Functions can take arguments and return results, enabling dynamic and flexible
operations tailored to different inputs. Additionally, Python supports default and keyword
arguments, which enhance flexibility by allowing optional parameters and more readable
function calls.
Modules
A module in Python is a file containing Python definitions, functions, variables, and classes.
Modules allow you to organize your code, reuse functionality, and keep programs
modular.
A Python module is a file containing Python code, which may include functions, classes,
variables, and runnable statements, saved with a .py extension. Modules serve as a way to
logically organize code into reusable components, promoting modular programming. This
allows developers to break complex problems into smaller parts that are easier to manage,
maintain, and reuse across different programs.
Creating a module is straightforward: you write the desired Python code and save it as
a .py file. For example, a file named mymodule.py might contain some related functions:
Pandas module:
pandas is a powerful, open-source Python library used for data manipulation, analysis, and
cleaning. It provides data structures that make working with structured data (like tables or
time series) fast and easy.
Use Cases in Statistics
 Cleaning and preparing raw data
 Grouping and aggregating for descriptive statistics
 Joining datasets from different sources
 Time series data handling
 Preparing data for modeling or plotting
Useful Built-in Modules for Statistics:
list of essential functions from the statistics module:

Function Description Example

mean(data) Arithmetic mean (average) statistics.mean([1,2,3])

median(data) Middle value (sorted) statistics.median([1,2,3])

mode(data) Most frequent value statistics.mode([1,1,2,3])

stdev(data) Standard deviation (sample) statistics.stdev([1,2,3,4])

variance(data) Sample variance statistics.variance([1,2,3])

median_low(data) Low median in even-numbered data statistics.median_low([1,2,3,4])

median_high(data) High median in even-numbered data statistics.median_high([1,2,3,4])

pstdev(data) Population standard deviation statistics.pstdev([1,2,3])

pvariance(data) Population variance statistics.pvariance([1,2,3])

Random Module:
The random module is a built-in Python library used to generate pseudo-random numbers and
perform random sampling.
It is commonly used in:
 Simulations (e.g., Monte Carlo methods)
 Bootstrapping and resampling
 Random data generation
 Probabilistic modelling
list of essential functions from the Random Module:

Function Description Example Output

random() Returns a float between 0.0 and 1.0 0.7435

uniform(a, b) Random float between a and b random.uniform(1, 10)

Random integer between a and b


randint(a, b) random.randint(1, 6)
(inclusive)

Randomly select 1 item from a


choice(seq) random.choice(['A', 'B', 'C'])
list/tuple/string
Function Description Example Output

sample(population, k) Random sample of k unique elements random.sample(range(100), 5)

Randomly reorder the elements in


shuffle(seq) random.shuffle(my_list)
place

Initializes the random number random.seed(42) (for


seed(n)
generator reproducibility)

You might also like