0% found this document useful (0 votes)
2 views28 pages

Python Practical Programs for BCA III

The document outlines various Python programming tasks, including the use of data types such as sets, lists, tuples, and dictionaries, as well as string handling, temperature conversion, calendar display, number swapping, recursion, mathematical functions, file handling, and SQL database operations. Each task includes an aim, algorithm, and example code, demonstrating practical applications of Python. Additionally, it covers reading from CSV files and creating graphs using matplotlib.

Uploaded by

rajkumarm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views28 pages

Python Practical Programs for BCA III

The document outlines various Python programming tasks, including the use of data types such as sets, lists, tuples, and dictionaries, as well as string handling, temperature conversion, calendar display, number swapping, recursion, mathematical functions, file handling, and SQL database operations. Each task includes an aim, algorithm, and example code, demonstrating practical applications of Python. Additionally, it covers reading from CSV files and creating graphs using matplotlib.

Uploaded by

rajkumarm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

1.

Sets and Lists Datatypes

Aim
To write and implement python program using Sets and Lists datatypes
Algorithm
Step 1 : Start the program

Step 2 : To implement a sets and lists datatypes

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)

print ("Using Lists")


thislist=["apple","orange","cherry"]
print(thislist)
thislist.append("banana")
print(thislist)
print(len(thislist))
thislist.remove("banana")
print(thislist)
thislist.clear()
Output:
Using Sets
{'apple', 'cherry', 'orange'}
{'apple', 'cherry', 'orange', 'banana'}
4
{'apple', 'cherry', 'orange'}
set( )

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 2 : To implement a tuples and data dictionaries datatypes

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 2 : To implement a string handling function

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 2 : To implement the temperature for fahrenheit and celsius

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 5 : Enter the temperature Fahrenheit as F=(( celcius *1.8)+32) and


celcius=C(Fahrenheit-32)/1.8

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

Enter the temprature in Fahrenheit:93.2


tempraure in celcius: 34.0
5. Displaying Calendar

Aim
To write a python program and display the calendar for the given year and month
Algorithm
Step 1 : Start the program

Step 2 : To implement and display calendar using python

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 2 : Swap two numbers using user defined functions

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

n1=int(input("Enter first number: "))


n2=int(input("Enter second number: "))
print("Befor swap: ",n1,n2)
swap(n1,n2)

Output:
Enter first number: 45

Enter second number: 25


Befor swap: 45 25
After swap: 25 45
7. Using Recursion Function

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

print("\n\nRecursion Example Results")


tri_recursion(6)

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

Output of Readlines function is


['Hello \n', 'Hello jaganathar temple of odisa']
[5, 10, 15, 20, 25]
10. SQL3 Database Insert

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.execute("SELECT ID, NAME, AGE FROM COMPANY")


records=rs.fetchall()
print("Total records are: ", len(records))
print("Printing Each Row")
for row in records:
print("ID= ", row[0])
print("NAME= ", row[1])
print("AGE= ", row[2], "\n")

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

Date viewed successfully


12. Read the data from csv file

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.

Step 4 : Create an CSV file with datas.


Step 5 : Using pandas read pd.read_csv(‘stddata.csv’).
Step 6 : Using sdata print the CSV file and .loc is used to print the sliced data.
Step 7 : To run our code and save the program as program12.py as the result and display in
the screen
Step 8 : Stop and execute the program
program12.py
import pandas as pd

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.

Step 4 : Create an x variable and assigned to arranged 5 elements.


Step 5 : Squaring the x value to get the y value.
Step 6 : Using plt.title(‘Square Value’) to declare the graph name.
Step 7 : To print the graph value the plt.plot() function is used to show plot.
Step 8 : To run our code and save the program as program13.py as the result and display in
the screen
Step 9 : Stop and execute the program
program13.py
import matplotlib.pyplot as plt
import numpy as np

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.

Step 4 : Create an array value x and y. Then assign it to variable.


Step 5 : Create a subplot to show or print the plot in graph.
Step 6 : Using subplot to take the value of x and y array for another subplot graph.
Step 7 : To print the graph value the plt.plot() function is used to show plot.
Step 8 : To run our code and save the program as program14.py as the result and display in
the screen
Step 9 : Stop and execute the program
program14.py
import matplotlib.pyplot as plt
import numpy as np

#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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy