0% found this document useful (0 votes)
5 views23 pages

Chapter 4. Introduction To Python Libraries

Chapter 4 introduces Python libraries, which are collections of related modules that simplify programming by allowing code reuse. It covers the Python Standard Library, third-party libraries, and their applications in various fields such as data science, machine learning, and web scraping. The chapter also provides examples of how to install and use these libraries in Python programs.

Uploaded by

Yến Hoàng
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views23 pages

Chapter 4. Introduction To Python Libraries

Chapter 4 introduces Python libraries, which are collections of related modules that simplify programming by allowing code reuse. It covers the Python Standard Library, third-party libraries, and their applications in various fields such as data science, machine learning, and web scraping. The chapter also provides examples of how to install and use these libraries in Python programs.

Uploaded by

Yến Hoàng
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 4

Introduction to Python Libraries

1
Chapter Content

• 1. Python Libraries
• 2. Use of Libraries in Python Program
• 3. The Python Standard Library
• 4. The 3rd Party Libraries

Python 2
1. Python Library

• A Python library is a collection of


related modules containing
bundles of code that can be used
repeatedly in different programs.
• It makes Python Programming
simpler and convenient for the
programmer when we needn’t to
write the same code again and
again for different programs

Python 3
2. Use of Libraries in Python Program
• Importing the libraries:
# Importing math library
import math
A = 16
print([Link](A))

• Importing specific items from a library module:


# Importing specific items
from math import sqrt, sin
A = 16
B = 3.14
print(sqrt(A))
print(sin(B))
Python 4
3. Python Standard Library
• Python Standard library plays a very important role to access to the system
functionalities of python
• [Link]
• The library contains more than 200 core prebuilt pretested modules (written in C) that
are provided as part of the default python installation itselt

Python 5
4. The 3rd Party Libraries

• Are open-source libraries (packages) developed by somebody else and


made available for download using a standard interface.
• Used to create applications and models in a variety of fields, e.g.
machine learning, data science, data visualization, image and data
manipulation, and many more.
• Find, install and publish Python packages with the Python Package
Index [Link]
• Using pip to install packages
pip install <packages>

Python 6
4. The 3rd Party Libraries: Data Science & AI

Data mining
from data
sources.

Python 7
4. The 3rd Party Libraries: Data Mining

• There are several Python tools for scraping data used in Python
machine learning models.
• These libraries are known for web crawling, data scraping and arrange
it into the required format

Python 8
4. The 3rd Party Libraries: Data Mining

• There are mainly two ways to extract data from a website:


• Use the API of the website (if it exists). For example, Facebook
has the Facebook Graph API which allows retrieval of data posted
on Facebook.
• Access the HTML of the webpage and extract useful
information/data from it. This technique is called web scraping or
web harvesting or web data extraction.

Python 9
4. The 3rd Party Libraries: Data Mining
install: pip install requests
• Example pip install bs4

from bs4 import BeautifulSoup


import requests
# Fetch the web page
url = "[Link]
response = [Link](url)
data = [Link]
# Parse the HTML content
soup = BeautifulSoup(data, '[Link]')
# Extract specific elements
titles = soup.find_all('h1')
for title in titles:
print([Link])

Python 10
4. The 3rd Party Libraries: Data Analytics
• There are several Python tools for processing and visualizing data.

Python 11
4. The 3rd Party Libraries: Machine Learning (Traditional Algorithms)

• Provide the implementation of traditional Machine Learning


• Algorithms:
• Classification: SVM, Random Forest, Decision Tree, etc…
• Clustering: K-Mean, etc ...
• Except neural networks.

Python 12
4. The 3rd Party Libraries: Deep Learning

Python 13
4. The 3rd Party Libraries: Reinforcement Learning

• There are libraries designed to have all the necessary tools to both
implement and test Reinforcement Learning models.

Python 14
4. The 3rd Party Libraries: NLP Machine Learning

• There are many Python libraries that can be used for practically
implementing natural language processing (NLP) and text mining
tasks.

Python 15
4. The 3rd Party Libraries: Computer Vision

• Python provides several computer vision libraries and frameworks for


developers to help them automate tasks, which includes detections
and visualisations.

Python 16
4. The 3rd Party Libraries: Example
Install: pip install speedtest-cli

import speedtest as st
def Speed_Test():
test = [Link]()
down_speed = [Link]()
down_speed = round(down_speed / 10 ** 6, 2) # Result
print("Download Speed in Mbps: ", down_speed)
Download Speed in Mbps: 20.57
up_speed = [Link]()
up_speed = round(up_speed / 10 ** 6, 2) Upload Speed in Mbps: 18.47
print("Upload Speed in Mbps: ", up_speed)
Ping: 26.177
ping = [Link]
print("Ping: ", ping)

Speed_Test()

Python 17
4. The 3rd Party Libraries: Example
Install: pip install wifi-qrcode-generator

from wifi_qrcode_generator import wifi_qrcode

wq = wifi_qrcode("danang123", hidden=False,
authentication_type="WPA",password="ptc555" )
qrcodeimage = wq.make_image()
[Link]("[Link]")

Python 1818
4. The 3rd Party Libraries: Example

import qrcode
Install: pip install qrcode
from PIL import Image pip install pillow
url = [Link] # Generate QR code for a URL

qr = [Link](version=1, error_correction=[Link].ERROR_CORRECT_L,
box_size=8, border=5)
qr.add_data(url)
[Link](fit=True)

# Create an image with logo


image = qr.make_image(fill_color="black", back_color="pink")

logo = [Link]("[Link]") # Add logo to the QR code


logo_size = [Link][0] // 4

# resizing with anti-aliasing


logo = [Link]((logo_size, logo_size), [Link])
[Link](logo, (([Link][0] - [Link][0]) // 2, ([Link][1] - [Link][1]) // 2))

[Link]("qr_code.png") # Save the image


[Link]("qr_code.png") # Open the image
Python 19
4. The 3rd Party Libraries: Example

Install: pip install python-barcode


import barcode

from [Link] import ImageWriter


from [Link] import Image, display

barcode_format = barcode.get_barcode_class('ean13')
barcode_number = '1234567890128'
barcode_image = barcode_format(barcode_number,
writer=ImageWriter())
barcode_filename = 'barcode_image'
barcode_image.save(barcode_filename)

display(Image(filename=f'{barcode_filename}.png'))

Python 20
4. The 3rd Party Libraries: Example
Install: pip install pickle

import pickle import pickle import pickle

my_dict = {'name': 'John', 'age': 30, my_list = ['apple', 'banana', 'cherry'] my_tuple = (10, 20, 30, 'Hello')
'city': 'New York'} with open('[Link]', 'wb') as file: with open('[Link]', 'wb') as file:
with open('[Link]', 'wb') as file: [Link](my_list, file) [Link](my_tuple, file)
[Link](my_dict, file)
with open('[Link]', 'rb') as file: with open('[Link]', 'rb') as file:
with open('[Link]', 'rb') as file: loaded_list = [Link](file) loaded_tuple = [Link](file)
loaded_dict = [Link](file)
print("Loaded List:", loaded_list) print("Loaded Tuple:", loaded_tuple)
print("Loaded Dictionary:", loaded_dict)

# Loaded Dictionary: {'name': 'John', #Result: # Loaded List: ['apple', # Resulte: Loaded Tuple: (10, 20, 30,
'age': 30, 'city': 'New York'} 'banana', 'cherry'] 'Hello')

Saving and Loading Saving and Loading Saving and Loading


a Dictionary a List a Tuple

Python 21
4. The 3rd Party Libraries: Example
Install: pip install pickle import pickle
list_data = [1, 2, 3]
dict_data = {'a': 1, 'b': 2}
import pickle
string_data = "Hello, World!"
class Person:
def __init__(self, name, age):
with open('[Link]', 'wb') as file:
[Link] = name
[Link](list_data, file)
[Link] = age
[Link](dict_data, file)
[Link](string_data, file)
def __repr__(self):
return f"Person(name={[Link]}, age={[Link]})"
with open('[Link]', 'rb') as file:
loaded_list = [Link](file)
person = Person('Alice', 25)
loaded_dict = [Link](file)
with open('[Link]', 'wb') as file:
loaded_string = [Link](file)
[Link](person, file)
print("Loaded List:", loaded_list)
with open('[Link]', 'rb') as file:
print("Loaded Dictionary:", loaded_dict)
loaded_person = [Link](file)
print("Loaded String:", loaded_string)
print("Loaded Person:", loaded_person)
# Loaded Person: Person(name=Alice, age=25) Loaded List: [1, 2, 3]
Loaded Dictionary: {'a': 1, 'b': 2}
Saving and Loading a Class Loaded String: Hello, World!
Python 2222
The end of Chapter

Python 2323

You might also like