Python Manual
Python Manual
TIRUCHENGODE
V SEMESTER
BATCH (2021-2023)
LAB MANUAL
Procedure:
Step 1: Start a process.
Program:
Output:
First number: 10
Second number: 30
Procedure:
Step 1: Start a process.
Program:
# Python program to demonstrate numeric value
a=5
print("Type of a: ", type(a))
b = 5.0
print("\nType of b: ", type(b))
c = 2 + 4j
print("\nType of c: ", type(c))
String1 = 'Welcome to the Geeks World'
print("String with the use of Single Quotes: ")
print(String1)
Output:
Aim:
Procedure:
Step 1: Start a process.
Program:
Output:
Enter first number: 10
Enter second number: 20
The sum of 10 and 20 is 30.0
The subtraction of 10 and 20 is -10.0
The multiplication of 10 and 20 is 200.0
The division of 10 and 20 is 0.5
Aim:
Procedure:
Step 1: Start a process.
Step5: If the number is less than 0 then display Sorry, factorial does not exist for negative
numbers
Program:
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
# take input from the user
num = int(input("Enter a number: "))
# check is the number is negative
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",recur_factorial(num))
Output:
Enter a number: 5
The factorial of 5 is 120
3.b) Program to perform filter() to filter only even numbers from given the list
Aim:
To write a python program using filter() to filter only even numbers from given the
list.
Procedure:
Step 1: Start a process.
Step 4: Then filter the even numbers and convert to list format
Program:
# returns True if the argument passed is even
def check_even(number):
if number % 2 == 0:
return True
return False
# converting to list
even_numbers = list(even_numbers_iterator)
print(even_numbers)
Output:
[2, 4, 6, 8, 10]
4.(a) Program to save image using Scipy
Aim:
Program:
from scipy import misc
import imageio
import matplotlib.pyplot as plt
plt.imshow(face)
plt.show()
Output:
4.(b) program to create Numpy array from the image
Aim:
Program:
from scipy import misc
import imageio
import matplotlib.pyplot as plt
img = imageio.imread('raccoon.png')
print(img.shape)
print(img.dtype)
plt.imshow(img)
plt.show()
Output:
(768, 1024, 3)
5. Program to perform various operations on List, Tuples, Set & dictionary
Aim:
To write a python program to perform various operations on List, Tuples, Set & dictionary
Procedure:
Step 1: Start a process.
Step 2: Add the 5,10 to the list and pop one element to list
Step 4: Add the 5,10 to the set and remove 5 from the set
Program:
# use of list, tuple, set and dictionary
# Lists
l = []
# Adding Element into list
l.append(5)
l.append(10)
print("Adding 5 and 10 in list", l)
# Popping Elements from list
l.pop()
print("Popped one element from list", l)
print()
# Set
s = set()
# Adding element into set
s.add(5)
s.add(10)
print("Adding 5 and 10 in set", s)
# Removing element from set
s.remove(5)
print("Removing 5 from set", s)
print()
# Tuple
t = tuple(l)
# Tuples are immutable
print("Tuple", t)
print()
# Dictionary
d = {}
# Adding the key value pair
d[5] = "Five"
d[10] = "Ten"
print("Dictionary", d)
# Removing key-value pair
del d[10]
print("Dictionary", d)
Output:
Adding 5 and 10 in list [5, 10]
Popped one element from list [5]
Adding 5 and 10 in set {10, 5}
Removing 5 from set {10}
Tuple (5,)
Dictionary {5: 'Five', 10: 'Ten'}
Dictionary {5: 'Five'}
6. Program to override super class constructor and method in subclass
Aim:
To write a python program to override super class constructor and method in subclass
Procedure:
Step 1: Start a process.
Step 3: class Father and class Son have the same method name
Step 4: The same method names in the base and inheritance classes. We call method
overriding.
Step 5: Writing constructor in base and inherited class we call constructor overriding.
Program:
#Overriding the base class constructor and method in sub class
class Father:
def __init__(self):
self.property=80000.00
def display_property(self):
print('Father\'s property=',self.property)
class Son(Father):
def __init__(self):
self.property=20000.00
def display_property(self):
print('Child\'s property=',self.property)
#create sub class instance and display father's property
s= Son()
s.display_property()
Output:
Child's property= 20000.0
Procedure:
Step 1: Start a process.
Program:
# iterable object
mylist = [-1, 2, 0, 4, 0]
# for loop
for i in mylist:
# Python catch multiple exceptions
try:
# raise when divide by zero
print("thedivision is:", 10/i)
# this block will be executed if error occurs
except:
# raise an error
print("Error occurs while dividing")
Output
Aim:
Procedure:
Step 1: Start a process.
Step 4: If the file is not exists and display yourfile.txt does not exist
Program:
if os.path.isfile(fname):
f=open(fname,'r')
else:
print(fname+'doesnot exist')
sys.exit()
Output:
Enter filename:myfile.txt
The file contents are:
Welcome to Python Programming
Enter filename:yourfile.txt
yourfile.txtdoesnot exist
9. Program to read a CSV file consists of students marks statements and write in
another CSV file with total, average and grade.
Aim:
To write a python program to read a CSV file consists of students marks statements
and write in another CSV file with total, average and grade.
Procedure:
Step 1: Create a CSV file manually with header (“Reg. No.”,
“Name”,”Mark1”,”Mark2”,”Mark3”,)
Step 2: Enter the values in the CSV file manually
Step 3: Open the CSV file and read its content
Step 4: Calculate the total, average and grade using the values read
Step 5: Create list and store the values read from the file and calculated values
Step 6: Create a new CSV file
Step 7: Write all the content to a new CSV file
Step 8: Close the file
Program
import csv
def calculate_grade(average):
if average >= 90:
return 'A'
elif average >= 80:
return 'B'
elif average >= 70:
return 'C'
elif average >= 60:
return 'D'
else:
return 'F'
# Example usage:
input_file = 'C:\Program Files\Python311\marks.csv'
output_file = 'C:\Program Files\Python311\marks_with_grade.csv'
process_marks(input_file, output_file)
Output
CSV Input File Content
To write a python program to retrieve all rows from employee table and display the
common values in tabular column in MYSQL.
Procedure:
Step 1: Start a process.
Step 4: Execute a SELECT query, Use the cursor's execute () method to execute an SQL
SELECT query to retrieve all rows from the employee table.
Step 5: Fetch the results: Use the cursor's fetchall() method to retrieve all rows returned by
the SELECT query.
Step 6: Process the retrieved data and Close the cursor and the database connection
Program:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="venu"
)
mycursor = mydb.cursor()
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Output:
Result:
The above program has been verified successfully