Вы находитесь на странице: 1из 14

Chaper 9

1. Write a program to remove duplicate from a list.


a=eval(input("Enter a list"))
b=[]
for i in a:
if i not in b:
b.append(i)
print(b)
2. Write a program that prints the maximum value in a tuple
a=eval(input("Enter a tuple"))
b=a[0]
for i in a:
if i>=b:
b=i
print(b)
3. Write a program tht finds the sum of all the numbers in a tuples using while loop
a=eval(input("Enter a tuple"))
s=0
i=0
while(i<len(a)):
s+=a[i]
i+=1
print(s)
4. Write a program that finds sum of all even numbers in a list
a=eval(input("Enter a list"))
s=0
i=0
while(i<len(a)):
if(a[i]%2==0):
s+=a[i]
i+=1
print(s)
5. Write a program that reverse a list using loop
a=eval(input("Enter a list"))
for i in range(len(a)-1,-1,-1):
print(a[i])
6. Write a program to insert a value in a list at the specified location
a=eval(input("Enter a list"))
pos=eval(input("Enter a position to insert"))
elt=eval(input("Enter a element to insert"))
a.insert(pos,elt)
print(a)
7. Write a program that creates a list from 1 to 50 that are either divisible by 3 or
divisible by 6
a=[]
for i in range(1,51):
if(i%3==0 or i%6==0):
a.append(i)
print(a)
8. Write a program to create a list of numbers in the range 1 to 20. Then delete all the
numbers from the list that are divisible by 3.
a=[x for x in range(1,21)]
print(a)
for i in a:
if(i%3==0):
a.remove(i)
print(a)
9. Write a program that counts the number of times a value appears in the list. Use a
loop to do the same.
a=eval(input("Enter a list"))
s=eval(input("Enter a element to count"))
c=0
for i in a:
if i==s:
c+=1
print(c)
10. Write a program that prints the maximum and minimum value in a dictionary
a=eval(input("Enter a dictionary"))
print("Maximum=",min(a.values()))
print("Minimum=",max(a.values()))
Chapter 10

1. Write a program using class to store name and marks of students in list and pint total
marks.
class Student:
def get(self):
self.name=input("Enter name")
self.mark1=eval(input("Enter mark1"))
self.mark2=eval(input("Enter mark2"))
self.mark3=eval(input("Enter mark3"))
def cal(self):
self.total=self.mark1+self.mark2+self.mark3
print("Name=",self.name)
print("total=",self.total)
ob=Student()
ob.get()
ob.cal()
2. Write a program using class to accept three sides of triangle and print its area
import math
class Triangle:
def get(self):
self.a,self.b,self.c=eval(input("Enter the three sides for Triangle "))
def cal(self):
self.s=(self.a+self.b+self.c)/2.0
self.area=math.sqrt(self.s*(self.s-self.a)*(self.s-self.b)*(self.s-self.c))
print(self.area)
ob=Triangle()
ob.get()
ob.cal()
3. Write a menu driven program to read, display, add and subtract two distance.
class Distance:
def read(self):
self.a,self.b=eval(input("Enter two distance"))
def display(self):
print(self.a)
print(self.b)
def add(self):
self.c=self.a+self.b
print("add=",self.c)
def sub(self):
self.d=self.a+self.b
print("sub=",self.b)
ob=Distance()
ob.read()
ob.display()
ob.add()
ob.sub()
chapter 13
1. Write a python program to read the following Namlist.csv file and sort the data in
alphabetically order of names in a list
import csv
s=[]
f=open(‘NameList.csv’,’r’)
reader=csv.reader(f)
for i in reader:
s.append(i[1])
s.sort()
print(s)
2. Write a python program to accept the name and five subject mark of students. Find
the total and store all the details of students in CSV file.
import csv
s=[[‘Name’,’Tamil’,’English’,’Maths’,’Science’,’Social’]]
for i in range(1,6):
name=eval(input(“Enter name”))
tam=eval(input(“Enter tamil mark”))
eng=eval(input(“Enter English mark”))
mat=eval(input(“Enter Maths mark”))
sci=eval(input(“Enter science mark”))
soc=eval(input(“Enter social mark”))
s.append([name,tam,eng,mat,sci,soc])
print(s)
f=open(‘Student.csv’,’w’)
wr=csv.writer(f)
wr.writerows(s)
f.close()
chapter 14
1. Student.cpp
#include <iostream>
using namespace std;
class Student
{
protected:
int regno;
public:
void Readno(int rollno)
{
regno=rollno;
}
void Writeno()
{
cout<<"Register number "<<regno;
}
};
class Test:public Student
{
protected:
int mark1,mark2;
public:
void Readmark(float m1,float m2)
{
mark1=m1;
mark2=m2;
}
void Writemark()
{
cout<<"Mark1 "<<mark1;
cout<<"Mark2 "<<mark2;
}
};
class Sports
{
protected:
int score;
public:
void Readscore(int sc)
{
score=sc;
}
void Writescore()
{
cout<<"Score "<<score;
}
};
class Result:public Test,public Sports
{
private:
float total;
public:
void display()
{
total=mark1+mark2+score;
cout<<"Total "<<total;
}
};
int main()
{
Result ob;
ob.Readno(101);
ob.Readmark(99,98);
ob.Readscore(99);
ob.Writeno();
ob.Writemark();
ob.Writescore();
ob.display();
return 0;
}

demo.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:])
2. Border.cpp
#include<iostream>
using namespace std;
int main()
{
int a[10][10],i,j,r,c;
cout<<"Enter row and column";
cin>>r>>c;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
cout<<"Boundry elements"<<"\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==0||j==0||i==r-1||j==c-1)
{
cout<<a[i][j];
}
else
{
cout<<" ";
}
}
cout<<"\n";
}
}

demo.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:])
chapter 15
1. create an interactive program to accept the details from user and store it in a csv file
using python for the following table.
import sqlite3
import csv
con=sqlite3.connect(‘DB1.db’)
con.execute(‘drop table customer’)
con.execute(‘create table customer(cust_id integer primary key,cust_name
varchar(20), address varchar(20), phoneno integer, city varchar(20))’)
print(‘created’)
n=eval(input(“Enter n”))
d=[]
id1=[]
name=[]
add=[]
ph=[]
city=[]
for i in range(n):
id1.append(eval(input(“Enter id”)))
name.append(input(“Enter name”))
add.append((input(“Enter address”))
ph.append(eval(input(“Enter phone no”)))
city.append(eval(input(“Enter city”)))
for I in range(n):
con.execute(“insert into customer values(?,?,?,?,?)”, (id1[i] , name[i]
,add[i], ph[i], city[i])
con.commit()
d=open(‘sqlexcel.csv’,’w’)
c=csv.writer(d)
cur=con.cursor()
cur.execute(‘select * from customer’)
cur.fetchall()
print(*cur,sep=”\n”)
2. Consider the table games and give the output for sql queries
import sqlite3
con=sqlite3.connect(‘DB1.db’)
con.execute(‘drop table games’)
con.execute(‘create table games(Gcode integer,Name varchar(20),GameName
varchar(20),Number integer, PrizeMoney integer,ScheduleDate date)
con.execute(‘insert into games values(101,’Padmaja’,’Carom
Board’,2,5000,’01-23-2014’)”)
con.execute(‘insert into games values(102,’Vidhya’,’Badminton’,2,12000,’12-
12-2013’)”)
con.execute(‘insert into games values(103,’Guru’,’Table Tennis’,4,8000,’02-
12-2014’)”)
con.execute(‘insert into games values(105,’Keerthana’,’Carom
Board’,2,9000,’01-01-2014’)”)
con.execute(‘insert into games values(108,’Krishna’,’Table
Tennis’,4,25000,’03-19-2014’)”)
con.commit()
cur=con.cursor()

print(‘Display Gcode in Descending order’)


cur.execute(‘select * from games order by Gcode desc’)
res=cur.fetchall()
print(*res,sep=”\n”)

print(‘Display records whose price moey is more than 7000’)


cur.execute(‘select * from games where PrizeMoney>=7000’)
res=cur.fetchall()
print(*res,sep=”\n”)

print(‘Display name,gamename in Ascending order’)


cur.execute(‘select Name,GameName from games order by GameName’)
res=cur.fetchall()
print(*res,sep=”\n”)
print(‘Display Sum of price money’)
cur.execute(‘select sum(PrizeMoney) from games group by Number’)
res=cur.fetchall()
print(*res,sep=”\n”)

print(‘Display all the records)


cur.execute(‘select * from games’)
res=cur.fetchall()
print(*res,sep=”\n”)

Вам также может понравиться