7_Functions and Modules
7_Functions and Modules
Libraries
Learning objective
• What is a Function?
• Rules - Defining a Function
• Creating and calling Functions
• Types of Functions
• What is a Module?
• Creating a Module
• Importing a Module
• Locating Module
• Math Library
• Standard Libraries
• Third Party Libraries
What is a Function?
• A function in Python is a block of reusable code that performs a
specific task.
• The code inside the function runs when the function is called.
Rules - Defining a Function
• Function blocks begin with the keyword def followed by the
function name and parentheses
pow(3,2)
Function Example
def sq(a):
return a*a
def cube(a):
return a*a*a
def Display():
return 35
D = Display()
print("My age is", D)
Anonymous Functions (Lambda Functions)
• Lambda functions are small, unnamed functions defined using
the lambda keyword.
• They are often used for short, simple operations that are passed
as arguments to other functions.
square = lambda x: x * 2
print(square(5))
What is a Module?
• Module is basically a file in python.
def greet(name):
return f"Hello, {name}!"
PI = 3.14159
Importing a Module
• When using a function from a module, use
modulename.functionname
• Create another file in python named as importmodule.py
import testmodule
print(testmodule.greet("Arif"))
print(testmodule.PI)
print(testmodule.add(30,50))
Importing specific functions of variables
import testmodule as tm
print(tm.add(40,70))
print(tm.PI)
print(tm.greet("Arif"))
Import all the functions and variables
from testmodule import *
print(add(40,5))
print(PI)
print(greet("Arif Ahmad"))
Locating modules
• The module search path is stored in the system module sys as the
sys.path variable.
• The sys.path variable contains the current directory,
• PYTHONPATH, and the installation-dependent default.
• To find out the current working directory, same as pwd in linux
import os
print(os.getcwd())
Math Library
• The math library is a built-in library in Python that provides
mathematical functions.
import math
print(math.sqrt(4))
print(math.pi)
print(math.floor(2.9))
print(math.ceil(2.9))
The from…import statement
• Python’s from statement lets you import specific attributes from a
library.
• For Example:
from math import sqrt, factorial
print (sqrt(16))
print (factorial(6))
The from…import * statement
• It is also possible to import all names from a module or library into the
current namespace by using the following import statement:
from modulename import *
• This provides an easy way to import all the items from a module into
the current namespace. For Example:
from math import *
print(pow(5,3))
print(radians(30))
print(sqrt(36))
print(degrees(0.5239))
Standard Libraries
• Collection of modules and packages included with Python itself.
• It covers a wide range of functionality, from file I/O and system calls to web
services and data serialization.
• Example modules:
• math: Mathematical functions like sqrt, sin, cos, etc.
• datetime: Date and time manipulation.
• os: Interacting with the operating system, such as reading or writing files.
• json: JSON (JavaScript Object Notation) encoding and decoding.
• re: Regular expressions for string pattern matching.
• collections: Specialized container datatypes like namedtuple, deque, and
Counter.
Import datetime
import datetime
now = datetime.datetime.now()
print(now)
print(now.strftime("%Y-%m-%d %H:%M:%S"))
print(now.time())
print(datetime.date.today())
print(datetime.date.ctime(now))
Python Libraries
Libraries in Python
• Collection of modules and packages that provide pre-written code
to perform common tasks, so you don't have to write everything
from scratch.
import datetime
now = datetime.datetime.now()
print(now)
print(now.strftime("%Y-%m-%d %H:%M:%S"))
print(now.time())
print(datetime.date.today())
print(datetime.date.ctime(now))
Third-party libraries
• Created by the Python community and are not included in the
standard library.
• You can install these libraries using the pip package manager.
• python installer package similar to rpm in linux / msi in Windows