MODULE 3 Python
MODULE 3 Python
1. Python Functions
What is a Function?
A function is a block of reusable code designed to perform a specific task. It helps in writing
efficient programs by reducing repetition and improving organization.
Purpose of Functions
Functions help break down complex problems into smaller steps. This improves readability,
code reusability, and makes debugging easier.
2. A) Built-in Functions
Python provides many built-in functions that can be used without needing to write them from
scratch. These functions perform common tasks like calculations, data conversions, and
input/output handling.
1
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
2
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
b) User-Defined Functions
These are functions created by the user to perform specific tasks. You define them using the def
keyword.
3
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
Explanation:
Python provides three ways to import functions from the math module:
You can use functions by prefixing them with math., e.g., math.ceil(5.4).
You can use the functions directly without the module prefix, e.g., ceil(5.4).
This imports all functions, allowing you to use them without the module prefix. However, it's
generally not recommended due to potential naming conflicts.
4
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
The math module provides various functions that perform mathematical operations. Below
are some commonly used functions:
1. math.ceil(x): (Ceiling)
o Returns the smallest integer greater than or equal to x.
o Example: math.ceil(5.4) → 6
2. math.comb(n, k): (Combination)
o Returns the number of ways to choose k items from n items without repetition and
without order (combinations).
o Example: math.comb(5, 2) → 10
3. math.fabs(x): (Floating Absolute Value)
o Returns the absolute value of x as a float.
o Example: math.fabs(-3) → 3.0
4. math.factorial(x): (Factorial)
o Returns the factorial of a non-negative integer x.
o Example: math.factorial(5) → 120
5. math.floor(x): (Floor)
o Returns the largest integer less than or equal to x.
o Example: math.floor(5.9) → 5
6. math.fsum(iterable): (Floating-point Sum)
o Returns an accurate floating-point sum of a given iterable (e.g., list or tuple).
o Example: math.fsum([1, 2, 3]) → 6.0
7. math.gcd(a, b): (Greatest Common Divisor)
o Returns the greatest common divisor of a and b.
o Example: math.gcd(8, 12) → 4
8. math.isqrt(n): (Integer Square Root)
o Returns the integer square root of n.
o Example: math.isqrt(16) → 4
9. math.modf(x): (Modf)
o Returns the fractional and integer parts of x as a tuple.
o Example: math.modf(5.75) → (0.75, 5.0)
10. math.perm(n, k): (Permutation)
o Returns the number of ways to choose k items from n items with order
(permutations).
o Example: math.perm(5, 2) → 20
11. math.prod(iterable, *, start=1): (Product).
o Returns the product of all the elements in an iterable. It can also take an optional
start value.(1)
o Example: math.prod([2, 3, 4]) → 24
5
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
Conclusion
Mathematical functions in Python provide powerful tools for performing a wide range of
numerical operations. The math module includes many useful functions that can simplify
complex calculations, making it easier for programmers to work with numbers in Python.
6
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
Conclusion
These functions allow you to handle power and logarithmic operations easily in Python,
helping with a variety of mathematical calculations.
Python provides several built-in trigonometric functions in the math module, which are used
to calculate angles and distances. Here's a simple explanation of each function:
7
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
Conclusion
These trigonometric functions are useful for working with angles and distances, especially in
fields like geometry, physics, and engineering. They provide quick solutions for common
trigonometric problems
8
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
Conclusion
These functions are useful when working with angles in different units, as radians are often
used in mathematical calculations, while degrees are more commonly used in everyday
applications.
Hyperbolic functions are similar to trigonometric functions but are based on hyperbolas
instead of circles.
9
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
Python provides multiple modules to handle dates and times efficiently, with the most
common ones being datetime and time.
The datetime module is the main way to work with dates and times in Python. It consists of
several classes:
1. date
o Full Form: Date
o Purpose: Represents a date (year, month, day).
o Usage:
Creating a date object: date(year, month, day)
2. time
3. datetime
10
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
4. timedelta
5. tzinfo
6. timezone
11
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
12
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
The time module provides various time-related functions that can be used to measure time
intervals, manage sleep times, and get the current time.
Key Functions:
time.time()
o Full Form: Current Time
o Purpose: Returns the current time in seconds since the epoch (January 1, 1970).
time.sleep(seconds)
13
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
In Python, random numbers are used in various applications like simulations, games, data
analysis, and cryptography. The random module provides tools to generate random numbers
and perform random operations. It supports generating numbers from different distributions
and allows customization to meet specific needs.
14
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
15
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
Parameters:
These are variables that are defined in a function's declaration. They act as
placeholders for the values (arguments) that will be passed into the function when it is
called.
Example:
Arguments:
These are the actual values or data you pass into the function when you call it.
Arguments can be variables, constants, or expressions.
Example:
1. Required Arguments:
These are arguments that must be provided to the function. If they are missing, the
program will raise an error.
Example:
2. Default Arguments:
These are arguments that have a default value. If the caller does not provide a value,
the function will use the default.
Syntax: parameter=default_value
Example:
16
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
3. Keyword Arguments:
These are arguments passed to a function by explicitly stating the parameter name.
This allows you to pass arguments in any order.
Example:
4. Arbitrary Arguments:
These allow a function to accept any number of positional arguments. They are
defined using an asterisk (*) before the parameter name.
Example:
These allow you to pass any number of keyword arguments to a function. You don't
know how many arguments you'll pass in advance.
17
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
Example:
Explanation:
Output:
7. Return Values:
When a function completes its task, it can send a result back using the return statement. To get the
output from a function and use it later in your code.
Local Variables:
18
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
Global Variables:
Example:
9. Recursion:
A function keeps calling itself until it reaches a stopping condition (called the base case).
19
PYTHON NOTES AL JAMIA ARTS AND SCIENCE COLLEGE
Explanation:
The function factorial(5) calls itself with smaller numbers (4, 3, 2, 1) until it
reaches 1.
When it reaches 1, it stops calling itself and calculates the final result.
Key Point:
Recursion must always have a base case to stop the function from calling itself
forever
It's used when you need a function quickly, for small tasks.
You use the keyword lambda to create it.
It can take arguments and returns a result, just like a regular function, but it doesn’t
need a name.
Syntax:
Example:
20