3.
Functions, Modules and Packages inPython
Functions: A function is a block of code which only
runs when it is called. A function helps avoiding
code repetition.
• How to Create a Function:
• In Python, a function is defined using
the def keyword, followed by a function name
and parentheses:
Example:-
def myfunction():
print("Hello”)
This creates a function named myfunction that
prints "Hello" when called.
Calling a Function
• To call a function, write its name followed by
parentheses.
• You can call the same function multiple times:
Function Arguments
Arguments:-
• Information can be passed into functions as arguments.
• Arguments are specified after the function name, inside
the parentheses. You can add as many arguments as you
want, just separate them with a comma.
Number of Arguments
• By default, a function must be called with the
correct number of arguments.
• If your function expects 2 arguments, you must
call it with exactly 2 arguments.
• Arguments:-
[Link] Arguments(single/multiple arguments)
[Link] Argument
[Link] Argument
Lambda Functions
• A lambda function is a small anonymous function.
• A lambda function can take any number of arguments, but
can only have one expression.
# A lambda function that adds 10 to a given
number
Why Module
Modules:
• Create a Module
• To create a module just save the code you want
in a file with the file extension .py:
• Example:-Save this code in a file named [Link]
def greeting(name):
print("Hello, " + name)
Use a Module
• Now we can use the module we just created,
by using the import statement:
• Import the module named mymodule, and
call the greeting function:
import mymodule
[Link]("Jonathan")
• Create a Module
• To create a Python module, write the desired code
and save that in a file with .py extension.
• Example: Let's create a [Link] in which we define
two functions, one add and another subtract.
• Import module
• Modules can be used in another Python file using
the import statement. When Python sees an import.
• Syntax to import a module: import module
• Example: Here, we are importing the calc that we
created earlier to perform add operation.
Create a calculator for addition ,subtraction,
multiplication,division using module
• Create file [Link]
def add(no1,no2):
print(no1+no2)
Again Create file [Link]
def sub(no1,no2):
print(no1-no2)
Now create another file [Link]
import addition
[Link](3,5)
Types of Modules
• User define Module
• Built in Module
Built-in Modules
• There are several built-in modules in Python,
which you can import whenever you like.
Package is a collection of Modules or collection of python files +
__init__.py
• Types of Packages
[Link] defined package
[Link] In package
[Link] In package
• To down load in built packages use PIP
command
• Check PIP version:
-->pip --version
• Install PIP
If you do not have PIP installed, you can
download and install it from this page: https://
[Link]/project/pip/
• Download a package named "camelcase":
• C:\Users\Your
Name\AppData\Local\Programs\Python\Python36-
32\Scripts>pip install camelcase
[Link](txt) capitalizes the first letter of each word in the string.