0% found this document useful (0 votes)
144 views

Python Programming (21EC643) (Module-5) by Prof. Sujay Gejji

Python Programming (21EC643) (Module-5) Notes by Prof. Sujay Gejji VTU Notes Python Programming Notes by Prof. Sujay Gejji

Uploaded by

sujay g
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views

Python Programming (21EC643) (Module-5) by Prof. Sujay Gejji

Python Programming (21EC643) (Module-5) Notes by Prof. Sujay Gejji VTU Notes Python Programming Notes by Prof. Sujay Gejji

Uploaded by

sujay g
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

PYTHON PROGRAMMING (QUESTION & ANSWERS)

Module 5

Network Programming & SQL with Python


Question Bank Solution
Questions based on Socket (Retrieval of webpages using socket)
What is socket? Explain how socket connection can be established to the internet using Python code over the
TCP/IP connection and the http protocol to get the web document July 18

Define socket? Write a python program that makes a connection to a webserver and follows the rules of
HTTP protocol to request a plain test document and display what server sends back Jan 20
Define socket? Write a python program that makes a connection to a webserver and follows the rules of
HTTP protocol to request a plain test document and display what server sends back July 20
What is socket? Write and explain a program in python, that connects to web server requests a document,
and display what the server sends back. Jan 22
Define Socket. Explain how socket connection can be established to the internet using Python code over
TCP/IP connection and http protocol to get the web document July 22
Solution:
Sockets are endpoints in bidirectional communication channel.
o The sockets (endpoints) can communicate within a process, between processes in the same
machine, or between the processes on the different machines.
o Single socket provides a two-way connection, ie we can both read from and write to the same
socket.
o The Python provides built-in library called socket to make network connections and to
access data over other sockets

Creating the socket:


o To create socket, we have to use the python socket method from socket library(module).
Syntax of Socket
socket.socket(socket_family, socket_type)
Family Address family provides different types of addresses during inter-
process communication. Mainly 2 families: AF_INET & PF_INET
AF_INET = Address Format, Internet
AF_INET refers to IP addresses from the internet (IP V4 or IP V6)

Type Type of transport layer protocol TCP or UDP.


SOCK_STREAM for TCP (connection-oriented protocols)
SOCK_DGRAM for UDP (connectionless protocols)

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Example : The Simple HTTP Web Browser


Program: To retrieve webpage /text from the web pages using HTTP
import socket #import the socket library

#To create the socket


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Now to connect to the website (http port 80)


s.connect(('data.pr4e.org', 80))

#Creating request to web page by using GET method of HTTP protocol


cmd='GET /romeo.txt HTTP/1.0\r\n\r\n'

#Data sent should be in bytes format, so convert from string to byte


cmd= cmd.encode()

#Now to send request to the server (website)


s.send(cmd)

#Creating variable of type bytes to store the received data


data=b''
#Now to receive the data from server
while True:
chunk = s.recv(512) #receiving the data of 512 bytes chuncks
if (len(chunk) < 1): #if the data is less than 1 byte
break #comeout of the loop
data=data +chunk
s.close()

a=data.decode() #convert back to string


print(a)

Explanation:
To create socket and send the request
1. First to import the socket library
2. Create the socket using socket.socket () method
3. Now to connect to the website using connect () method
4. To send request to webpage:
i. First create request for the web page using GET method of the HTTP and store it in
cmd variable.
ii. Data sent should be in bytes format, so convert string into byte using encode () method
and again store back in cmd variable.
iii. Now send request (stored in cmd variable) to the server using send () method.

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Note ; HTTP/1.0: This specifies the version of the HTTP protocol i.e HTTP version 1.0.
Here \r\n means next line and \r\n\r\n represents the end of HTTP header.
5. To receive the data from webpage
i. Create the one variable “data” of type byte to store the received data
ii. We have to receive the data in 512-character chunks at time, so to extract entire data
we have to use loop (to repeat the process)
6. Close the connection using close method

Note:
The various functions used:
1. socket.socket () :
The socket method from the socket library(module) is used to create the socket
2. connect () :By using this method client establishes a connection with server.
3. send() : The send () method of Python's socket module is used to send data from one socket
to another socket.
4. encode() : The data sent should be in bytes format. String data can be converted to bytes by using the
encode() method
5. recv() : This function is used to receive the contents of the web page
6. close() : This function is used to close the socket connection

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Write a python program retrieve the image (Binary file) from web using HTTP socket

import socket

# Create the socket


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to the website (http port 80)


s.connect(('data.pr4e.org'), 80)

# Creating request to web page by using GET method of HTTP protocol


cmd = 'GET /dhoni.jpg HTTP/1.0\r\n\r\n'

# Data sent should be in bytes format, so convert from string to byte


cmd = cmd.encode()

# Send request to the server (website)


s.send(cmd)

# Creating variable of type bytes to store the received data


data = b''

# Now to receive the data from server


while True:
chunk = s.recv(512) # Receiving the data of 512 bytes chunks
if (len(chunk) < 1): # If the data is less than 1 byte
break # Come out of the loop
data = data + chunk

# Close the socket


s.close()

#To remove the unwanted data (header of image)


# First to find the end of the header (it contains 4 bytes)
header_end = data.find(b'\r\n\r\n') + 4

# Extract the binary image ; start after the header end


image_data = data[header_end:]

# Save the image to a file


f1 = open('image.jpg', 'wb')
f1.write(image_data)
f1.close()

Note: for image we can not directly print, it has to be saved in local file.

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

To retrieve data from Webpage using urllib


Write a python code to read the text file from web using urllib and retrieve the data of the file. Also
compute the frequency of each word in the file. Jan 20
Write a python code for retrieving the romeo.txt file from the web and compute the frequency of each word
in the file. June 20
Write a program to retrieve data text file using URLLIB on web and compute frequency of each word in file.
Jan 22

• Python has urllib library, which is used to handle the URLs and to make the HTTP request
• It provides a simple way to interact with web servers and retrieve data from the internet.
• By using urllib library, we can, send GET requests, receive data, parse data, and modify the
headers, error handling etc. urllib handles all these tasks related to HTTP protocol
• Urllib takes care of creating sockets and connecting to servers, so we don't need to worry
about creating sockets, sending requests, and receiving responses.
urllib.request :
• It is default module used for opening HTTP URLs.
• The urllib library has many modules to handle the URLs.
o urllib.request : for opening and reading. To open the file: urllib.request.urlopen().

Program : To retrieve web pages using urllib


# Step 1: Import the urllib library
import urllib.request

# Step 2: Specify the URL of the text file


url = 'http://data.pr4e.org/romeo.txt'

# Step 3: Open the URL & save response in variable f1


f1 = urllib.request.urlopen(url)

# Read and decode the and store it variable “content”


content = f1.read().decode()

# Now to store contents in local file


f2 = open('myfile.txt', 'w')
f2.write(content) # Pass the content to write
f2.close()

OR another textbook method


import urllib.request

file1 = urllib.request.urlopen('http://data.pr4e.org/romeo.txt')

for line in file1:


line=line.decode() #To convert to string (from byte)
print(line)

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Example: To write a program to retrieve the data and compute the frequency of each word
# Step 1: Import the urllib library
import urllib.request

# Step 2: Specify the URL of the text file


url = 'http://data.pr4e.org/romeo.txt'

# Step 3: Open the URL & save response in variable f1


f1 = urllib.request.urlopen(url)

# Read and decode the content


content = f1.read().decode()

# Now to store contents in local file


f2 = open('myfile.txt', 'w')
f2.write(content) # Pass the content to write
f2.close()

# Compute the frequency of each word


words = content.split() # Split the content into words
word_freq = {} # Create a dictionary to store word frequencies

for word in words:


if word in word_freq:
word_freq[word] += 1 # Increment frequency
else:
word_freq[word] = 1 # Add new word to dictionary
print(word_freq)

Write a python code to read the Binary (Image) file from web using urllib
# Step 1: Import the urllib library
import urllib.request

# Step 2: Specify the URL of the binary (image) file


url = 'http://data.pr4e.org/romeo.jpg'

# Step 3: Open the URL & save response in variable file1


f1 = urllib.request.urlopen(url)

# Read and decode the content


content = f1.read()

# Now to store contents in local file


f2 = open('myimage.jpg', 'wb')
f2.write(content) # Pass the content to write
f2.close()

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Web scraping

Explain the Web scraping and parse HTML page using regular expression
Web scraping:
Process of extracting data from websites, web pages, or online documents and looking for specific
patterns or information.
Example: Google extracts links from web pages to determine page importance and rank them.

Parsing HTML:
It focusses on extracting the data from HTML content or HTMLpage. HTML parsing is part of web
scraping.
Example : To extract all hyperlinks from a web page

Methods to Parse HTML


• Using Regular Expressions
• Xpath
• Using Beatifulsoap library etc

Parsing HTML using Regular Expression:


Note:
To parse HTML page, first we should know the syntax of an HTML file.
Sample HTML file

Here,
<h1> ……. </h1> : Header tags (beginning and end)
<p>…… </p> : Paragraph tags (beginning and end)
<a> ……</a> Anchor tags where URLs are placed
href : Attribute used to define the URL in anchor tags. It indicates start of hyperlink

Regular Expression Pattern: “href=http://.*? ”

href=" : Matches the start of a link.


http://: Specifies that the URL should start with http://.
.*?: .matches any characters (except a newline) between the http:// and ". The ? makes it match
as little as possible.

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Program: To extract the URL from the webpage or HTML

import urllib.request
import re

url = "http://www.python.org"
s = urllib.request.urlopen(url) # Open the webpage

html = s.read() # Read the webpage content

# Convert(decode) the byte content to a string


html = html.decode()

# Define the regular expression pattern and compile it


pattern = r' href="(http://.*?)" ' # Fixed pattern for HTTP links
comp= re.compile(pattern)

links = comp.findall(html) # Find all matching URLs from HTML

# Print each URL


for link in links:
print(link)

Explain the Web scraping and parse HTML using Beautiful Soap Library

Web scraping:
Process of extracting data from websites, web pages, or online documents and looking for specific
patterns or information.
Example: Google extracts links from web pages to determine page importance and rank them.

Parsing HTML:
It focusses on extracting the data from HTML content or HTML page. HTML parsing is part of web
scraping.
Example : To extract all hyperlinks from a web page

Methods to Parse HTML


• Using Regular Expressions
• Xpath
• Using BeatifulSoap library etc

Parsing HTML using Beautiful Soup Library


BeautifulSoup is a library specifically designed for parsing HTML and XML documents.
It allows us to easily navigate and search through HTML elements.

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Program:

import urllib.request
from bs4 import BeautifulSoup

url = "http://www.python.org"
s = urllib.request.urlopen(url) # Open the webpage

html = s.read() # Read the webpage content

# Convert(decode) the byte content to a string


html = html.decode()

# Parse the HTML content using BeautifulSoup


soup = BeautifulSoup(html, 'html.parser')

# Find all anchor tags with href attributes


links = soup.find_all('a', href=True)

# Print each URL


for link in links:
print(link['href'])

Note:
find_all() method is used to locate all <a> tags with href attributes, which typically contain URLs.

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Questions based on XML


Write a note on XML. Design python program to retrieve a node present in XML tree. Jan 2019
What is XML? How is it used in python? Explain parsing of XML with example. Jan 2020
Write a note on XML. June 2020
What is XML? Explain the process of parsing XML using a tree representation. Jan 2022
Note on XML:
• XML is the markup language similar to HTML. It is structured and tag-based language
• This is mainly used to exchange or transmit the data across the web. It is most suitable
for exchanging document style data.
• The XML is similar to HTML, but the difference is, HTML has predefined tags, but in
XML user or developer can create his own tags (user defined). That is why it is called
EXTENSIBLE
• XML is more structured than HTML.
• The most important part of XML are XML tags.
• Two important tags of XML are: start tag & end tag
• The content (data) or instructions have to be placed between these starts and stop tags.
• Start Tag: Every XML element or content begins with start-tag.
Example: <name>
• End Tag: Every element that has a start tag should end with an end-tag.
Note: end tag includes a ("/")
Example </name>
• Simple example for xml document

• We can think XML document as a tree structure. Here Player is top tag or root tag(node)
and other tags such as name, age are child elements.

Tree structure above XML code.

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Parsing XML (To retrieve the node present in the XML tree)
(Parsing means extracting the data from text)
• To parse XML document, first XML document has to be converted into Tree structure (Tree
of nodes) and then required node of the tree has to be extracted(parsed).
• To convert the XML into Tree structure, inbuilt functions of Element Tree library are used.
So, first we need to import Element Tree library (xml.etree.ElementTree)
• Mainly 3 functions of Element Tree library are used.
▪ fromstring(): It is used to convert XML code into a tree-structure (tree of XML
nodes.)
▪ find () function is used extract the node (value of the tag) from XML tree
▪ get () function is used to extract the attribute value.
Example:

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Output:
Name:Dhoni
Age: 40
Runs: 15000
Attribute: yes

Explanation :

ET.fromstring(data) => converts the XML data (stored in the data variable) into an Element Tree structure
(tree).

tree.find('name').text =>finds the <name> tag in the XML tree and returns its content ('Dhoni')

tree.find('age').text =>finds the <age> tag in the XML tree and returns its content ('40').

tree.find('runs').text => finds the <runs> tag in the XML tree and returns its text content ('15000').

tree.find('email').get('hide')=> finds the <email> tag in the XML tree and retrieves the value of the 'hide'
attribute ('yes' ).

Explain the significance of XML over the web development. Illustrate with an example. July 2018
Importance of XML in Web Applications:
XML has several important web applications; such as
• Web Publishing: XML allows user to create interactive pages that can display information.
• Searching: In XML data is placed between tags, so it makes searching easy.
For example: Consider XML page containing information of different cricket players such as
player name, age, runs scored etc. Now if we search the pages with player name, then it will
directly search into name tags and retrieve related data
• Independent Data Interchange: Almost all other programming languages can parse (extract
data from) XML. So, XML is highly used for interchanging data between web application
using APIs.
• Granular Updates: Updating Entire document makes process slow, as the entire document
needs to be refreshed from the server. So, in updating/uploading an XML document, only the
part (granular) of the document is updated/uploaded.
• Metadata Applications:
▪ Assists in Data Assessments and Aggregation
▪ Custom Tags

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Explain the JSON and parsing of JSON

JSON (JavaScript Object Notation)


Definition:
• JSON stands for JavaScript Object Notation.
• It is a standard text-based data format based on JavaScript syntax.
• JSON format is inspired by the syntax of Python dictionaries and lists.
• It is a standard data format used to store and retrieve data.
• JSON is mainly used to transmit data in web applications.

Syntax:
• JSON stores data in the form of key-value pairs (similar to Python dictionaries).
• All items are written within curly braces {}.
• Each key-value pair is separated by a comma.
• The key should be a string, but the value can be of any data type.

Example:

{
"Player": {
"name": "Dhoni",
"age": "40",
"runs": "15000",
"birthplace": "Ranchi"
}
}

Nested JSON
{
"Team": {
"India": {
"Player": {
"name": "Dhoni",
"age": "40",
"runs": "15000",
"birthplace": "Ranchi"
}
}
}
}

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Parsing of JSON
Parsing means extracting of retrieving data from JSON file

import json

# stotre the JSON in variable data as string using ‘’’


data = '''
{
"Player": {
"name": "Dhoni",
"age": "40",
"runs": "15000",
"birthplace": "Ranchi"
}
}
'''

# Load (convert) the JSON data into a Python dictionary


data_dict = json.loads(data)

# Obtain data from the dictionary using keys


info = data_dict["Player"]
name = info["name"]
age = info["age"]
runs = info["runs"]
birthplace = info["birthplace"]

# Print the extracted information


print("Name:", name)
print("Age:", age)
print("Runs:", runs)
print("Birthplace:", birthplace)

Difference between JSON and XML

1. Syntax:
* JSON: Lightweight, easy to read and write, uses curly braces {} and square brackets [].
* XML: More verbose, uses tags <> and closing tags </>.

JSON syntax:
{
"Player": {
"name": "Dhoni",
"age": "40",

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

"runs": "15000",
}
}

XML syntax:
<Player>
<name>Dhoni</name>
<age>40</age>
<runs>15000</runs>
</Player>

2. Data Structure:
* JSON: Based on JavaScript objects and arrays, supports nested data structures.
* XML: Based on markup language, uses elements and attributes to represent data.

3. Data Types:
* JSON: Supports strings, numbers, booleans, arrays, and objects.
* XML: Supports strings, numbers, and other data types through attributes and elements.

4. Size and Performance:


* JSON: Smaller in size, faster parsing and generation.
* XML: Larger in size, slower parsing and generation.

5. Human Readability:
* JSON: Easier to read and write, more human-friendly.
* XML: More difficult to read and write, more machine-friendly.

6. Platform Independence:
* JSON: Supported by most programming languages and platforms.
* XML: Also supported by most programming languages and platforms, but more widely used in enterprise
environments.

7. Namespaces:
* JSON: No built-in support for namespaces.
* XML: Supports namespaces to avoid element name conflicts.

8. Comments:
* JSON: No support for comments.
* XML: Supports comments using <!-- and -->.

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

10. Usage:
* JSON: Widely used in web development, APIs, and data exchange.
* XML: Widely used in enterprise environments, configuration files, and data exchange.

In summary, JSON is a lightweight, easy-to-read data format, while XML is a more verbose, feature-rich
markup language. The choice between JSON and XML depends on the specific use case and requirements.

Questions based on SQL


Brief on structured Query language, with suitable python program explain functions involved in creation of
database table in python. Jan 19
With appropriate code segment in python using SQLite, explain table creation, insertion, fetching and
deletion. Jan 2022
Write the four SQL commands needed to create and maintain data (Model QP)
(SQL: Structured query language):
• SQL is a language used to interact with databases.
• A database is an organized collection of interrelated data.
• SQL is used to perform operations like: storing, manipulating and retrieving the data in
databases.

• Basic SQL commands

Command Meaning Syntax


CREATE TABLE To Create a new table CREATE TABLE Player (name VARCHAR, age INT, runs
INT)
DROP TABLE Deletes table DROP TABLE Player
SELECT To Extract/retrieve data from SELECT * FROM Player
database
INSERT INTO To Insert new data INSERT INTO Player (name, age, runs) VALUES
('Dhoni', 35, 15000)
UPDATE To modify the existing data in UPDATE Player SET age = 45 WHERE name =
database 'Dhoni'
DELETE To Delete data from database DELETE FROM player WHERE name='Dhoni'

SQLite: It is a single file file-based, relational database management system (RDBMS)

SQLite with Python


Python has an inbuilt library called sqlite3 to handle SQLite databases. The library has methods to
perform operations on databases:

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

connect(): Creates and opens a database


cursor(): Creates a cursor object (file handle)
execute(): Executes SQL commands
commit(): Confirms execution of commands like INSERT, UPDATE, DELETE
close(): Closes the database connection

The various data types in SQLite are


1. INTEGER 2. TEXT 3. BOOLEAN 4. FLOAT 5. VARCHAR 6. NUMERIC

Example program:

import sqlite3

# Create and open a database


conn = sqlite3.connect('example.db')
cursor = conn.cursor()

# Create a table
cursor.execute('''
CREATE TABLE Player (
name VARCHAR,
age INT,
runs INT
)
''')

# Insert data
cursor.execute("INSERT INTO Player (name, age, runs) VALUES ('Dhoni', 38, 15000)")
cursor.execute (‘INSERT INTO Player (name, age, runs) VALUES ('Virat', 35, 14000)’)
cursor.execute (‘INSERT INTO Player (name, age, runs) VALUES ('Rohit', 34, 12000)’)

# Fetch data
cursor.execute("SELECT * FROM Player")
rows = cursor.fetchall()
for row in rows:
print(row)

# Update data
cursor.execute("UPDATE Player SET age = 45 WHERE name = 'Dhoni'")

# Delete data
cursor.execute("DELETE FROM Player WHERE name = 'Dhoni'")

# Commit changes
conn.commit()

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

# Close the connection


conn.close()

Note; for understanding (Only Write if asked for more marks)

Syntax of commands: (Creating the Table, inserting, extracting and deleting data)

1. Creating Table: CREATE TABLE command is used to create a new table.


CREATE TABLE Player (name VARCHAR, age INT, runs INT )

This command creates a table wit name Player with the attributes(columns) name, age and
runs. The name is of character data type and age, runs are of Integer type

2. Inserting data: Data is inserted using INSERT INTO command


INSERT INTO Player (name, age, runs) VALUES ('Dhoni', 35, 15000)

3. Extracting data: SELECT command is used to extract or fetch the data


SELECT * FROM Player #Retrieves all the data from the table Player
SELECT name, age FROM player #Retrieves only name and age

4. Updating value: UPDATE command is used to modify the existing data


UPDATE Player SET age = 45 WHERE name = 'Dhoni‘

5. Deleting data: DELETE command is used to delete the particular record


DELETE FROM player WHERE name='Dhoni'
DELETE FROM player # delete all data

6. Deleting Table: DROP TABLE is used to delete the table.


DROP TABLE Player

Explain the concept of basic data modelling using SQL & SQLite

Data modelling: It is the process of creating a conceptual representation of data structures and
relationships to store and manage data efficiently.

SQL and Data Modelling


SQL (Structured Query Language) is a language used to interact with databases, perform operations
like storing, manipulating, and retrieving data. SQLite is a file-based relational database
management system (RDBMS) that uses SQL.

Basic SQL commands

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Command Meaning Syntax


CREATE TABLE To Create a new table CREATE TABLE Player (name VARCHAR, age INT, runs
INT)
DROP TABLE Deletes table DROP TABLE Player
SELECT To Extract/retrieve data from SELECT * FROM Player
database
INSERT INTO To Insert new data INSERT INTO Player (name, age, runs) VALUES
('Dhoni', 35, 15000)
UPDATE To modify the existing data in UPDATE Player SET age = 45 WHERE name =
database 'Dhoni'
DELETE To Delete data from database DELETE FROM player WHERE name='Dhoni'

SQLite: It is a single file file-based, relational database management system (RDBMS)

SQLite with Python


Python has an inbuilt library called sqlite3 to handle SQLite databases. The library has methods to
perform operations on databases:
connect(): Creates and opens a database
cursor(): Creates a cursor object (file handle)
execute(): Executes SQL commands
commit(): Confirms execution of commands like INSERT, UPDATE, DELETE
close(): Closes the database connection

The various data types in SQLite are


1. INTEGER 2. TEXT 3. BOOLEAN 4. FLOAT 5. VARCHAR 6. NUMERIC

Example: To create the following table:

Table name: Player

Name age runs


Dhoni 38 15000
Virat 35 14000
Rohit 34 12000

It has 3 columns: 1. name (variable character ie VARCHAR), 2. age (int), 3. runs (int)

program:

import sqlite3

# Create and open a database


conn = sqlite3.connect('example.db')
cursor = conn.cursor()

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

# Create a table
cursor.execute('''
CREATE TABLE Player (
name VARCHAR,
age INT,
runs INT
)
''')

# Insert data
cursor.execute("INSERT INTO Player (name, age, runs) VALUES ('Dhoni', 38, 15000)")
cursor.execute (‘INSERT INTO Player (name, age, runs) VALUES ('Virat', 35, 14000)’)
cursor.execute (‘INSERT INTO Player (name, age, runs) VALUES ('Rohit', 34, 12000)’)

# Fetch data
cursor.execute("SELECT * FROM Player")
rows = cursor.fetchall()
for row in rows:
print(row)

# Update data
cursor.execute("UPDATE Player SET age = 45 WHERE name = 'Dhoni'")

# Delete data
cursor.execute("DELETE FROM Player WHERE name = 'Dhoni'")

# Commit changes
conn.commit()

# Close the connection


conn.close()

SQLite with Python (Embedding SQL commands in Python)

Simple programs:

Example1: Write SQL Program to create the table


import sqlite3
conn=sqlite3.connect(‘cricket.db’) #create & open database by name cricket and store in variable conn
cur=conn.cursor() # creating the cursor object : cur
cur.execute (‘CREATE TABLE Player (name TEXT, age INT, runs INT)’) #create table
conn.close() # close the database or connection

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Example 2: Write SQL Program to delete the table


import sqlite3
conn=sqlite3.connect(‘cricket.db’) #create & open database by name cricket and store in variable conn
cur=conn.cursor() # creating the cursor object : cur
cur.execute (‘DROP TABLE Player’) # Delete table
conn.commit() #It is for confirmation
conn.close() # close the database or connection

Example 3: Write SQL Program to create the table and to insert the values
import sqlite3
conn=sqlite3.connect(‘cricket.db’) #create & open database by name cricket and store in variable conn
cur=conn.cursor() # creating the cursor object : cur
cur.execute (‘CREATE TABLE Player (name TEXT, age INT, runs INT)’) #create table
cur.execute (‘INSERT INTO Player (name, age, runs) VALUES ('Dhoni', 35, 15000)’)
conn.close() # close the database or connection

Example 4: Write SQL Program to update or modify the values


import sqlite3
conn=sqlite3.connect(‘cricket.db’) #create & open database by name cricket and store in variable conn
cur=conn.cursor() # creating the cursor object : cur
cur.execute (‘UPDATE Player SET runs 20,000 WHERE ‘name’ = virat)
conn.commit() #It is for confirmation
conn.close() # close the database or connection

Example 5: Write SQL Program to delete the data


import sqlite3
conn=sqlite3.connect(‘cricket.db’) #create & open database by name cricket and store in variable conn
cur=conn.cursor() # creating the cursor object : cur
cur.execute (‘DELETE FROM Player WHERE= ‘Dhoni’)
conn.commit() #It is for confirmation
conn.close() # close the database or connection

Example 6: Extracting and displaying the data


import sqlite3
conn=sqlite3.connect(‘cricket.db’) #create & open database by name cricket and store in variable conn
cur=conn.cursor() # creating the cursor object : cur
cur.execute (‘SELECT name, age FROM Player’)
for row in cur
print(row[0])
print(row[1])
conn.close() # close the database or connection

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Define cursor? Explain connect, execute and close command of databases with suitable
example
• A cursor is a database object that allows us to work with the result of a SQL command.
• When a SQL statement is executed, the result is temporarily stored in memory within the
cursor.
• Cursors is used to navigate through the rows of the result, typically one row at a time.
• They act as pointers, allow us to fetch individual rows, move to the next row, and process
each row in a loop.
• Cursors are used to interact with the data returned by a SQL, thus enable us to perform
various operations on the data.

cursor () method: It is used to create cursor object (file handle). It acts as a file handle or
intermediator

connect () method: Use connect () method to Create the database (or database object) and open
(connect) it, if already existing, then directly open it.

execute () method : We can Execute the SQL commands using execute () method. By using this
method, we can execute the commands like CREATE TABLE, INSERT INTO etc

commit method (): Confirm the execution by using commit method (), specially while deleting or
updating

close() method : Use close() method to close the connection or database

#Program
#To create the table & Insert the values

import sqlite3
conn=sqlite3.connect(‘cricket.db’) #create & open database by name cricket and store in variable conn
cur=conn.cursor() # creating the cursor object : cur
cur.execute (‘CREATE TABLE Player (name TEXT, age INT, runs INT)’) #create table
cur.execute (‘INSERT INTO Player (name, age, runs) VALUES ('Dhoni', 35, 15000)’)
cur.execute (‘INSERT INTO Player (name, age, runs) VALUES ('Rohit', 30, 12000)’)
cur.execute (‘INSERT INTO Player (name, age, runs) VALUES ('Virat', 30, 14000)’)
conn.close() # close the database or connection

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

# To update or modify the values (age of rohit)

import sqlite3
conn=sqlite3.connect(‘cricket.db’) #create & open database by name cricket and store in variable conn
cur=conn.cursor() # creating the cursor object : cur
cur.execute (‘UPDATE Player SET age 32 WHERE ‘name’ = Rohit)
conn.commit() #It is for confirmation
conn.close() # close the database or connection

#To delete the data

import sqlite3
conn=sqlite3.connect(‘cricket.db’) #create & open database by name cricket and store in variable conn
cur=conn.cursor() # creating the cursor object : cur
cur.execute (‘DELETE FROM Player WHERE= ‘Dhoni’)
conn.commit() #It is for confirmation
conn.close() # close the database or connection

# Extracting and displaying the data

import sqlite3
conn=sqlite3.connect(‘cricket.db’) #create & open database by name cricket and store in variable conn
cur=conn.cursor() # creating the cursor object : cur
cur.execute (‘SELECT name, runs, age FROM Player’)
for row in cur
print(row[0])
print(row[1])
print(row[2])
conn.close() # close the database or connection

#To delete the table

import sqlite3
conn=sqlite3.connect(‘cricket.db’) #create & open database by name cricket and store in variable conn
cur=conn.cursor() # creating the cursor object : cur
cur.execute (‘DROP TABLE Player’) # Delete table
conn.commit() #It is for confirmation
conn.close() # close the database or connection

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Write a python code for creating employee database, inserting records and selecting the employees
working in the company June 2020

Solution:
EmpID DeptName GrossSalary
51 Quality Control 30000
52 Quality Control 25000
53 Quality Control 20000
54 Testing 25000
55 Testing 20000

Program:

import sqlite3

conn = sqlite3.connect('EmpDB.db')
cur = conn.cursor()

#To create the table


cur.execute('''
CREATE TABLE Employee (
EmpID INTEGER,
DeptName TEXT,
GrossSalary INTEGER
)
''')

# Inserting data into the table


cur.execute("INSERT INTO Employee (EmpID, DeptName, GrossSalary) VALUES (51, 'Quality Control', 30000)")
cur.execute("INSERT INTO Employee (EmpID, DeptName, GrossSalary) VALUES (52, 'Quality Control', 25000)")
cur.execute("INSERT INTO Employee (EmpID, DeptName, GrossSalary) VALUES (53, 'Quality Control', 20000)")
cur.execute("INSERT INTO Employee (EmpID, DeptName, GrossSalary) VALUES (54, 'Testing', 25000)")
cur.execute("INSERT INTO Employee (EmpID, DeptName, GrossSalary) VALUES (55, 'Testing', 20000)")

#To extract or retrieve the data and print all the data
cur.execute('SELECT * FROM Employee') # *indicates all data
print(cur.fetchall())

# Now only to extract gross salary of quality control


cur.execute("SELECT GrossSalary FROM Employee WHERE DeptName = 'Quality Control'")
print(cur.fetchall())

conn.commit()
conn.close()

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

What is embedded SQL? Explain the importance of SQLite database. Write a Python code to
establish a database connection to ‘EmpDb’ and display the total gross salary paid to the employee
working in the ‘Quality Control’ department.
Assume the employee table has been already created and exit in the ‘EmpDb’. The fields of
Employable table are: (EmpID, DeptName, GrossSalary).

Embedded SQL: Putting SQL queries into high-level languages (python or java) to get meaningful
& efficient output
Importance of SQLite database is embedded (serverless), lightweight and fast database. It is open
source database

EmpID DeptName GrossSalary


51 Quality Control 30000
52 Quality Control 25000
53 Quality Control 20000
54 Testing 25000
55 Testing 20000

# Python code
import sqlite3
conn = sqlite3.connect('EmpDB')
cur = conn.cursor()

#To create the table


cur.execute('CREATE TABLE Employee (EmpID, DeptName, GrossSalary)')

# Inserting data into the table


cur.execute('INSERT INTO Employee (EmpID, DeptName, GrossSalary) VALUES (51, 'Quality Control', 30,000))
cur.execute('INSERT INTO Employee (EmpID, DeptName, GrossSalary) VALUES (52, 'Quality Control', 25,000))
cur.execute('INSERT INTO Employee (EmpID, DeptName, GrossSalary) VALUES (53, 'Quality Control', 20,000))
cur.execute('INSERT INTO Employee (EmpID, DeptName, GrossSalary) VALUES (54, 'Testing', 25000))
cur.execute('INSERT INTO Employee (EmpID, DeptName, GrossSalary) VALUES (55, 'Testing', 20,000))

#To extract or retrieve the data and print all the data
cur.execute('SELECT * FROM Employee') # * indicates extract all the data
print (cur.fetchall())

# Now only to extract gross salary of quality control


cur.execute('SELECT GrossSalary FROM Employee WHERE DeptName = \'Quality Control\'')

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

print (cur.fetchall())

#To calculate total gross salary of employees working in the Quality Control department (using sum command)
cur.execute('SELECT SUM(GrossSalary) FROM Employee WHERE DeptName = \'Quality Control\'')
print ('Total Gross Salary of Employees Working in Quality Control Dept. is', cur.fetchall()[0][0])

Explain with a neat diagram of Service Oriented Architecture (SOA)

Service-Oriented Architecture (SOA): It is a way of building applications using small,


independent services that interact with each other over a network. SOA involves building
programs that use services provided by other programs.

Service: It is a small piece of code that performs a specific task, such as processing payments or
booking flights. Each service has an API that allows it to communicate with other services or
applications.

How SOA Works:


1. A user interacts with a web application (e.g., a travel booking site) to book an air ticket.
2. After that, travel site sends a request to the payment application through an API to process
the payment.
3. Each service performs its task and communicates with other services or external systems as
needed.
4. The services work together to complete the user's request.

Benefits of SOA:
• Flexibility: Services can be easily added, removed, or changed without affecting the
whole application.
• Scalability: Services can handle more requests as needed.
• Reusability: Services can be used by multiple applications.

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Example of SOA:
• A single website allows booking air travel, booking of hotel rooms.
• Payment can be done for air travel or hotel accommodations is also possible.
• These interconnected services can handle air-booking-data, hotel-booking-data, or credit
card transactions etc.

Explain Application Programming Interface (API) & SOA

Application Programming Interface (API)


• Set of rules/ protocols that allows different software applications to communicate with each
other. It defines the methods and data formats that applications can use to request and
exchange information.
• APIs enable data exchange between applications, using protocols like HTTP, XML, and
JSON
• APIs define a set of rules between applications for smooth data transfer.

Key aspects of APIs:


• Program 'X' publishes its APIs (rules) for other applications to access its services.
• Other applications must follow Program 'X's rules to access its services.

Service-Oriented Architecture (SOA) & API


• SOA involves using services provided by other programs to complete the task. Multiple
applications work together, each providing services to others.
• In context of SOA, APIs facilitate/ allows the use of external services, by setting the rues,
and ensure smooth data exchange. It also enhances flexibility and modularity in software
development.
• Each service has an API that allows it to communicate with other services or applications.

Web Services:
• When an application makes its services available over the web using APIs, they are called
web services.

Service-Oriented Architecture (SOA):


It is a way of building applications using small, independent services that interact with each other
over a network. SOA involves building programs that use services provided by other programs.

Service: It is a small piece of code that performs a specific task, such as processing payments or
booking flights. Each service has an API that allows it to communicate with other services or
applications.

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

How SOA Works:


5. A user interacts with a web application (e.g., a travel booking site) to book an air ticket.
6. After that, travel site sends a request to the payment application through an API to process
the payment.
7. Each service performs its task and communicates with other services or external systems as
needed.
8. The services work together to complete the user's request.

Write a note on Google Geocoding web service. Using Python supported libraries. Demonstrate with a
Snippet code.

Google Maps and its Geocoding API are very popular web services provided by Google. These are
used to obtain Geographical information (find locations, get directions etc)
Geocoding is the process of converting human-readable addresses or place names (like "Hubli") into
geographic coordinates (latitude and longitude) that can be used to locate those places on the map.
Working principle:
Users submit a geographic search string (e.g., "Hubli") to the Geocoding API.
Geocoding API processes this string and tries to find a matching location from its database.
If a match is found, Google returns the geographic coordinates (latitude and longitude) of the location.
Also, Google Maps may display the location on the map with nearby landmarks, making it easier for
users to recognize the place.
Google Geocoding web service

import requests

def geocode(address):

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

base_url = "https://maps.googleapis.com/maps/api/geocode/json"
api_parameters = {"address": address, "key": "YOUR_GOOGLE_MAPS_API_KEY"}

response = requests.get(base_url, params=api_parameters)


data = response.json()

# Extracting latitude and longitude from the response


location = data['results'][0]['geometry']['location']
latitude = location['lat']
longitude = location['lng']
return latitude, longitude

# To call the function


address = "M.G. road, Bengaluru"
latitude, longitude = geocode(address)
print(f"The coordinates of '{address}' are: Latitude {latitude}, Longitude
{longitude}")

Create simple spidering program that will go through Twitter accounts and build a database
of them
import tweepy

# 1.Twitter Developer API keys


consumer_key = "YOUR_CONSUMER_KEY"
consumer_secret = "YOUR_CONSUMER_SECRET"
access_token = "YOUR_ACCESS_TOKEN"
access_token_secret = "YOUR_ACCESS_TOKEN_SECRET"

# Authenticate with Twitter API


auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

# Create API object


api = tweepy.API(auth, wait_on_rate_limit=True)

def fetch_user_details(username):
user = api.get_user(screen_name=username)
return user._json

# Call the function


username = "twitter"
user_details = fetch_user_details(username)
print("User Details:")
for key, value in user_details.items():
print(f"{key}: {value}")

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

1. Import Required Libraries:


2. Twitter Developer API Keys and Access Tokens:
These keys and tokens are required for authentication to access the Twitter API.
3. Authenticate with Twitter API:
These keys and tokens are required for authentication to access the Twitter API.
4. Create API Object:
We create an OAuthHandler object, by passing the consumer key and secret. Then, we set the access
tokens to access the Twitter account.
5. Define the Function to Fetch User Details:
We define a function fetch_user_details(username) that takes a username as input. The function uses
the api.get_user() method to get the details of the Twitter user with the given username.
6. Call the Function:
We call the fetch_user_details() function, passing the Twitter username as "twitter". The function
will retrieve the user details
7. Print the User Details:

SQL Keys

Name of table: Player Name of table: Team


PlayerID Name Age Runs TeamID TeamName PlayerID
1 Dhoni 39 10500 1 Team India 1
2 Kohli 32 12000 2 Team India 2
3 Rohit 34 9000 3 Team India 3
CREATE TABLE Player ( CREATE TABLE Team (
PlayerID INT PRIMARY KEY, #Primary Key TeamID INT PRIMARY KEY, #Primary Key
Name VARCHAR(100), TeamName VARCHAR(100),
Age INT, PlayerID INT, #Foreign Key
Runs INT FOREIGN KEY (PlayerID) REFERENCES Player(PlayerID)
); );

Explanation of Keys:
1. Primary Key: PlayerID
• A primary key is a unique identifier for each record (field) or cell in the table. No two
records(fields) can have the same value in the primary key column, and it cannot be NULL.
• In the Player table, PlayerID is the primary key because each player has a unique PlayerID.

2. Foreign Key: PlayerID


o A foreign key is a column or a set of columns in one table that refers or points to the primary key
in another table. It is used to establish a relationship between the two tables.
o For Team table, PlayerID is a foreign key that refers to the PlayerID column in the Player
table. This creates a link between the Team and Player tables.

3. Candidate Key:
• A column or a set of columns that can uniquely identify any record (cell) in the table. A table can
have multiple candidate keys, but one of them is selected as the primary key.

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

• Example: In the Player table, both PlayerID and Name could potentially serve as candidate
keys, but PlayerID is chosen as the primary key.

4. Composite Key:
• A composite key is a primary key that consists of two or more columns. It is used when a single
column is not sufficient to uniquely identify a record.
• Example: In the table, Team; if it has additional column MatchID to represent different matches,
then a combination of PlayerID and MatchID can be used as a composite key, which can uniquely
identify each player's performance in each match.

Summary:
• The primary key ensures that each record is unique within the table.
• The foreign key establishes a relationship between tables.
• Candidate keys are potential primary keys.
• A composite key is a combination of multiple columns that uniquely identify a record.

JOIN
• JOIN in SQL is used to combine rows from two or more tables based on a related column.
• It allows us to retrieve data from multiple tables as if the data were stored in a single table.

Key Concepts:
1. Tables with Related Data:
• To perform a JOIN, the tables must have at least one common column. This column is
usually a primary key in one table and a foreign key in the other.

2. Types of JOINs:

o INNER JOIN:
o LEFT JOIN (or LEFT OUTER JOIN):
o RIGHT JOIN (or RIGHT OUTER JOIN):
o FULL JOIN (or FULL OUTER JOIN):

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

Example:

Name of table: Player Name of table: Team


PlayerID Name Age Runs TeamID TeamName PlayerID
1 Dhoni 39 10500 1 Team India 1
2 Kohli 32 12000 2 Team India 2
3 Rohit 34 9000 3 Team India 3
4 Team Australia NULL

1. INNER JOIN:
It is used to combine the rows of 2 or multiple tables
Retrieves only the records (rows) that have matching values in both tables based on the
column specified in the SQL query.

SELECT Player.Name, Team.TeamName


FROM Player
INNER JOIN Team ON Player.PlayerID = Team.PlayerID;

Explanation:
• SELECT Player.Name, Team.TeamName: Specifies the columns to retrieve: player names
and team names.
• FROM Player: Indicates primary table is Player table.
• INNER JOIN Team: Combines the Player table with the Team table.
• ON Player.PlayerID = Team.PlayerID: Specifies the condition for the join. It means that
only records where Player.PlayerID matches Team.PlayerID will be included.

Result :

Name TeamName
Dhoni Team India
Kohli Team India
Rohit Team India

2. LEFT JOIN:
• Retrieves all records from the left table.
• Includes matched records from the right table.
• If no match is found, NULL values are shown for columns from the right table.

SELECT Player.Name, Team.TeamName


FROM Player

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

LEFT JOIN Team ON Player.PlayerID = Team.PlayerID;

Explanation:
• SELECT Player.Name, Team.TeamName: Specifies the columns to retrieve: player
names and team names.
• FROM Player: Indicates left table is Player
• LEFT JOIN Team: Joins the Player table (left table) with the Team table (right table).
• ON Player.PlayerID = Team.PlayerID: Specifies the condition for the join. It means
that only records where Player.PlayerID matches Team.PlayerID will be included.

Name TeamName
Dhoni Team India
Kohli Team India
Rohit Team India

3. RIGHT JOIN:
• Retrieves all records from the right table.
• Includes matched records from the left table.
• If no match is found, NULL values are shown for columns from the left table.

Query:
SELECT Player.Name, Team.TeamName
FROM Player
RIGHT JOIN Team ON Player.PlayerID = Team.PlayerID;

Explanation:
• SELECT Player.Name, Team.TeamName: Specifies the columns to retrieve: player
names and team names.
• FROM Player: Indicates left table is Player
• RIGHT JOIN Team: Joins the Player table (left table) with the Team table (right table). In
this case, Team is the right table
• ON Player.PlayerID = Team.PlayerID: Specifies the condition for the join. It
means that only records where Player.PlayerID matches Team.PlayerID will be
included

• Result:
Name TeamName
Dhoni Team India
Kohli Team India
Rohit Team India
NULL Team Australia

4. FULL JOIN:
• Retrieves all records when there is a match in either the left or right table.
• If no match is found, NULL values are shown for columns from the table without match

Query:
SELECT Player.Name, Team.TeamName

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING (QUESTION & ANSWERS)

FROM Player
FULL JOIN Team ON Player.PlayerID = Team.PlayerID;

Explanation:
• SELECT Player.Name, Team.TeamName: Specifies the columns to retrieve: player
names and team names.
• FROM Player: Indicates left table is Player
• FULL JOIN Team: Combines the Player table (left table) with the Team table (right
table). This join includes all records from both tables.
• ON Player.PlayerID = Team.PlayerID: Specifies the condition for the join. It
means that only records where Player.PlayerID matches Team.PlayerID will be
included
Result:

Name TeamName
Dhoni Team India
Kohli Team India
Rohit Team India
NULL Team Australia
Summary:
• INNER JOIN: Shows only matching records from both tables.
• LEFT JOIN: Shows all records from the left table, with matching records from the right table (or
NULL if no match).
• RIGHT JOIN: Shows all records from the right table, with matching records from the left table (or
NULL if no match).
• FULL JOIN: Shows all records from both tables, with NULLs where there is no match.

Prof. Sujay Gejji ECE, SGBIT, Belagavi

You might also like