Python Practical Programs for BCA III
Python Practical Programs for BCA III
Aim
To write and implement python program using Sets and Lists datatypes
Algorithm
Step 1 : Start the program
Step 3 : Open the python launcher such as spider (or) Python ide and start to type the
program
Step 4 : Type the code as to implement sets and lists datatypes
Step 5 : To run the datatypes as save the program1.py as the result and display in the screen
Step 6 : Stop and execute the program
program1.py
print("Using Sets")
thisset={"apple","orange","cherry"}
print(thisset)
thisset.add("banana")
print(thisset)
print(len(thisset))
thisset.remove("banana")
print(thisset)
thisset.clear()
print(thisset)
Using Lists
['apple', 'orange', 'cherry']
['apple', 'orange', 'cherry', 'banana']
4
['apple', 'orange', 'cherry']
2. Tuples and Data Dictionaries Datatypes
Aim
To write and implement python program using Tuples and Data Dictionaries datatypes
Algorithm
Step 1 : Start the program
Step 3 : Open the python launcher such as spider (or) Python ide and start to type the
program
Step 4 : Type the code as to implement tuples and data dictionaries datatypes
Step 5 : To run the datatypes as save the program2.py as the result and display in the screen
Step 6 : Stop and execute the program
program2.py
print("Using Tuples")
thistuple = ("apple", "banana", "cherry")
print(thistuple)
print(thistuple[1])
for x in thistuple:
print(x)
print(len(thistuple))
#join method
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
print("Using Dictionaries")
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
print(len(thisdict))
thisdict.pop("model")
print(thisdict)
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
Output:
Using Tuples
('apple', 'banana', 'cherry')
banana
apple
banana
cherry
3
('a', 'b', 'c', 1, 2, 3)
Using Dictionaries
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
3
{'brand': 'Ford', 'year': 1964}
{'brand': 'Ford', 'year': 1964}
3. String Handling Function
Aim
To write and implement a string handling function using python
Algorithm
Step 1 : Start the program
Step 3 : Open the python launcher such as spider (or) Python ide and start to type the
program
Step 4 : Type the code such as function upper, lower, strip, replace etc.
Step 5 : To run the string handling function as save the program3.py as the result and display
in the screen
Step 6 : Stop and execute the program
program3.py
a="Ramakrishna"
print(a)
print(len(a))
a = " Hello, World! "
print(a.strip())
a = "Hello, World!"
print(a.lower())
a = "Hello, World!"
print(a.upper())
a = "Hello, World!"
print(a.replace("H", "J"))
a = "Hello, World!"
print(a.split(","))
txt = "The rain in Spain stays mainly in the plain"
str = "Ramakrishnan is a very good boy"
tmp = "-"
a = "Hello"
b = "World"
c=a+b
print(c)
string = "Ramakrishnan"
print(string.swapcase())
number = [3, 2, 8, 5, 10, 6]
largest_number = max(number);
print("The largest number is:", largest_number)
s = "28212"
print(s.isdecimal())
s = "32ladk3"
print(s.isdecimal())
Output:
Ramakrishna
11
Hello, World!
hello, world!
HELLO, WORLD!
Jello, World!
['Hello', ' World!']
HelloWorld
rAMAKRISHNAN
The largest number is: 10
True
False
4. Finding Temperature for Fahrenheit and Celsius
Aim
To write a python program and find the Temperature for Fahrenheit and Celsius
Algorithm
Step 1 : Start the program
Step 3 : Open the python launcher such as spider (or) Python ide and start to type the
program
Step 4 : Type the code to find the temperature for Fahrenheit, Celsius etc.
Step 6 : To run the temperature for Fahrenheit, celsius as save the program4.py as the result
and display in the screen
Step 7 : Stop and execute the program
program4.py
celcius=float(input("Enter the temprature in celcius:"))
f=(celcius*1.8)+32
print("temprature in Fahrenheit:",f)
fahrenheit=float(input("Enter the temprature in Fahrenheit:"))
c=(fahrenheit-32)/1.8
print("tempraure in celcius:",c)
Output:
Enter the temprature in celcius:34
temprature in Fahrenheit: 93.2
Aim
To write a python program and display the calendar for the given year and month
Algorithm
Step 1 : Start the program
Step 3 : Open the python launcher such as spider (or) Python ide and start to type the
program
Step 4 : Type the code and enter the year, month etc.
Step 5 : To run our code and save the program as program5.py as the result and display in the
screen
Step 6 : Stop and execute the program
Program5.py
import calendar
yy=int(input("Enter the year:"))
mm=int(input("Enter the month:"))
print(calendar.month(yy,mm))
Output:
6. Swapping two numbers using user defined functions
Aim
To write the python program and swap two numbers using user defined functions
Algorithm
Step 1 : Start the program
Step 3 : Open the python launcher such as spider (or) Python ide and start to type the
program
Step 4 : Type the code and scope of variable and declare.
Step 5 : To run our code and save the program as program6.py as the result and display in the
screen
Step 6 : Stop and execute the program
program6.py
def swap(a,b):
temp=a
a=b
b=temp
print("After swap: ",a,b)
return
Output:
Enter first number: 45
Aim
To write the python program and find the recursion for the given number
Algorithm
Step 1 : Start the program
Step 2 : Open the python launcher such as spider (or) Python ide and start to type the
program
Step 3 : Type the number for displaying the recursion value.
Step 5 : To run our code and save the program as program7.py as the result and display in the
screen
Step 6 : Stop and execute the program
program7.py
def tri_recursion(k):
if(k > 0):
result = k + tri_recursion(k - 1)
print(result)
else:
result = 0
return result
Output:
Recursion Example Results
1
3
6
10
15
21
8. Using Mathematical Function
Aim
To write the python program and use the mathematical functions.
Algorithm
Step 1 : Start the program
Step 2 : Open the python launcher such as spider (or) Python ide and start to type the
program
Step 3 : Type the code as log, cos, sib, sqrt, gamma, tangent. etc.
Step 5 : To run our code and save the program as program8.py as the result and display in the
screen
Step 6 : Stop and execute the program
program8.py
import math
x=4
print('log value:',math.log(x))
print('cos value:',math.cos(x))
print('sin value:',math.sin(x))
print('square root value:',math.sqrt(x))
print('gama value:',math.gamma(x))
print('factorial value:',math.factorial(x))
print('floor value:',math.floor(x))
print('tangent value:',math.tan(x))
print('degee value:',math.degrees(x))
print('exp value:',math.exp(x))
Output:
log value: 1.3862943611198906
cos value: -0.6536436208636119
sin value: -0.7568024953079282
square root value: 2.0
gama value: 6.0
factorial value: 24
floor value: 4
tangent value: 1.1578212823495777
degee value: 229.1831180523293
exp value: 54.598150033144236
9. Reading and writing a text file
Aim
To write the python program to read and write a text file.
Algorithm
Step 1 : Start the program
Step 2 : Open the python launcher such as spider (or) Python ide and start to type the
program
Step 3 : Type the code as will open a new text file and write the lines as open the new
creating text file as seek and read the text file and close the function.
Step 5 : To run our code and save the program as program9.py as the result and display in the
screen
Step 6 : Stop and execute the program
program9.py
file1 = open("myfile.txt","w")
L = ["Hello jaganathar temple of odisa"]
file1.write("Hello \n")
file1.writelines(L)
file1.close()
file1 = open("myfile.txt","r")
file1.seek(0)
print ("Output of Readline function is ")
print (file1.readline())
print
file1.seek(0)
print ("Output of Readlines function is ")
print (file1.readlines() )
print
file1.close()
f=open("binfile.bin","wb")
num=[5, 10, 15, 20, 25]
arr=bytearray(num)
f.write(arr)
f.close()
f=open("binfile.bin","rb")
num=list(f.read())
print (num)
f.close()
Output:
Output of Readline function is
Hello
Aim
To write the python program with SQL3 to create and insert table.
Algorithm
Step 1 : Start the program
Step 2 : Open the python launcher such as spider (or) Python ide and start to type the
program
Step 3 : Type the code and create test.db sqlite3 database file.
Step 4 : Type the code and execute create table and insert into table query to handle the data
Step 5 : To run our code and save the program as program10.py as the result and display in
the screen
Step 6 : Stop and execute the program
program10.py
import sqlite3
conn=sqlite3.connect('test.db')
print("Opened database successfully")
conn.execute('''CREATE TABLE Company
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL);''')
print("Table created successfully")
conn.execute("INSERT INTO COMPANY (ID, NAME, AGE) VALUES (1, 'Arun. A', 25)")
conn.execute("INSERT INTO COMPANY (ID, NAME, AGE) VALUES (2, 'Babu. S', 26)")
conn.execute("INSERT INTO COMPANY (ID, NAME, AGE) VALUES (3, 'Hari. D', 28)")
conn.execute("INSERT INTO COMPANY (ID, NAME, AGE) VALUES (4, 'Vignesh. E', 24)")
conn.execute("INSERT INTO COMPANY (ID, NAME, AGE) VALUES (5, 'Rangaraj. S', 29)")
conn.commit()
print("Records created successfully")
Output:
Opened database successfully
Table created successfully
Records created successfully
11. SQL3 Database Table View
Aim
To write the python program with SQL3 to view the table data by using select query.
Algorithm
Step 1 : Start the program
Step 2 : Open the python launcher such as spider (or) Python ide and start to type the
program
Step 3 : Type the code to connect test.db sqlite3 database file.
Step 4 : Type the code and execute to view the table data by using select query
Step 5 : To run our code and save the program as program11.py as the result and display in
the screen
Step 6 : Stop and execute the program
program11.py
import sqlite3
conn=sqlite3.connect('test.db')
print("Opened database successfully")
rs=conn.cursor()
rs.close()
print("Date viewed successfully")
Output:
Opened database successfully
Total records are: 5
Printing Each Row
ID= 1
NAME= Arun. A
AGE= 25
ID= 2
NAME= Babu. S
AGE= 26
ID= 3
NAME= Hari. D
AGE= 28
ID= 4
NAME= Vignesh. E
AGE= 24
ID= 5
NAME= Rangaraj. S
AGE= 29
Aim
To write the python program and read the data from csv file and find the location.
Algorithm
Step 1 : Start the program
Step 2 : Open the python launcher such as spider (or) Python ide and start to type the
program
Step 3 : Import pandas library.
sdata=pd.read_csv('stddata.csv')
print(sdata)
print(sdata.loc[2,:])
stddata.csv
Output:
Stdid Name Mark
0 1 Arun A 67
1 2 Bala S 76
2 3 Kalai R 87
3 4 Manoj K 98
4 5 Suresh S 81
Stdid 3
Name Kalai R
Mark 87
Name: 2, dtype: object
13. Graph for square the value
Aim
To write the python program and create a graph for square the value and print using
matplotlib.
Algorithm
Step 1 : Start the program
Step 2 : Open the python launcher such as spider (or) Python ide and start to type the
program
Step 3 : Import matplotlib and import numpy library.
x=np.arange(5)
print(x)
y=x*x
print(y)
plt.title('Square Value')
plt.plot(x,y,'r^')
Output:
14. Graph for Subplot
Aim
To write the python program and create a graph for Subplot value and print using matplotlib.
Algorithm
Step 1 : Start the program
Step 2 : Open the python launcher such as spider (or) Python ide and start to type the
program
Step 3 : Import matplotlib and import numpy library.
#Plot 1:
x=np.array([0,1,2,3])
y=np.array([3,8,1,10])
plt.subplot(1,2,1)
plt.plot(x,y)
#Plot 2:
x=np.array([0,1,2,3])
y=np.array([10,20,30,40])
plt.subplot(1,2,2)
plt.plot(x,y)
plt.show()
Output: