0% found this document useful (0 votes)
6 views66 pages

DE With Python

Data engineering is the practice of designing, building, and maintaining the systems and infrastructure such as databases and data pipeline that enable organizations to collect, store, and analyze data at scale.
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)
6 views66 pages

DE With Python

Data engineering is the practice of designing, building, and maintaining the systems and infrastructure such as databases and data pipeline that enable organizations to collect, store, and analyze data at scale.
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

DE with Python MCA I Year II Semester

INDEX
[Link]. Name of the program [Link].
1. Installing Anaconda Python Distribution

2. Basic Python Programs


a) Running Instructions In Interactive Interpreter And A Python Script.
b) Write A Program To Purposefully Raise Indentation Error And
Correct It.
c) Write A Program To Compute Distance Between Two Points Taking
Input FromThe User.
d) Write A Program [Link] That Takes 2 Numbers As Command Line
ArgumentsAnd Prints The Sum.
e) Program To Display Personal Information
f) Write A Program For Checking Whether The Given Number Is
Even Number Or Not.

3 Conditional Statements
A) Write A Program To Find The Largest Of Three Integers Using
If-Else
B) Write A Python Program To Receive A Series Of Positive Numbers
And DisplayThe Numbers In Order And Display Their Sum

4 Control Structures
A) Write A Python Program To Print The Elements Of The List Using For
Loop.
B) Write A Python Program To Find Sum Of Numbers Entered By User.

5 Functions & Recursion


A) Write A Python Program To Add Two Numbers Using Functions
B) Write Recursive Functions To Find The Gcd Of Two Integers.
C) Write Recursive Functions To Find The Factorial Of The Given
Number
D) Program To Generate Fibonacci Series Using Recursion
E) Write A Python Program To Generate Prime Numbers From 2 To N.
6 Exception Handling
A) Write A Python Program To Demonstrate Exception Handling
B) Write A Python Program To Raise An Exception

7 Object Oriented Programming


a) Program To Implement Inheritance.

b) Program To Implement Polymorphism

8 Binary Files

1
DE with Python MCA I Year II Semester

A) Program To Create A Binary File And Write The Data Into It


Using Pickle Module
B) Program To Read Data From The Binary File

B) Program To Create A Text File, Write Into The Text File,


Append The Data To The TextFile And Read From Text
File.
D) CSV Files
E) Program To Open/Create A New Html File In The Write Mode
F) Parsing Json – Converting From Json To Python
G) Python Program To Convert Python To Json
H) Writing To A Json File
9 A) Create An Array Of 10 Zeros
B) Reate An Array Of Even Integers Up To 50
C) Create A 3x3 Matrix
D) Generate An Array Of 25 Random Numbers Sampled
From A Standard NormalDistribution.
E) Create An Array Of 20 Linearly Spaced Points Between 0 And 1
F) Demonstrate Slicing An Indexing OperationsSource Code
G) Get The Sum Of All Columns In Matrix

10 Program For Searching, Splitting, And Replacing Strings Based On


Pattern Matching Using Regular Expressions

11 Web Scraping
Web Scraping Using Regular Expressions
12 Data Wrangling Using Pandas

13 Using SQL CRU(Create,Read,Update And Delete) Operations

14 Data Visualizations
A. Scatter Plot
B. Line Chart
C. Bar Chart
D. Histogram

15 A) Implement Linear Regression In Python.


B) Implement K-MEANS Clustering Algorithm

1.a. INSTALLING ANACONDA PYTHON DISTRIBUTION


2
DE with Python MCA I Year II Semester

Anaconda is a free and open source distribution of the Python


programming language for data science and machine learning related
applications such as large scale data processing, predictive analytics, and
scientific computing, that aims to simplify package management and
deployment. Package versions are managed by the package management
system conda, which makes it quite simple to install, run, and update
complex data science and machine learning software libraries like
Scikitlearn, PyTorch, TensorFlow, and SciPy. Anaconda Distribution is
used by over 6 million users, and it includes more than 250 popular data
science packages suitable for Windows, Linux, and MacOS.

The steps described here work on the Windows 10 OS.

Step 1: Go to the link ttps://[Link]/downloads. You have


the option to download the 32bit or the 64bit version of either Python 2.7
or Python 3.6 supported Anaconda distribution.
Download the version which is compatible to your system.
Step 2: Click on the executable file of Anaconda Python distribution which
you have downloaded and the setup screen will start loading. Step 3: You
will get a welcome screen as shown in FIGURE 1.5. Click
on Next button.

FIGURE 1.5
Welcome screen of Anaconda installation.

3
DE with Python MCA I Year II Semester

Step 4: You will get a License Agreement Screen, read the licensing terms
and click on I Agree button.
Step 5: Assuming that you are the only user on your system, select
Just Me radio button (FIGURE 1.6). Click on Next button.

FIGURE 1.6
Selection of installation type.

Step 6: You need to choose a location to install Anaconda Python


distribution. The default installation will be under Users folder. Change
the destination folder to C:\Anaconda3 to install Anaconda and click on
Next button (FIGURE 1.7).

FIGURE 1.7
Choosing the destination folder.

4
DE with Python MCA I Year II Semester

Step 7: In the Advanced Installation Options screen, select all the check
boxes. Ignore the warnings and click on Install button (FIGURE 1.8).

FIGURE 1.8
Selecting Advanced Installation Options.
Step 8: This starts the installation of Anaconda Python Distribution and
once the installation is complete, click on Next button.
Step 9: Finish the setup by clicking on Finish button.
Step 10: To check whether the installation is working properly or not, go
to the com mand prompt and type python. You should see a series of
lines and a prompt as shown in FIGURE 1.9. This is Python Interactive
mode. Here, the three greaterthan signs “>>>” is the primary prompt of
the interactive mode.

FIGURE 1.9
Python interactive shell.

5
DE with Python MCA I Year II Semester

1.b. INSTALLING PYCHARM IDE TO SET UP A


PYTHON DEVELOPMENT
ENVIRONMENT

PyCharm is an Integrated Development Environment (IDE) used for


Python programming language. It is developed by the Czech company
JetBrains. It provides code analysis, a graphical debugger, an integrated
unit tester, integration with version control systems and supports web
development with Django web framework. PyCharm is designed by
programmers, for programmers, to provide all the tools you need for
productive Python development.

The steps described here works on the Windows 10 OS.


Step1:Go to the link [Link]
You have the option to download either the PyCharm Professional
Edition or the PyCharm Community Edition
Step 2: Click on the executable file which you have downloaded. You
will be presented with the PyCharm Community Edition Setup screen.
Click on Next button.
Step 3: Now you will be presented with Choose Install Location screen.
Go with the default destination folder to install PyCharm Community
Edition. Click on Next button.
Step 4: Select all the check boxes in the Installation Options screen
except for the 32bit launcher check box (and click on Next (FIGURE
1.10).

FIGURE 1.10
Installation options for PyCharm.

6
DE with Python MCA I Year II Semester

Step 5: Go with the default Start Menu Folder as shown on the screen
and click on Install button. It will take some time for the installation to
finish. Once the installation is done click on the Finish button.
Step 6: You will be asked whether you want to import previous
PyCharm settings. Since we are starting on a clean slate, let’s select the
second radio button as shown in FIGURE 1.11 and click on OK button.

FIGURE 1.11
Importing PyCharm settings.

Step 7: You will be prompted with a Windows Security Alert. Click on


Allow Access Button. Next screen will be Python community edition
initial configuration. Let the default settings remain as it is and click on
OK button. After you have completed initial PyCharm configuration,
click on Skip Remaining and Set Defaults button.

FIGURE 1.12
Customization of PyCharm.

7
DE with Python MCA I Year II Semester

Step 8: In the next screen, click on Configure pull down list and select Settings option as
shown in FIGURE 1.13.

FIGURE 1.13
Welcome screen for PyCharm.

Step 9: In the Default Settings screen, on the left pane, click on Project Interpreter as
shown in FIGURE 1.14.

FIGURE 1.14
Default settings of PyCharm.
On the right pane, in the Project Interpreter option, click on the button having toothed
wheel icon and select Add. In the Add Python Interpreter screen, on the left pane, click
on System Interpreter and select the Python interpreter path from the Interpreter pull
down list as shown in FIGURE 1.15. Click on OK button.

14
DE with Python MCA I Year II Semester

FIGURE 1.15
Adding Python Interpreter.

Step 10: It will take some time to list all the packages. Once done click on OK button.
Step 11: You will be again presented with the Welcome screen as shown in FIGURE
1.13. Now, to work with PyCharm IDE, click on Create New Project option. In the next section,
steps to create and execute Python program are discussed in detail.

15
DE with Python MCA I Year II Semester

1.c. INTRODUCTION TO PYTHON PROGRAMMING

Python is a general-purpose interpreted, interactive, object-oriented, and highlevel


programming language. It was created by Guido van Rossum during 1985- 1990. Python got
its name from “Monty Python’s flying circus”. Python was released in the year 2000.

Python Features:

Easy-to-learn: Python is clearly defined and easily readable. The structure of the program is
very simple. It uses few keywords.

Easy-to-maintain: Python's source code is fairly easy-to-maintain.

Portable: Python can run on a wide variety of hardware platforms and has the same interface
on all platforms.

Interpreted: Python is processed at runtime by the interpreter. So, there is no need to compile
a program before executing it. You can simply run the program.

Extensible: Programmers can embed python within their C,C++,Java script ,ActiveX, etc.

Free and Open Source: Anyone can freely distribute it, read the source code, and edit it.

High Level Language: When writing programs, programmers concentrate on solutions of the
current problem, no need to worry about the low level details.

Scalable: Python provides a better structure and support for large programs than shell scripting.

Applications:
• Bit Torrent file sharing
• Google search engine
• Youtube
• Intel, Cisco, HP, IBM
• i-Robot
• NASA
• Facebook
• Dropbox

MODES OF PYTHON INTERPRETER

Python Interpreter is a program that reads and executes Python code. It uses 2 modes of Execution.
1. Interactive mode
2. Script mode

16
DE with Python MCA I Year II Semester

1. Interactive mode: Interactive Mode, as the name suggests, allows us to interact with OS.
When we type Python statement, interpreter displays the result immediately. In interactive
mode, you type Python programs and the interpreter displays the result:
>>> 1 + 1
2
>>>
We cannot save the statements and have to retype all the statements once again to re- run
them.

The chevron, >>>, is the prompt the interpreter uses to indicate that it is ready for you to
enter code.

2. Script mode: In script mode, we type python program in a file and then use interpreter
to execute the content of the file. Scripts can be saved to disk for future use.

Python scripts have the extension .py, meaning that the filename ends with .py. Save the code
with [Link] and run the interpreter in script mode to execute the script.

Indentation:
Indentation refers to the spaces at the beginning of a code line.
Python uses indentation to indicate a block of code

Input and Output functions in Python:


The input() function allows user input.

The print() function prints the specified message to the screen, or other standard output device.
DATA TYPES IN PYTHON

17
DE With Python MCA I Year II Semester

Python has the following data types built-in by default, in these categories

Text Type: str

Numeric Types: int, float, complex

Sequence Types: list, tuple, range

Mapping Type: dict

Set Types: set, frozenset

Boolean Type: bool

Binary Types: bytes, bytearray, memoryview

18
DE With Python MCA I Year II Semester

2 Basic Python Programs


2.a) Running instructions in Interactive interpreter and a Python script.

Interactive Mode:

Source Code:

#to demonstrate data type


a=5
b=1.25
c='Hello'

print(type(a)) print(type(b))
print(type(c))))

Output:

19
DE With Python MCA I Year II Semester

2.b. Write a program to purposefully raise Indentation Error and correct it.

Source Code with indentation error:

# Program to show indentation error


print("Starting Loop")
for x in [1,2,3,4,5]:
if(x%2==0):
x+=3
print (x)
print("One execution of the loop")
print("End of the Loop")

Source Code after correction:

# Program to show indentation error


print("Starting Loop")
for x in [1,2,3,4,5]:
if(x%2==0):
x+=3
print(x)
print("One execution of the loop") print("End
of the Loop")

Output after correction:

20
DE With Python MCA I Year II Semester

2. c. Write a program to compute distance between two points taking input from the
user.

Source Code:
import math

a=input("Enter the first coordinate - ")


x=[Link](",")

print(x)

b=input("Enter the second coordinate - ")


y=[Link](",")

print(y)

d=[Link](((int(x[0])-int(y[0]))**2)+((int(x[1])-int(y[1]))**2))

print("The distance between two points",d)

Output:

21
DE With Python MCA I Year II Semester

2.d. Write a program [Link] that takes 2 numbers as command line arguments and
prints the sum.

Source Code:

#Program to accept two numbers as command line arguments and prints the sum.

num1=eval(input("Enter the first number -"))


num2=eval(input("Enter the second number -"))

sum=num1+num2

print("Sum of two numbers is",sum)

Output:

22
DE With Python MCA I Year II Semester

2.e. Program to display personal information

Source Code:

name=input("Enter your name")


Adr=input("Enter the address")
mno=input("Enter the mobile number")
cname=input("Enter the college name")
sub=input("Enter the course subjects")

print("Name: {}\nAddress: {}\nMobile_Number: {}\nCollege_Name: {}


\nSubjects: {}".format(name,Adr,mno,cname,sub))

Output:

23
DE With Python MCA I Year II Semester

2.f. Write a program for checking whether the given number is even number or not.

Source Code:

# to check whether the given number is even or odd.

a=eval(input("Enter the number - "))

if(a%2==0):
print("Given number is even")
else:
print("Given number is odd")

Output:

24
DE With Python MCA I Year II Semester

3 CONDITIONAL STATEMENTS

[Link] a program to find the largest of three integers using if-else

Source Code:
# Python program to find the largest number among the three input numbers

num1 = eval(input("Enter the first number: ")) num2 =


eval(input("Enter the second number: ")) num3 =
eval(input("Enter the third number: "))

# uncomment following lines to take three numbers from user #num1


= float(input("Enter first number: "))
#num2 = float(input("Enter second number: "))
#num3 = float(input("Enter third number: "))

if (num1 >= num2) and (num1 >= num3): largest


= num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3

print("The largest number is", largest)

Output:

25
DE With Python MCA I Year II Semester

3.b. Write a Python program to receive a series of positive numbers and display the
numbers in order and display their sum.
Source Code:

import random

num1=[Link](100,500)
num2=[Link](100,500)
sum=num1+num2
print("First random number is ", num1)
print("Second random number is ", num2)

res=eval(input("Add the random numbers and type the sum: ")) if


res==sum:
print("Congratulations......Answer is correct ....... ")
else:
print("You have entered wrong answer..Correct answer is ",sum)

Output:

26
DE With Python MCA I Year II Semester

4 CONTROL STRUCTURES

4.a. Write a python program to print the elements of the list using for loop.

Source Code:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)

27
DE With Python MCA I Year II Semester

4.b. Write a python program to find sum of numbers entered by user.

Source Code:
# program to calculate the sum of numbers until the user enters zero total = 0

number = eval(input('Enter a number: '))

# add numbers until number is zero


while number != 0:
total += number # total = total + number

# take integer input again


number = int(input('Enter a number: '))

print('total =', total)

Output:

28
DE With Python MCA I Year II Semester

5 FUNCTIONS & RECURSION

5.a Write a Python Program to add two numbers using functions

Source Code:

# function that adds two numbers


def add_numbers(num1, num2):
sum = num1 + num2
return sum

# calling function with two values result


= add_numbers(5, 4)

print('Sum: ', result)

29
DE With Python MCA I Year II Semester

5.b Write recursive functions to find the GCD of two integers.

Source Code:
#Recursive function to find the GCD of two numbers
def gcd(a, b):
if a == b:
return a elif
a < b:
return gcd(b, a)
else:
return gcd(b, a - b)

a =eval(input("Enter the first Value: "))


b =eval(input("Enter the second Value: "))

print("GCD of the numbers is :",gcd(a, b))

30
DE With Python MCA I Year II Semester

5.c. Write recursive functions to find the factorial of the given number.

Source code:

# Python program to find factorial of given number def


factorial(n):

# single line to find factorial


return 1 if (n==1 or n==0) else n * factorial(n - 1)

# Driver Code
num = eval(input("Enter the number to find the factorial: "))
print("Factorial of",num,"is",factorial(num))

Output:

31
DE With Python MCA I Year II Semester

5.d. Program to generate Fibonacci series using recursion.


def fibonacci(n):
if(n <= 1):
return n
else:
return(fibonacci(n-1) + fibonacci(n-2)) n =
int(input("Enter number of terms:"))
print("Fibonacci sequence:")
for i in range(n):
print(fibonacci(i))

Output:

32
DE With Python MCA I Year II Semester

5. e. Write a Python program to generate prime numbers from 2 to n.

Source Code:

# Program to generate prime numbers using recursion def


CheckPrime(i,num):
if num==i:
return 0
else:
if(num%i==0):
return 1 else:
return CheckPrime(i+1,num)
n=int(input("Enter your Number:"))
print("Prime Number Between 2 to n are: ") for
i in range(2,n+1):
if(CheckPrime(2,i)==0):
print(i,end=" ")

Output:

33
DE With Python MCA I Year II Semester

6 EXCEPTION HANDLING

6.a. Write a Python program to demonstrate Exception Handling #


Python program to demonstrate finally

# No exception Exception raised in try block


try:
k = 5//0 # raises divide by zero exception.
print(k)

# handles zerodivision exception


except ZeroDivisionError:
print("Can't divide by zero")

finally:
# this block is always executed
# regardless of exception generation.
print('This is always executed')

Output:

34
DE With Python MCA I Year II Semester

6.b. Write a Python program to raise an exception

# Program to depict Raising Exception

try:
raise NameError("Hi there") # Raise Error
except NameError:
print ("An exception")
raise # To determine whether the exception was raised or not

35
DE With Python MCA I Year II Semester

7 OBJECT ORIENTED PROGRAMMING

7.a. Program to implement inheritance.

Source Code:

# A Python program to demonstrate inheritance class


Person(object):
# Constructor
def init (self, name):
[Link] = name #
To get name
def getName(self):
return [Link]
# To check if this person is an employee
def isEmployee(self):
return False
# Inherited or Subclass (Note Person in bracket)
class Employee(Person):
# Here we return true
def isEmployee(self):
return True
# Driver code
emp = Person("Shreyansh") # An Object of Person
print([Link](), [Link]())
emp = Employee("Shrinidhi") # An Object of Employee
print([Link](), [Link]())
Output:

36
DE With Python MCA I Year II Semester

7.b. Program to implement Polymorphism.

Source Code:

#Polymorphism
class India():
def capital(self):
print("New Delhi is the capital of India.")
def language(self):
print("Hindi is the most widely spoken language of India.") def
type(self):
print("India is a developing country.")
class USA():
def capital(self):
print("Washington, D.C. is the capital of USA.") def
language(self):
print("English is the primary language of USA.") def
type(self):
print("USA is a developed country.")
def func(obj):
[Link]()
[Link]()
[Link]()
obj_ind = India()
obj_usa = USA()
func(obj_ind)
func(obj_usa)

Output:

37
DE With Python MCA I Year II Semester

8.a. BINARY FILES

Program to create a binary file and write the data into it using Pickle Module Source

Code:

import pickle
s={ } #declare empty dictionary
file2 = open("E:/[Link]" , "wb") #open file
R = int (input ('Enter roll number=' ) )
N = input ('Enter name =' )
M = float(input ('Enter marks =' )) #
add read data into dictionary
s['Rollno'] = R
s['Name'] = N
s['Marks'] = M
# now write into the file
[Link] (s ,file2)
[Link]( )

Output:

38
DE With Python MCA I Year II Semester

Program to read data from the binary file.

import pickle
Stu = {} #Empty dictionary object to stored records read from [Link] Studentfile =
open('E:/[Link]', 'rb') #open binary file in read mode
try:
while True : # It will become False upon EOF
Stu = [Link](Studentfile) #Read record in Emp dictionary from file handle Empfile
print (Stu) # print the record
except EOFError :
[Link]()
Output:

39
DE With Python MCA I Year II Semester

8.b. WORKING WITH TEXT FILE

Program to create a text file, write into the text file, append the data to the text file and
read from text file.

#to create a text file

f=open("F:/Saritha/Python Lab Manual Programs/[Link]","w")

[Link]("Welcome to Python class")

[Link]()

#to append data to a text file

f=open("F:/Saritha/Python Lab Manual Programs/[Link]","a")

[Link]("\nToday is our first Python class")

[Link]()

#to read from a text file

fread=open("F:/Saritha/Python Lab Manual Programs/[Link]","r")

data=[Link]()

print(data) [Link]()

40
DE With Python MCA I Year II Semester

Output:

41
DE With Python MCA I Year II Semester

[Link] FILES

#writing into a csv file

import csv

with open("C:/Users/APPY/Desktop/Python prgs/DA python prgs/CSV/[Link]","w",newline="")


as csvfile:

headernames=["id","name"]

writer=[Link](csvfile,headernames) [Link]()

[Link]({"id":"101","name":"Avinash"})

42
DE With Python MCA I Year II Semester

# reading from a csv file

a=open("C:/Users/APPY/Desktop/Python prgs/DA python prgs/CSV/[Link]") print(a)

b=[Link](a)

for x in b:
print(x)

43
DE With Python MCA I Year II Semester

8.d .HTML Files Program to open/create a new html file in

the write mode

import codecs
f = open('F:\NewFolder\Python Programs\[Link]', 'w') html_template =
"""<html>
<head>
<title>Title</title>
</head>
<body>
<h2>Welcome To Python Class</h2>
<p>Default code has been loaded into the Editor.</p>
</body>
</html>
"""
# writing the code into the file
[Link](html_template) [Link]()
# viewing html files
file = [Link]("F:\ NewFolder \Python Programs\[Link]", 'r', "utf-8") # using .read
method to view the html code from our object
print([Link]())

Output:

44
DE With Python MCA I Year II Semester

Output: Reading from the HTML file:

45
DE With Python MCA I Year II Semester

8.e. JSON FILES Parsing JSON – Converting from

JSON to Python

import json

# JSON string

employee ='{"name": "Nitin", "department":"Finance",\

"company":"WIPRO"}'

# Convert string to Python dict employee_dict

= [Link](employee) print("Data after

conversion") print(employee_dict)

print(employee_dict['department'])

print("\nType of data")

print(type(employee_dict))

Output:

46
DE With Python MCA I Year II Semester

Python program to convert Python to JSON

import json

# Data to be written

dictionary = {

"name": "Sunil",

"department": "HR",

"Company": 'Infosys'

# Serializing json

json_object = [Link](dictionary)

print(json_object)

Output:

47
DE With Python MCA I Year II Semester

Writing to a JSON file

import json

# Data to be written

dictionary ={

"name" : "Nisha", "rollno"

: 420,

"cgpa" : 10.10,

"phonenumber" : "1234567890"

with open("F:\Saritha\Python Lab Manual Programs\[Link]", "w") as outfile:

[Link](dictionary, outfile)

Output:

48
DE With Python MCA I Year II Semester

9 Demonstration of Data Analysis using NUMPY -Numerical Python

9.a. Create an array of 10 zeros:

9.b Create an array of even integers up to 50

[Link] a 3x3 matrix

49
DE With Python MCA I Year II Semester

9.d. Generate an array of 25 random numbers sampled from a standard normal


distribution.

9.e. Create an array of 20 linearly spaced points between 0 and 1

50
DE With Python MCA I Year II Semester

9.f. Demonstrate slicing and indexing operations

Source Code:

# Python program for basic slicing.


import numpy as np
# Arrange elements from 0 to 19 a
= [Link](20)
print("\n Array is:\n ",a) #
a[start:stop:step]
print("\n a[-8:17:1] = ",a[-8:17:1])
# The : operator means all elements till the end.
print("\n a[10:] = ",a[10:])

#indexing using basic slicing with ellipsis


# A 3 dimensional array.
b = [Link]([[[1, 2, 3],[4, 5, 6]],
[[7, 8, 9],[10, 11, 12]]])
print("\nThree dimensional Array is :\n",b)
print("\nIndexing using slicing of the above array:")
print(b[...,1]) #Equivalent to b[: ,: ,1 ]

Output:

51
DE With Python MCA I Year II Semester

9.g. Get the sum of all columns in matrix

52
DE With Python MCA I Year II Semester

10 REGULAR EXPRESSIONS

Program for searching, splitting, and replacing strings based on pattern matching using
regular expressions

Source Code:

import re
txt = "The rain in Spain"
print("Given Data...",txt)
#Find all lower case characters alphabetically between "a" and "m": x =
[Link]("[a-m]", txt)
print(x)
y=[Link]("[abc]",txt) if
y:
print("Match found")
else:
print("Match not found")

#Split the string at every white-space character: x =


[Link]("i", txt,2)
print(x)

#Replace all white-space characters with the digit "9": x =


[Link]("\s", "9", txt,2)
print(x)

Output:

53
DE With Python MCA I Year II Semester

11 WEB SCRAPING

Web Scraping using Regular Expressions:

Source Code:

import [Link] import

re

url = "[Link] response =

[Link](url)

html = [Link]()

htmlStr = [Link]()

pdata = [Link]("\(\d{3}\) \d{3}-\d{4}", htmlStr) for

item in pdata:

print(item)

Output:

54
DE With Python MCA I Year II Semester

12 DATA WRANGLING USING PANDAS

# Import pandas package

import pandas as pd

#1)Data exploration in Python #

Assign data

data = {'Name': ['Jai', 'Princi', 'Gaurav',

'Anuj', 'Ravi', 'Natasha', 'Riya'],

'Age': [17, 17, 18, 17, 18, 17, 17],

'Gender': ['M', 'F', 'M', 'M', 'M', 'F', 'F'],

'Marks': [90, 76, 'NaN', 74, 65, 'NaN', 71]}

# Convert into DataFrame

df = [Link](data)

# Display data

print(df)

#2)Dealing with missing values in Python #

Compute average

c = avg = 0

for ele in df['Marks']:

if str(ele).isnumeric():

c += 1

avg += ele

avg /= c

55
DE With Python MCA I Year II Semester

# Replace missing values

df = [Link](to_replace="NaN",value=avg)

# Display data

print("\nDataframe after replacing missing values")

print(df)

#3)Data Replacing in Data Wrangling #

Categorize gender

df['Gender'] = df['Gender'].map({'M': 0,'F': 1, }).astype(float)

# Display data

print("\nDataframe after replacing values")

print(df)

#4)Filtering data in Data Wrangling #

Filter top scoring students

df = df[df['Marks'] >= 75].copy()

# Remove age column from filtered DataFrame

[Link]('Age', axis=1, inplace=True)

# Display data

print("Dataframe after deleting age column")

print(df)

56
DE With Python MCA I Year II Semester

#5)Data Wrangling Using Merge Operation #

Import module

import pandas as pd

# Creating Dataframe

details = [Link]({

'ID': [101, 102, 103, 104, 105,

106, 107, 108, 109, 110],

'NAME': ['Jagroop', 'Praveen', 'Harjot',

'Pooja', 'Rahul', 'Nikita',

'Saurabh', 'Ayush', 'Dolly', "Mohit"],

'BRANCH': ['CSE', 'CSE', 'CSE', 'CSE', 'CSE',

'CSE', 'CSE', 'CSE', 'CSE', 'CSE']})

# Creating Dataframe

fees_status = [Link](

{'ID': [101, 102, 103, 104, 105,

106, 107, 108, 109, 110],

'PENDING': ['5000', '250', 'NIL',

'9000', '15000', 'NIL',

'4500', '1800', '250', 'NIL']})

# Merging Dataframe

print("\nDataframe after merging:")

print([Link](details, fees_status, on='ID'))

57
DE With Python MCA I Year II Semester

#Grouping

# Import module

import pandas as pd

# Creating Data

car_selling_data = {'Brand': ['Maruti', 'Maruti', 'Maruti',

'Maruti', 'Hyundai', 'Hyundai', 'Toyota',

'Mahindra', 'Mahindra', 'Ford', 'Toyota',

'Ford'],

'Year': [2010, 2011, 2009, 2013,

2010, 2011, 2011, 2010,

2013, 2010, 2010, 2011],

'Sold': [6, 7, 9, 8, 3, 5,

2, 8, 7, 2, 4, 2]}

# Creating Dataframe for Provided Data df

= [Link](car_selling_data)

# Group the data when year = 2010

print("\nGroup Dataframe") grouped

= [Link]('Year')

print(grouped.get_group(2010))

Output:

58
DE With Python MCA I Year II Semester

59
DE With Python MCA I Year II Semester

60
DE With Python MCA I Year II Semester

13 Using SQL CRUD(Create,Read,Update and Delete) operations Source

Code:

import [Link]

#Creating a connection

mydb = [Link](

host="[Link]",

user="root",

password="appy1723",

database="dataanalytics"

print(mydb)

#Creating a table

mycursor = [Link]()

[Link]("CREATE TABLE customers (name VARCHAR(255), address


VARCHAR(255))")

#to insert records

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"

val = [

('Peter', 'Lowstreet 4'),

('Amy', 'Apple st 652'),

('Hannah', 'Mountain 21'),

('Michael', 'Valley 345'),

('Sandy', 'Ocean blvd 2'),

('Betty', 'Green Grass 1'),

('Richard', 'Sky st 331'),

('Susan', 'One way 98'),

61
DE With Python MCA I Year II Semester

('Vicky', 'Yellow Garden 2'),

('Ben', 'Park Lane 38'),

('William', 'Central st 954'),

('Chuck', 'Main Road 989'),

('Viola', 'Sideway 1633')

[Link](sql, val)

[Link]()

print([Link], "record inserted.")

#To read the data

[Link]("SELECT * FROM customers")

myresult = [Link]()

for x in myresult:

print(x)

#to delete a record

sql = "DELETE FROM customers WHERE address = 'Mountain 21'" [Link](sql)

[Link]()

print([Link], "record(s) deleted")

#Update a record

sql = "UPDATE customers SET address = 'Canyon 123' WHERE address = 'Valley 345'"

[Link](sql)

[Link]()

print([Link], "record(s) Updated")

62
DE With Python MCA I Year II Semester

Output:

63
DE With Python MCA I Year II Semester

14 DATA VISUALIZATIONS

SCATTER PLOT

#Scatter Plot

import pandas as pd

import [Link] as plt

# reading the database

data = pd.read_csv("F:/NewFolder/Python Programs/[Link]")

# Scatter plot with day against tip

[Link](data['day'], data['tip'], c=data['size'],


s=data['total_bill']) #
Adding Title to the Plot [Link]("Scatter
Plot")
# Setting the X and Y labels
[Link]('Day')
[Link]('Tip')
[Link]()
[Link]()

Output:

64
DE With Python MCA I Year II Semester

LINE CHART

import pandas as pd
import [Link] as plt
# reading the database
data = pd.read_csv("[Link]")

# Scatter plot with day against tip


[Link](data['tip'])
[Link](data['size'])

# Adding Title to the Plot


[Link]("Line Plot")

# Setting the X and Y labels


[Link]('Day')
[Link]('Tip')

[Link]()

Output:

65
DE With Python MCA I Year II Semester

BAR CHART

import pandas as pd

import [Link] as plt #

reading the database

data = pd.read_csv("[Link]") #

Bar chart with day against tip

[Link](data['day'], data['tip'])

[Link]("Bar Chart")

# Setting the X and Y labels

[Link]('Day')

[Link]('Tip')

# Adding the legends

[Link]()

66
DE With Python MCA I Year II Semester

HISTOGRAM

import pandas as pd

import [Link] as plt #

reading the database

data = pd.read_csv("[Link]") #

histogram of total_bills

[Link](data['total_bill'])

[Link]("Histogram")

# Adding the legends

[Link]()

67
DE With Python MCA I Year II Semester

15 MACHINE LEARNING ALGORITHMS

15.a. Implement Linear Regression in Python.

import [Link] as plt

from scipy import stats

x = [5,7,8,7,2,17,2,9,4,11,12,9,6]

y = [99,86,87,88,111,86,103,87,94,78,77,85,86]

slope, intercept, r, p, std_err = [Link](x, y) def

myfunc(x):

return slope * x + intercept

mymodel = list(map(myfunc, x))

[Link](x, y)

[Link](x, mymodel)

[Link]()

print(r)

speed = myfunc(10)

print(speed)

if(r<=5):

print("Model is a bad fit")

else:

print("Model is a good fit")

68
DE With Python MCA I Year II Semester

69
DE With Python MCA I Year II Semester

15.b. Implement K-MEANS Clustering algorithm

from [Link] import KMeans

import [Link] as plt

x = [4, 5, 10, 4, 3, 11, 14 , 6, 10, 12]

y = [21, 19, 24, 17, 16, 25, 24, 22, 21, 21]

data = list(zip(x, y))

inertias = []

for i in range(1,11):

kmeans = KMeans(n_clusters=i)

[Link](data)

[Link]([Link])

[Link](range(1,11), inertias, marker='o')

[Link]('Elbow method') [Link]('Number

of clusters') [Link]('Inertia')

[Link]()

kmeans = KMeans(n_clusters=2)

[Link](data)

[Link](x, y, c=kmeans.labels_)

[Link]()

70
DE With Python MCA I Year II Semester

71
DE With Python MCA I Year II Semester

72

You might also like