You are on page 1of 71

Modules in Python

Like many other programming languages, Python


supports modularity. That is, you can break large
code into smaller and more manageable pieces. And
through modularity, Python supports code reuse. You
can import modules in Python into your programs
and reuse the code therein as many times as you
want.
What are Python Modules?
Modules provide us with a way to share reusable
functions. A module is simply a “Python file” which
contains code we can reuse in multiple Python programs.
A module may contain functions, classes, lists, etc.
Modules in Python can be of two types:

Built-in Modules.
User-defined Modules.
Built-in Modules in Python
One of the many superpowers of Python is that it comes
with a “rich standard library”. This rich standard library
contains lots of built-in modules. Hence, it provides a lot of
reusable code.

To name a few, Python contains modules like “os”, “sys”,


“datetime”, “random”.

You can import and use any of the built-in modules


whenever you like in your program.
 User-Defined Modules in Python

Another superpower of Python is that it lets you take things in


your own hands. You can create your own functions and
classes, put them inside modules and voila! You can now
include hundreds of lines of code into any program just by
writing a simple import statement.
To create a module, just put the code inside a .py file.
Let’s create one.

# my Python module
def greeting(x):
print("Hello,", x)

Write this code in a file and save the file with the name
mypymodule.py. Now we have created our own module.
Importing Modules in Python
We use the import keyword to import both built-in and user-
defined modules in Python.

Let’s import our user-defined module from the previous


section into our Python shell:

>>> import mypymodule

To call the greeting function of mypymodule, we simply need


to use the dot notation:

>>> mypymodule.greeting("Techvidvan")
Let’s now import a built-in module into our Python shell:

>>> import random

To call the randint function of random, we simply need to use


the dot notation:

>>> random.randint(20, 100)


Using import…as statement (Renaming a module)

This lets you give a shorter name to a module while


using it in your program.

>>> import random as r

>>> r.randint(20, 100)


Using from…import statement

You can import a specific function, class, or attribute from


a module rather than importing the entire module. Follow
the syntax below,

from <modulename> import <function>

>>> from random import randint


>>> randint(20, 100)
69
You can also import multiple attributes and functions
from a module:

>>> from math import pi, sqrt

>>> print(3 * pi)


9.42477796076938

>>> print(sqrt(100))
10.0
>>>

Note that while importing from a module in this way,


we don’t need to use the dot operator while calling the
function or using the attribute.
Importing everything from Python module

If we need to import everything from a module and we


don’t want to use the dot operator, do this:

>>> from math import *

>>> print(3 * pi)

9.42477796076938

>>> print(sqrt(100))

10.0
>>>
Python dir() function
The dir() function will return the names of all the properties and
methods present in a module.
>>> import random
>>> dir(random)
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random',
'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_Sequence', '_Set',
'__all__', '__builtins__', '__cached__', '__doc__', '__file__',
'__loader__', '__name__', '__package__', '__spec__', '_accumulate',
'_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_inst', '_log', '_os', '_pi',
'_random', '_repeat', '_sha512', '_sin', '_sqrt', '_test',
'_test_generator', '_urandom', '_warn', 'betavariate', 'choice',
'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits',
'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint',
'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular',
'uniform', 'vonmisesvariate', 'weibullvariate‘]
Python Variable Scope

What is Variable Scope in Python?

In programming languages, variables need to


be defined before using them. These variables can only be
accessed in the area where they are defined, this is
called scope. You can think of this as a block where you
can access variables.
Types of Python Variable Scope
There are four types of variable scope in Python, let’s go
through each of them.
1. Local Scope
Local scope variables can only be accessed within its block.

Let’s see it with an example.


a = 10

def function():
print(“Hello”)
b = 20
function()
print(a)
print(b)
Output:
Hello
10
Traceback (most recent call last):
  File “main.py”, line 7, in <module>
    print(b)
NameError: name ‘b’ is not defined

In the above example, we see that Python prints the value of


variable a but it cannot find variable b. This is because b was
defined as a local scope in the function so, we cannot access
the variable outside the function. This is the nature of the local
scope.
Global Scope

The variables that are declared in the global scope can


be accessed from anywhere in the program. Global
variables can be used inside any functions. We can also
change the global variable value.

Msg = “This is declared globally”


def function():
#local scope of function
print(Msg)
function()

Output:
This is declared globally
But, what would happen if you declare a local variable with
the same name as a global variable inside a function?

msg = “global variable”


def function():
#local scope of function
msg = “local variable”
print(msg)
function()
print(msg)
Output:
local variable
global variable
As you can see, if we declare a local variable with the
same name as a global variable then the local scope will
use the local variable.
If you want to use the global variable inside local scope
then you will need to use the “global” keyword.

Enclosing Scope

A scope that isn’t local or global comes under enclosing


scope.
def vehicle():
fun= “Start”
def car():
model= “Toyota”
print(fun)
print(model)
car()
vehicle()
Output:
Start
Toyota

In the example code, the variable fun is used inside the car()


function. In that case, it is neither a local scope nor a global
scope. This is called the enclosing scope.
Built-in Scope
This is the widest scope in Python. All the reserved names in
Python built-in modules have a built-in scope.
When the Python doesn’t find an identifier in it’s local,
enclosing or global scope, it then looks in the built-in scope to
see if it’s defined there.
a = 5.5
int(a)
print(a)
print(type(a))
Python would see in the local scope first to see which of the
variables are defined in the local scope, then it will look in the
enclosing scope and then global scope.
If the identifier is not found anywhere then, at last, it will check the
built-in scope.
Here the functions int(), print(), type() does not need to be defined
because they are already defined in the built-in scope of Python.
Assign Function to a Variable

If you assign a function to a variable, you can use the


variable as the function:

def double(number):
return number * 2

print(double(5))
a = "abc"
print(a)
a = double Output:
print(a(17)) 10
b=a abc
print(b(12)) 34
24
So, in the code above the variable a was first assigned a
string. Then it was assigned the double function. From this
point on it’s possible to use a as the double function. Finally
the variable b was assigned the same reference as a, so it
also now points to the double function. In the end, we can
use double, a or b and they all reference the same function.
x = 123
 
def sum():
    x = 98
    print(x)
    print(globals()['x'])
  
print(x)
 
# assigning function
z = sum Output:
 # invoke function 123
z() 98
z() 123
98
123
parameterized function
def even_num(a):
    if a % 2 == 0:
        print("even number")
    else:
        print("odd number")
  
# assigning function
z = even_num
 
# invoke function with argument Output:
z(67) odd number
z(10) even number
z(7) odd number
def multiply_num(a):
    b = 40
    r = a*b
    return r
 
 # assigning function
z = multiply_num
 
# invoke function
print(z(6))
print(z(10))
print(z(100)) Output:
240
400
4000
Write a Python function that accepts a string and calculate the
number of upper case letters and lower case letters.
def string_test(s):
d={"UPPER_CASE":0, "LOWER_CASE":0}
for c in s:
if c.isupper():
d["UPPER_CASE"]+=1
elif c.islower():
d["LOWER_CASE"]+=1
else:
pass
print ("Original String : ", s)
print ("No. of Upper case characters : ", d["UPPER_CASE"])
print ("No. of Lower case Characters : ", d["LOWER_CASE"])

string_test('The quick Brown Fox')


Write a Python function that takes a list and returns a new list with
unique elements of the first list. 

def unique_list(l):
x = []
for a in l:
if a not in x:
x.append(a)
return x

print(unique_list([1,2,3,3,3,3,4,5]))
 Write a Python function to create and print a list where
the values are square of numbers between 1 and 30
(both included). 

def printValues():
l = list()
for i in range(1,21):
l.append(i**2)
print(l)

printValues()
LAMBDA EXPRESSIONS /
LAMBDA FUNCTIONS
What is a Lambda Function?

Lambda functions are similar to user-defined


functions but without a name. They're commonly
referred to as anonymous functions.

Lambda functions are efficient whenever you want


to create a function that will only contain simple
expressions – that is, expressions that are usually a
single line of a statement. They're also useful when
you want to use the function once.
How to Define a Lambda Function

You can define a lambda function like this:

lambda argument(s) : expression

1.lambda is a keyword in Python for defining the anonymous function.

2.argument(s) is a placeholder, that is a variable that will be used to


hold the value you want to pass into the function expression. A
lambda function can have multiple variables depending on what you
want to achieve.

3.expression is the code you want to execute in the lambda function.


Note : Anonymous function does not have a return
keyword. This is because the anonymous function will
automatically return the result of the expression in
the function once it is executed.
Assume I want to write a function that returns twice the number I
pass it. We can define a user-defined function as follows:

Now for a lambda function. We'll


def f(x): create it like this:
return x * 2

f(3) lambda x: x * 3
Let's see an example,

greet = lambda : print('Hello World')

Here, we have defined a lambda function and assigned it


to the variable named greet.
To execute this lambda function, we need to call it.

Here's how we can call the lambda function

# call the lambda

greet()
# lambda that accepts one argument

greet_user = lambda name : print('Hey there,', name)

# lambda call

greet_user('Delilah')

In the above example, we have assigned a lambda


function to the greet_user variable.
Here, name after the lambda keyword specifies that the
lambda function accepts the argument named name.
str1 = ‘Pythonisimpressive'
 
# lambda returns a function object

rev_upper = lambda string: string.upper()[::-1]

print(rev_upper(str1))
Condition Checking Using Python lambda function

format_numeric = lambda num: f"{num:e}" if isinstance(num, int) else f"{num:,.2f}"


 
print("Int formatting:", format_numeric(1000000))
print("float formatting:", format_numeric(999999.789541235))

Output:
Int formatting: 1.000000e+06
float formatting: 999,999.79
Python Lambda Function with if-else
Here we are using Max lambda function to find the
maximum of two integers.

Max = lambda a, b : a if(a > b) else b


 
print(Max(1, 2))
Python Lambda with Multiple statements
Lambda functions does not allow multiple statements, however, we
can create two lambda functions and then call the other lambda
function as a parameter to the first function. Let’s try to find the
second maximum element using lambda.

List = [[2,3,4],[1, 4, 16, 64],[3, 6, 9, 12]]


 
# Sort each sublist
sortList = lambda x: (sorted(i) for i in x)
 
# Get the second largest element
secondLargest = lambda x, f : [y[len(y)-2] for y in f(x)]
res = secondLargest(List, sortList)
 
print(res)
Output:
[3, 16, 9]
Lambda Function with Iterables

An iterable is essentially anything that consists of a


series of values, such as characters, numbers, and so on.
In Python, iterables include strings, lists, dictionaries,
ranges, tuples, and so on.

When working with iterables, you can use lambda


functions in conjunction with two common
functions: filter() and map().
Filter()
When you want to focus on specific values in an iterable, you can use
the filter function. The following is the syntax of a filter function:

filter(function, iterable)

As you can see, a filter function requires another function that


contains the expression or operations that will be performed on the
iterable.
For example, say I have a list such as [1, 2, 3, 4, 5, 6, 7, 8, 9,
10]. Now let's say that I’m only interested in those values in
that list that have a remainder of 0 when divided by 2. I can
make use of filter() and a lambda function.
Firstly I will use the lambda function to create the expression
I want to derive like this:
lambda x: x % 2 == 0
Then I will insert it into the filter function like this:
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filter(lambda x: x % 2 == 0, list1)
>> <filter at 0x1e3f212ad60>
# The result is always filter object so I will need to convert it
to list using list()
list(filter(lambda x: x % 2 == 0, list1))
Filter all people having age more than 18, using
lambda and filter() function:
ages = [13, 90, 17, 59, 21, 60, 5]
 
adults = list(filter(lambda age: age > 18, ages))
 
print(adults)

Output:
[90, 59, 21, 60]
Write a lambda function that checks if a number is
positive, like lambda x: x > 0, and use it
with filter to create a list of only positive numbers.

my_list = [18, -3, 5, 0, -1, 12]

new_list = list(filter(lambda x: x > 0, my_list))

print(new_list) # [18, 5, 12]


The map() function in Python takes in a function
and a list as an argument. The function is called
with a lambda function and a list and a new list is
returned which contains all the lambda modified
items returned by that function for each item.

Map Function's syntax is as follows:


map(insert function here, insert iterable here)
Map
What If I ask you to find a square of the first five odd
numbers. You would probably write something like
the following:

squares = []
for n in range(1, 10, 2):
squares.append(n ** 2)
The loop iterates through 1 to 10 with a step size of 2
and appends the square of each number to squares.
The approach shown above can be improved using
list comprehension but that too will use for loop. So,
there’s a faster way to do the same without using
explicit for loop. Python’s map function does that. It
transforms the given iterable based on the given
condition.
The syntax for the map function is as follows:

map(function, iterable)
The map function takes in a function and
an iterable(list, tuple, etc.) as an input; applies passed
function to each item of an iterable and returns a map
object(an iterator).
Let us re-implement the above example using the
map function.

def square(n):
return n ** 2
squares = map(square, range(1, 10, 2))
squares
# returns map object
list(squares)
# Output: [1, 9, 25, 49, 81]
Iterating Over a Dictionary Using Map and Lambda

dict_a = [{'name': 'python', 'points': 10}, {'name': 'java', 'points': 8}]

map(lambda x : x['name'], dict_a)


# Output: ['python', 'java']

map(lambda x : x['points']*10, dict_a)


# Output: [100, 80]

map(lambda x : x['name'] == "python", dict_a)


# Output: [True, False]
Here, the lambda function is applied on a given iterable
which returns a map object and is later converted into
the list.

squares = list(map(lambda n: n ** 2, range(1, 10, 2)))

The map function can have multiple iterables. Let’s take


a quick example to demonstrate the same.

num1 = [2, 4, 9]
num2 = [3, 8, 5]
result = list(map(lambda x, y: x + y, num1, num2))
result
# Output: [5, 12, 14]
Convert the list of string values to lowercase

myStrings = ["Python","Java","PHP","Scala"]

print("Original:",myStrings)

# Use lower() function

lower_result = list(map(lambda x: x.lower(), myStrings))

print("Result:",lower_result)
# Lambda example with two arguments

add = lambda x, y : x + y

print(add(10, 20))

#Output:
#30
Alternatively, you can also write the statement as
follows. This is called inline execution. For inline
invocation, we surround the lambda function with
parentheses and place the values for the arguments
next to it enclosed within parentheses.

result = (lambda x, y : x + y)(10,20)

print(result)
Libraries and Modules

Libraries and Modules make the life of a


programmer smooth.
When you are working with projects, you may
encounter scenarios where you won’t be able to
solve with the standard coding of a
programming language. We need some libraries
and modules to overcome those problems.
Luckily 
Python supports a plethora of modules and
libraries
. Python has built-in modules as well
as third-party libraries and modules for the
development. We will see both integrated
and third-party modules, which are very
beneficial for Python projects. 
Modules:
Modules are a collection of related codes that are
packed under a Python program. Programmers may
decide whether to define functions, classes, or
variables within a module. It’s also perfect to
accommodate runnable codes within modules. In other
words, they are Python files containing valid Python
definitions and statements. These are normal file
names that use the suffix .py when they are created.
Clustering the related code into a module makes the
code more straightforward to understand and
implement. It also prepares the code organized in a
logical manner.
Libraries:
A library is an umbrella term that comprises a reusable set
of Python code/instructions. Usually, a Python library is a
collection of related modules bundled together under one
single name. It is popularly used by developers to share
reusable code with the community. This eliminates the
necessity for writing any Python code from scratch.
Developers and community researchers can create their
own set of useful functions that are related to the same
domain. Standard libraries come bundled with the Python
interpreter when programmers and developers install it in
their system. Some common examples of Python libraries
are: matplotlib, Pygame, Pytorch, Requests, Beautifulsoap,
etc.
Difference between Modules and Libraries:
Modules Libraries
A module is a collection of A Python library is a set of
code or functions that uses related modules or packages
the .py extension. bundled together.
It is mostly used by the
It is used by the programmers community members,
as well as the developers. developers, and researchers.
Use of modules makes reading Libraries do not contribute in
the code easier. better readability.
Modules logically cluster the Libraries make the collection
functionality that of logically related code
programmers can import to reusable for the programming
reuse their code or set of language users, developers,
statements. and other researchers.
Whenever a programmer
imports a module in a Python We have to install the libraries
program, the interpreter scans in our Python project before
for several locations to look using its modules or packages.
Usually, we use the pip install
for the module's definition or command.
body.
When a module is not found
by Python's import statement, Whenever the Python
it searches for each directory
interpreter do not found the
within the shell variable i.e., Python library associated with
PYTHONPATH. The the project, it shows an error
PYTHONPATH is Python's message and the program
environment variable
consisting of a list of ends abruptly.
directories.
Modules are mostly written Libraries, mainly standard
using valid Python libraries, are mostly written
statements or codes. using C language or Python.
The main focus of creating
modules is to avoid DRY Libraries do not have any
(Don’t Repeat Yourself). such objective.

We can use the Python’s There is not such explicit


built-in dir() function to function that can return the
number of modules a library
return a sorted list of strings
holding the function names contain. Still, programmers
defined within a module. can use the help() to extract
some information.
Example of popular built-in Example of popular built-in
Python modules are os, sys, Python libraries are Pygame,
math, random, etc. Pytorch, matplotlib, etc.
Third-Party Tools
Python has a large standard library, providing many
useful modules right out of the box. But it will never have
everything you need. Python also has a huge collection
of third-party modules available for you to install.

pip
Python comes with a package manager called pip. The
name is a recursive acronym short for Pip Installs
Packages.
If you’re using a recent version of Python 3 (3.4 or
higher), then you already have a pip3 command that will
install packages.
Installing packages is as easy as:

pip install the_package_name

PyPI

Python packages are available from the Python


Package Index, or PyPI, pronounced Pie-Pee-Eye. Pip
automatically knows to install from PyPI, but you can
manually search or browse PyPI if you need to.
Popular third-party libraries
PyPI has thousands of packages, it would be impossible to summarize them all.
But there are a handful of go-to packages that many people use for common
tasks:
requests
scrapy
Twisted
Pillow
lxml
PyYAML
Django, Flask, Pyramid
SQLAlchemy
numpy, scipy, pandas
pytest, tox, coverage, mock
six
Jinija2
cryptography
pylint, flake8, pep8
pymongo, redis, MySQL-Python, psycopg2
Emoji
Emojis have become a way to express and to enhance
simple boring texts. Now, the same gems can be used in
Python programs too. Yes, really! You now have the
ultimate power to use emojis in your code. For
this, emoji module is needed to be installed.

In terminal. Use:
pip install emoji
To upgrade to the latest packages of emojis. Here’s how
it can be done:

pip install emoji --upgrade


from emoji import emojize

print(emojize(":thumbs_up:"))
Alternatively, encode() function can be used from
emojis module to convert Unicode to emojis:

import emojis

emojified = emojis.encode("There is a :snake: in my boot !")


print(emojified)
:smile: :snowman:
:laughing: :rabbit:
:smiley: :racehorse:
:confused: :dolphin:
:musical_note: :palm_tree:
:punch: :sun_with_face:
:running: :tada:
:raising_hand: :computer:
:eyes: :loudspeaker:
:sunny: :key:
sys.exit()

You may have used the sys module before but


did you know you could exit your program early
using it? We can cause the program to
terminate by calling the sys.exit() function. Since
this function is in the sys module, firstly,
the sys module should be imported. This is not a
third party module and comes built-in with
Python so there is no need to install it.
urllib
Urllib module is the URL handling module for python. It is
used to fetch URLs (Uniform Resource Locators). It uses
the urlopen function and is able to fetch URLs using a
variety of different protocols.
Urllib is a package that collects several modules for
working with URLs, such as:
urllib.request for opening and reading.
urllib.parse for parsing URLs
urllib.error for the exceptions raised
urllib.robotparser for parsing robot.txt files
This will import urlopen
# class from urllib module
from urllib.request import urlopen
  
 page = urlopen("http://geeksforgeeks.org/")
print(page.headers)
UNIT 2
COMPLETED

You might also like