2. MODULES
As you gain experience writing code, you will eventually
work on projects that are so large that keeping all of the
code in a single file becomes cumbersome.
Instead of writing a single file, you can put related code into
separate files called modules.
Individual modules can be put together like building blocks
to create a larger application.
There are four main advantages to breaking a program into
modules:
1) Simplicity: Modules are focused on a single problem.
2) Maintainability: Small files are better than large files.
3) Reusability: Modules reduce duplicate code.
4) Scoping: Modules have their own namespaces.
2
3. MODULES ---
A module is a file consisting of Python code that can define functions, classes and
variables.
A module allows you to organize your code by grouping related code which makes the
code easier to understand and use.
A module is a file containing Python code that can be re-used in other Python code files.
You can use any Python source file as a module by executing an import statement
Python's from statement lets you import specific attributes from a module into the
current namespace.
import * statement can be used to import all names from a module into the current
namespace
3
4. MODULES---
A namespace is a collection of names, such as variable
names, function names, and class names.
Every Python module has its own namespace. Variables,
functions, and classes in a module can be accessed from
within the same module by just typing their name.
To access a name in an imported module from the calling
module, type the imported module’s name followed by a
dot (.) and the name you want to use:
Note: The name used to import a module is the same as the
module’s
file name.
4
5. Working With Packages
Modules allow you to divide a program in to individual files
that can be reused as needed.
Related code can be organized into a single module and
kept separate from other code.
Package take this organizational structure one step further
by allowing you to group related modules under a single
namespace.
A package is a folder that contains one or more Python
5
12. module Date Time
A date in Python is not a data type of its own, but we can import a module named
datetime to work with dates as date objects.
When we execute the code from the example above the result will be: 2022-06-27
09:40:16.122293
The date contains year, month, day, hour, minute, second, and microsecond.
The datetime module has many methods to return information about the date object.
12
13. module Date Time ---
The datetime() class also takes parameters for time and timezone
(hour, minute, second, microsecond, tzone), but they are optional, and
has a default value of 0, (None for timezone).
13
15. Examples ---
A program to calculate area and circumstance of a circle
15
16. Functions In Python
A function is a block of organized, reusable code that is used to perform a single,
related action.
Functions provide better modularity for your application and a high degree of code
reusing.
Defining aFunction
Function blocks begin with the keyword def followed by the function name and
parentheses ( ( ) ).
Any input parameters or arguments should be placed within these parentheses.
You can also define parameters inside these parentheses.
The first statement of a function can be an optional statement - the documentation
string of the function or docstring.
The code block within every function starts with a colon (:) and is indented.
The statement return [expression] exits a function, optionally passing back an
expression to the caller.
A return statement with no arguments is the same as return None.
16
17. Functions
Function Syntax
Function Arguments
You can call a function by using any of the following types of
arguments:
• Required arguments: the arguments passed to the function in
correct positional order.
• Keyword arguments: the function call identifies the arguments
by the parameter names.
• Default arguments: the argument has a default value in the
function declaration used when the value is not provided in the
function call.
17
18. Functions
Variable-length arguments: This used when you need to process unspecified
additional arguments.
An asterisk (*) is placed before the variable name in the function declaration.
18
19. Types of Functions
Types of functions in Python:
1. Built-in functions : The Python interpreter has a number of functions built into it that are
always available. They are listed here in alphabetical order.
2. User-Defined Functions (UDFs): The Functions defined by User is known as User
Defined Functions. These are defined with the keyword def.
3. Anonymous functions, which are also called lambda functions because they are not
declared with the standard def keyword.
19
20. Functions---
Example: Define a function which can generate and print a tuple where
the value are square of numbers between 1 and 20.
20
23. Exercises
1) Write a function in python that counts number of vowels in given
string
2) Write a function in python that counts number of even and odd in a given
list.
3) Write a function in python that computes the factorial of a given number.
4) Write a Python function to sum all the numbers in a list.
Sample List : [8, 2, 3, 0, 7]
Expected Output : 20
5) Write a function in python that converts Decimal number to Binary
numbers?
6) Write a function in python that converts Binary number to Decimal
numbers?
23
24. The Anonymous Functions
These functions are called anonymous because they are not declared in the standard manner by using
the def keyword. You can use the lambda keyword to create small anonymous functions.
Lambda forms can take any number of arguments but return just one value in the form of an
expression. They cannot contain commands or multiple expressions.
An anonymous function cannot be a direct call to print because lambda requires an expression.
Lambda functions have their own local namespace and cannot access variables other than those in
their parameter list and those in the global namespace.
Although it appears that lambdas are a one-line version of a function, they are not equivalent to
inline statements in C or C++, whose purpose is to stack allocation by passing function, during
invocation for performance reasons.
The syntax of lambda functions contains only a single statement, which is as follows
lambda [arg1 [,arg2,.....argn]]:expression 24
25. The Anonymous Functions ---
Example:
# Function definition is here
sum = lambda arg1, arg2: arg1 + arg2
# Now you can call sum as a function
print ("Value of total : ", sum( 10, 20 ))
print ("Value of total : ", sum( 20, 20 ))
When the above code is executed, it produces the following result −
Value of total : 30
Value of total : 40
25