Professional Documents
Culture Documents
Hand On Practice Print
Hand On Practice Print
COMPUTER SCIENCE
PROGRAMMING IN
PYTHON 12
HAND ON PRACTICE & BOOK INSIDE
EXAMPLE PROGRAMS
20-21
Education is the passport to the future, for tomorrow
belongs to those who prepare for it today
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
ACKNOWLEDGEMENT
COMPUTER SCIENCE
PROGRAMMING IN PYTHON
Prepared by
DEPARTMENT OF COMPUTER SCIENCE
ALAGAPPA SCHOOLS, CH-84
1.V.Devanathan M.Sc (CS).,M.Phil.,Ph.D., 2. B.Sharmila B.Sc(CS).,B.Ed.,
PGT Computer Science TGT Computer Science
Alagappa Schools Alagappa Schools
Purasaiwalkam Purasaiwalkam
Chennai-84. Chennai-84.
E-mail:vigneshwardeva@gmail.com E-mail:sharmilababu11@gmail.com
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
TABLE OF CONTENT
PAGE
UNIT NO CHAPTER TITLE
NO
1 Function 1
UNIT- I
Problem 2 Data Abstraction nil
Solving 3 Scoping 2
Techniques
4 Algorithmic Strategies nil
Python -Variables and
5 nil
Operators
UNIT- II 6 Control Structures 3
Core
Python 7 Python functions 21
Strings and String
8 35
manipulations
UNIT-III Lists,Tuples,Sets and
9 49
Modularity Dictionary
and OOPS 10 Python Classes and objects 62
11 Database Concepts nil
UNIT-IV Structured Query Language
Database 12 82
(SQL)
concepts
and MySql 13 Python and CSV files 87
Importing C++ programs in
14 91
UNIT-V Python.
Integrating Data manipulation through
15 99
Python with SQL
MySql and Data visualization using pyplot:
C++ 16 line chart, pie chart and bar 106
chart
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
CHAPTER 1
FUNCTION
HANDS ON EXPERIENCE:
1
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
CHAPTER 3
SCOPING
HANDS ON PRACTICE:
1. Observe the following diagram and write the pseudo code for
the following.
2
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
CHAPTER 6
CONTROL STRUCTURES
HANDS ON EXPERIENCE:
Output:
3
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
4
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding
Output:
5
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output :
Coding:
Output:
6
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
7
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
8
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
*****
****
***
**
*
Coding:
Output:
Coding:
Output:
9
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Coding:
Output :
10
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
11
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
EXAMPLE-PROGRAMS
Coding:
Output:
2.Program to check the age and print whether eligible for voting.
Coding:
Output:
12
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Output:
Output:
13
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
14
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
15
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
9.Program to illustrate the use of for loop - to print single digit even
number.
Coding:
Output:
2468
16
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
10.Program to illustrate the use of for loop - to print single digit even
number with else part
Coding:
Output:
Coding:
Output:
Coding:
Output:
17
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
13.Program to illustrate the use nested loop -for within while loop.
Coding:
Output:
Coding:
Output:
Jump stat
18
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
19
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Output:
20
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
LESSON-7
PYTHON FUNCTIONS
HANDS ON EXPERIENCE:
Output 1:
Output 2:
21
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
22
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
23
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
24
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
EXAMPLE-PROGRAMS
Coding:
Output:
Hello python
Coding:
Output:
25
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
15
Coding:
Output:
26
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
27
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
28
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output
:
29
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
30
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
31
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
32
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
33
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
19. Program to illustrate that to use Function input() and apply eval()
function.
Coding:
Coding:
Output:
34
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Lesson 8
STRINGS AND STRINGS MANIPULATION
HANDS ON EXPERIENCE:
Coding:
Output:
Coding:
Output:
35
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Output:
36
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
37
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
38
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
39
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
11. Write a program to count the number of characters words and lies
in a given string.
Coding:
Output:
40
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
EXAMPLE-PROGRAMS
1. Program to access each character with its positive subscript of a
giving string.
Coding:
Output:
Coding:
Output:
41
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
42
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output
Coding:
Output
43
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output
44
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output
45
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Output:
Coding:
Output:
46
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
13.Program that accept a string from the user and display the same
after removing vowels from it.
Coding:
Output:
47
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
48
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Lesson 9
LISTS,TUPLES,SETS AND DICTIONARY
HANDS ON EXPERIENCE:
Coding:
Output:
Coding:
Output:
49
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
5. Write a program that finds the sum of all the numbers in a tuples
using while loop.
Coding:
Output:
57
50
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Output:
Output:
51
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
52
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
12. Write a program that counts the number of times a value appears
in the list. Use a loop to do the same
Coding:
Output:
13. Write a program that prints the maximum and minimum value in
a dictionary.
Coding:
Output:
53
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
EXAMPLE-PROGRAMS
Coding:
Output:
54
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding
Output
Coding
Output:
55
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
56
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
57
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Output:
Output:
58
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
59
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
10. Program using a function that returns the area and circumference
of a circle whose radius is passed as an argument.two values
using tuple assignment.
Coding:
Output:
Coding:
Output:
60
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
61
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Lesson 10
PHYTHON CLASSES AND OBJECTS
HANDS ON EXPERIENCE:
Output:
Output:
62
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
3. Find the error in the following program to get the given output
Coding:
Output:
Output:
63
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
64
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Output:
65
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
66
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
67
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Output:
68
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
EXAMPLE-PROGRAMS
Coding:
Output:
Coding:
Output:
69
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
Coding:
Output:
70
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
71
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Output:
Output
72
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
Output:
73
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
74
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Output:
75
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
76
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Coding:
77
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Output:
78
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
11. Program to store product and its cost price. Display all the
available products and prompt to enter quantity of all the products.
Finally generate a bill which displays the total amount to be paid.
Coding:
79
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
80
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Output:
81
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
CHAPTER 12
STRUCTURED QUERY LANGUAGE
HANDS ON EXPERIENCE:
82
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
4. In the above table, set the employee number as primary key and
check for NULL values in any field.
Alter table employee modify empno integer NOT NULL PRIMARY
KEY;
83
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
84
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
values(‘A1005’,’Balaguru’,’Engineer’,30000,15000);
86
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
CHAPTER 13
HANDS ON EXPERIENCE:
Program:
87
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Output:
Output:
Enter NameGanesh
Enter marks in 5 subjects
78
89
96
95
87
Enter Name Ramesh
Enter marks in 5 subjects
56
55
45
67
70
Enter Name Rakesh
Enter marks in 5 subjects
70
65
87
90
77
Enter Name Rahul
Enter marks in 5 subjects
98
98
100
100
100
Enter Name Suresh
Enter marks in 5 subjects
58
65
67
89
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
76
56
['Sl.no', 'Name', 'Tamil', 'English', 'Maths', 'Science', 'Social', 'Total']
[]
['1', 'Ganesh', '78', '89', '96', '95', '87', '445']
[]
['2', ' Ramesh', '56', '55', '45', '67', '70', '293']
[]
['3', ' Rakesh', '70', '65', '87', '90', '77', '389']
[]
['4', ' Rahul', '98', '98', '100', '100', '100', '496']
[]
['5', ' Suresh', '58', '65', '67', '76', '56', '322']
[]
90
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
CHAPTER 14
HANDS ON EXPERIENCE:
Public member
void display() assign the sum of mark1 ,mark2,score in total.
invokeWriteno(),Writemark() and Writescore()
Display the total also.
Save the c++ program in afile called hybrid. Write a python
program to
execute the hybrid.cpp
Procedure:
Open notepad and type the following code and save it as hybrid.cpp
#include<iostream>
using namespace std;
class Student
{
protected:
int Rno;
public:
void Readno(int r)
{
Rno=r;
}
void Writeno()
{
cout<<"\nRoll no : "<<Rno;
}
};
class Test :public Student
{
protected:
float Mark1,Mark2;
public:
void Readmark (float m1,float m2)
{
Mark1=m1;
92
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Mark2=m2;
}
void Writemark()
{
cout<<"\n\n\tMarks Obtained\n ";
cout<<"\n Mark1 : "<<Mark1;
cout<<"\n Mark2 : "<<Mark2;
}
};
class Sports
{
protected:
int score;// score = Sports mark
public:
void Readscore (int s)
{
score=s;
}
void Writescore()
{
cout<<"\n Sports Score : "<<score;
}
};
class Result :public Test,public Sports
{
int Total;
public:
void display()
{
Total = Mark1 + Mark2 + score;
Writeno();
Writemark();
Writescore();
cout<<"\n\n Total Marks Obtained : "<< Total<<endl;
93
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
}
};
int main()
{
Result stud1;
stud1.Readno(1201);
stud1.Readmark(93.5,95);
stud1.Readscore(80);
cout<<"\n\t\t\t HYBRID INHERITANCE PROGRAM\n";
stud1.display();
return 0;
}
Now type the python program in new notepad file and name it as
hybrid.py
import sys, os, getopt
def main (argv):
cpp_file=''
exe_file=''
opts, args=getopt.getopt(argv,"i:",['ifile='])
for o, a in opts:
if o in("-i","--ifile"):
cpp_file=a+'.cpp'
exe_file=a+'.exe'
run(cpp_file,exe_file)
def run(cpp_file,exe_file):
print("Compiling "+cpp_file)
os.system('g++ '+ cpp_file +' -o '+exe_file)
print("Running "+exe_file)
print(" ------ ")
print
os.system(exe_file)
print
if name ==' main ':
main(sys.argv[1:])
94
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Output:
#include <iostream>
using namespace std;
void printBoundary (int a[][10], int m, int n)
{
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
{
95
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
if (i==0|| j==0||i==m-1||j==n-1
cout<<a[i][j]<<" ";
else
cout<<" ";
}
cout <<endl ;
}
}
int main()
{
int a[10][10] ,i,j,m,n;
cout<<"Enter more than 3 x3 "<<endl;
cin>>m>>n;
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
cout<<"enter the values for array”;
cin>>a[i][j];
}
}
system("cls");
cout<<"\n\nOriginal Array\n";
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"\n\n The Boundry element\n";
printBoundary(a, m, n);
return 0;
96
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
}
Open notepad and type the following code and save as border.py
import sys, os, getopt
def main (argv):
cpp_file=''
exe_file=''
opts, args=getopt.getopt(argv,"i:",['ifile='])
for o, a in opts:
if o in("-i","--ifile"):
cpp_file=a+'.cpp'
exe_file=a+'.exe'
run(cpp_file,exe_file)
def run(cpp_file,exe_file):
print("Compiling "+cpp_file)
os.system('g++ '+ cpp_file +' -o '+exe_file)
print("Running "+exe_file)
print(" ------ ")
print
os.system(exe_file)
print
if name ==' main ':
main(sys.argv[1:])
In command prompt type the following
Python c:\pyprg\border.py –i c:\pyprg\border
97
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Output:
98
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
CHAPTER 15
HANDS ON EXPERIENCE:
Program:
import sqlite3
import io
import csv
d=open('c:\\pyprg\\sql.csv','w')
c=csv.writer(d)
con=sqlite3.connect("db1.db")
cursor=con.cursor()
cursor.execute("Drop table customer")
cursor.execute("Create table customer(cust_id char(5),cust_name
char(30),
address char(50), phone_no integer, city char(20));")
99
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Output:
Hrithik
Enter 3 address
14/1 Pritam Pura
15A, Park Road
7/2 Vasant Nagar
Enter 3 phonenos
41206819
61281921
26121949
Enter 3 cities
Delhi
Kolkata
Delhi
2. Consider the following table GAMES. Write a python program to
display the records for question i) to iv) and give output for SQL
queries v) to viii)
Table Games
Date);")
game_data=[(101,'Padmaja','Carom Board',2,5000, '2014-01-23'),
(102,'Vidhya','Badminton',2,12000,'2013-12-12'),
(103,'Guru','Table Tennis',4,8000,'2014-02-14'),
(105,'Keerthana','Carom Board',2,9000,'2014-01-01'),
(108,'Krishna','Table Tennis',4,25000,'2014-03-19')]
for row in game_data:
format_str="""Insert into games
values("{GC}","{name}","{Gname}","{num}","{Pmoney}","{Sdate
}");"""
sql_command=format_str.format(GC=row[0],name=row[1],Gname=
row[2],num=r
ow[3],Pmoney=row[4],Sdate=row[5])
cursor.execute(sql_command)
conn.commit()
conn.close()
i) To display the names of all Games with their Gcodes in descending
order of their schedule date.
Program :
import sqlite3
conn=sqlite3.connect("Games.db")
cursor=conn.cursor()
cursor.execute("Select Gamename, Gcode from Games order by
Scheduledate
desc;")
res=cursor.fetchall()
print(*res,sep="\n")
conn.close()
Output:
('Table Tennis', 108)
('Table Tennis', 103)
102
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
ii) To display the details of those games which are having prize
money more than 7000
Program:
import sqlite3
conn=sqlite3.connect("games.db")
cursor=conn.cursor()
cursor.execute("select * from games where prizemoney>7000;")
res=cursor.fetchall()
print(*res,sep='\n')
conn.close()
Output:
(102, 'Vidhya', 'Badminton', 2, 12000, '2013-12-12')
(103, 'Guru', 'Table Tennis', 4, 8000, '2014-02-14')
(105, 'Keerthana', 'Carom Board', 2, 9000, '2014-01-01')
(108, 'Krishna', 'Table Tennis', 4, 25000, '2014-03-19')
iii) To display the names and game names of the players in ascending
order of game name.
Program:
import sqlite3
conn=sqlite3.connect("games.db")
cursor=conn.cursor()
103
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Output:
('Vidhya', 'Badminton')
('Padmaja', 'Carom Board')
('Keerthana', 'Carom Board')
('Guru', 'Table Tennis')
('Krishna', 'Table Tennis')
Program:
import sqlite3
conn=sqlite3.connect("games.db")
cursor=conn.cursor()
cursor.execute("select sum(number*prizemoney) from games")
res=cursor.fetchall()
print(*res,sep='\n')
conn.close()
Output:
(184000,)
104
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Program:
import sqlite3
conn=sqlite3.connect("games.db")
cursor=conn.cursor()
cursor.execute("select * from games order by gamename")
res=cursor.fetchall()
print(*res,sep='\n')
conn.close()
Output:
(102, 'Vidhya', 'Badminton', 2, 12000, '2013-12-12')
(101, 'Padmaja', 'Carom Board', 2, 5000, '2014-03-01')
(105, 'Keerthana', 'Carom Board', 2, 9000, '2014-01-01')
(103, 'Guru', 'Table Tennis', 4, 8000, '2014-02-14')
(108, 'Krishna', 'Table Tennis', 4, 25000, '2014-03-19')
105
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
CHAPTER – 16
HANDS ON EXPERIENCE:
1. Create a plot. Set the title, x and y labels for both axes.
Program:
import matplotlib.pyplot as plt
x=[2,4,5]
y=[5,7,8]
plt.plot(x,y)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Line graph')
plt.show()
Output:
106
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Output:
Program:
import matplotlib.pyplot as plt
years=[2010, 2011,2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
avg=[85, 86, 88, 89, 86, 87, 89, 90, 92, 94]
plt.plot(years,avg)
107
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
periods=[1,2,2,3,2,1]
y_positions=range(len(daylabels))
plt.bar(y_positions,periods)
plt.xticks(y_positions,daylabels)
plt.ylabel("Periods")
plt.title("No.of. Computer Science Periods")
plt.show()
Output:
109
V.Devanathan M.Sc (CS)., M.Phil., Ph.D., B.Sharmila B.Sc(CS).,B.Ed.,
Program:
import matplotlib.pyplot as plt
slices=[7,2,2,13]
activities=['sleeping','eating', 'working','playing']
cols=['c','m','r','b']
plt.pie(slices, labels=activities, colors=cols,startangle=90,
shadow=True,
explode=(0,0,0.1,0),autopct='%1.1f%%')
plt.title('Interesting Graph \nCheck it out')
plt.show()