INDEX
1) Program to print the Fibonacci series - 0, 1, 1, 2, 3, 5, 8 . . .
2) Program that read a string and checks whether the string is
palindrome or not.
3) Program to display the elements of list twice, if it is a number and
display the element terminated with ‘*’ if it is not a number.
4) Program having function with default Arguments.
5) Program using global variable.
6) Program to get roll number, names and marks of the student and
store the details in a data file “[Link]”.
7) Program to write content of a list to a file [Link] using
writelines().
8) Program to count number of words in a file [Link].
9) Program that count and displays the occurance of alphabet E or e
and U or u in file [Link].
10) Program to display the size of a file in bytes.
11) Program to copy the data from text file “[Link]” to text file
"[Link]".
12) A program to write employee details available in the form of
dictionaries in a binary file [Link]
13) Program to create a CSV file to store student data(Name, Marks).
14) A program to read the records of the CSV file [Link] and display
them.
15) A program in python to count the number of alphabets present in
a text file "[Link]".
16) Program to implement Stack Operations.
17) Program to connect to MySQL database and create a table.
18) Python database connectivity program that insert records in the
table Student.
19) Python database connectivity program that update record of
student .
20) Python database connectivity program that fetch records from the
table student.
21) SQL Queries.
# Program to print the Fibonacci series - 0, 1, 1, 2, 3, 5, 8 . . .
first, second= 0,1
i=3
n = int(input("Enter the value of n "))
print("The Fibonacci series is :",)
print( first, second, end=” “)
while (i <= n):
third=first+second
print (third, end=” ”)
first = second
second = third
i=i+1
Output-
Enter the value of n 7
The Fibonacci series is :
0112358
# Program that read a string and checks whether the string is Palindrome or
not.
st=raw_input("enter the string")
l=len(st)
m=0
flag=1
for s in range(1,l):
if st[m]==st[-s]:
flag=flag+1
else:
flag=0
m=m+1
if flag==l:
print( " the string is palindrone")
else:
print ("the string is not a palindrone")
Output-
enter the stringmadam
the string is palindrome
#Program to display the elements of list twice, if it is a number and display
the element terminated with ‘*’ if it is not a number.
For example, if the content of list is as follows
MyList=['RAMAN',’21’,'YOGRAJ','3','TARA']
The output should be
RAMAN*
2121
YOGRAJ*
33
TARA*
def fun(L):
for I in L:
if [Link]():
print(2*I)
else:
print(I+'*')
MyList=['RAMAN','21','YOGRAJ','3','TARA']
fun(MyList)
Output-
RAMAN*
2121
YOGRAJ*
33
TARA*
# Program having function with default Arguments.
def sum(x, y=6, z=2):
c=x+y+z
return c
m=sum(3)
print (m)
Output-
11
# A program using global variable.
def state():
global tiger
tiger=15
print (tiger)
tiger=95
print(tiger)
state()
print(tiger)
Output-
95
15
15
# Write a program to get roll number, names and marks of the student and
store the details in a data file “[Link]”.
c=int(input("no of records"))
filein=open("[Link]","w")
for i in range(c):
print("enter details")
roll=int(input("enter roll no"))
name=input("enter name")
marks=float(input("enter marks"))
rec=str(roll)+","+name+","+str(marks)+"\n"
[Link](rec)
[Link]()
Output-
no of records3
enter details
enter roll no25
enter nameAmit
enter marks60.5
enter details
enter roll no26
enter nameRahul
enter marks70.5
enter details
enter roll no27
enter nameAnkit
enter marks80.5
#Program to write content of a list to a file [Link] using writelines().
fileout=open("[Link]","w")
list=[]
for i in range(5):
name=raw_input("enter name of the student:")
[Link](name+"\n")
[Link](list)
[Link]()
Output-
enter name of the student:jack
enter name of the student:hary
enter name of the student:peter
enter name of the student:david
enter name of the student:smith
# Program to count number of words in a file [Link].
eg
I like ice cream
I dont like junk foods
output should be 9
fb=open("[Link]","r")
tword = 0
Aline = [Link]()
while Aline:
L = [Link]()
tword=tword+len(L)
Aline = [Link]()
print ("Total No of words in the file are", tword)
[Link]()
Output-
Total No of words in the file are 9
# Program that count and displays the occurance of alphabet E or e and U or
u in file [Link].
eg If the file contains the lines:
Updated information
is simplified by official websites.
Eucount( ) function should display the output as:
Total No of E or e are 6
Total No of U or u are 1
Ecount = 0
Ucount = 0
fb = open("[Link]", 'r')
Str = [Link]()
while Str:
for ch in Str:
if ch=='e' or ch=='E':
Ecount += 1
elif ch=='u' or ch=='U':
Ucount += 1
Str = [Link]()
print ("Total No of E or e are", Ecount)
print ("Total No of U or u are", Ucount)
[Link]()
Output-
Total No of E or e are 4
Total No of U or u are 1
# Program to display the size of a file in bytes.
fileout=open("[Link]","r")
st=[Link]()
l=len(st)
print(l)
[Link]()
Output-
25
# Write a program to copy the data from text file “[Link]” to text file
"[Link]".
fr = open ("[Link]", "r")
fw = open ("[Link]", "w")
STR=[Link]()
while STR:
[Link](STR)
STR=[Link]()
print("File copied")
[Link]()
[Link]()
Output-
File copied
# A program to write employee details available in the form of dictionaries
in a binary file [Link].
import pickle
fh=open("[Link]","wb")
emp1={"name":"amit","age":25}
emp2={"name":"rahul","age":27}
emp3={"name":"sumit","age":35}
[Link](emp1,fh)
[Link](emp2,fh)
[Link](emp3,fh)
print("Details entered")
[Link]()
Output-
Details entered
# A program to create a CSV file to store student data(Name,Marks).
import csv
obj=open("[Link]","w")
fobj=[Link](obj)
[Link](["name", "marks"])
for i in range(2):
Name=input("enter name")
Marks=int(input("enter marks"))
[Link]([Name,Marks])
[Link]()
Output-
enter nameAmit
enter marks50
enter nameRahul
enter marks90
# A program to read the records of the CSV file [Link] and display them.
import csv
with open("[Link]","r") as myfile:
sr=[Link](myfile)
for rec in sr:
print(rec)
Output-
['name', 'marks']
[]
['amit', '50']
[]
['rahul', '70']
[]
# A program in python to count the number of alphabets present in
a text file "[Link]".
Alphacount = 0
fb = open("[Link] ", 'r')
Str = [Link]()
while Str:
for ch in Str:
if [Link]():
Alphacount += 1
Str = [Link]()
print(Alphacount)
[Link]()
Output-
32
# Program to implement Stack.
s=[]
c="y"
while(c=="y"):
print "[Link]"
print "[Link]"
print "[Link]"
choice=input("enter your choice")
if(choice==1):
a=raw_input("enter any number :")
[Link](a)
elif(choice==2):
if(s==[]):
print "stack empty"
else:
print "deleted element:", [Link]()
elif(choice==3):
l=len(s)
for i in range(l-1,-1,-1):
print s[i]
else:
print("wrong input")
c=raw_input("do u want to continue")
Output-
[Link]
[Link]
[Link]
enter your choice1
enter any number :4
do u want to continuey
[Link]
[Link]
[Link]
enter your choice1
enter any number :15
do u want to continuey
[Link]
[Link]
[Link]
enter your choice1
enter any number :24
do u want to continuey
[Link]
[Link]
[Link]
enter your choice3
24
15
do u want to continuen
#Program to connect to MySQL database namely Whitehall and create a
table Student having Name , Rollno and address.
import [Link] as sqltor
mycon=[Link](host="localhost", user="root", passwd="", database="Whitehall")
if mycon.is_connected():
print ("sucess")
cursor=[Link]()
[Link]("create table student (Name Char(20), Rollno int, Marks dec(4,1), address
char(20))")
Output-
Success
#Python database connectivity program that insert records in the table
Student.
import [Link] as sqltor
mycon=[Link](host="localhost", user="root", passwd="", database="Whitehall")
if mycon.is_connected():
print ("sucess")
cursor=[Link]()
[Link]("insert into student (Name, Rollno , Marks , address) values('Amit', 1,
50.5,'Katni')")
[Link]("insert into student (Name, Rollno , Marks , address) values('Rahul', 2,
70.5,'Jabalpur')")
[Link]("insert into student (Name, Rollno , Marks , address) values('Sumit', 3,
80.5,'Satna')")
[Link]("insert into student (Name, Rollno , Marks , address) values('Ankit', 4,
85.0,'Indore')")
[Link]("insert into student (Name, Rollno , Marks , address) values('Rakesh', 5,
45.5,'Bhopal')")
print ("Data of 5 Children Inserted in the table Student")
[Link]()
Output-
sucess
Data of 5 Children Inserted in the table Student
#Python database connectivity program that update record of student
having name Ankit .
import [Link] as sqltor
mycon=[Link](host="localhost", user="root", passwd="", database="Whitehall")
if mycon.is_connected():
print ("sucess")
cursor=[Link]()
[Link]("Update student set marks=marks+2 where Name='Ankit'")
print ("Marks of Ankit has been updated ")
[Link]()
Output-
sucess
Marks of Ankit has been updated
#Python database connectivity program that fetch records from the table
student.
import [Link] as sqltor
mycon=[Link](host="localhost", user="root", passwd="", database="Whitehall")
if mycon.is_connected():
print ("sucess")
cursor=[Link]()
[Link]("Select * from student")
data=[Link]()
for row in data:
print(row)
Output-
sucess
('Amit', 1, Decimal ('50.5'), 'Katni')
('Rahul', 2, Decimal( '70.5'), 'Jabalpur')
('Sumit', 3, Decimal( '80.5'), 'Satna')
('Ankit', 4, Decimal( '89.0'), 'Indore')
('Rakesh',5, Decimal( '45.5'), 'Bhopal')
# SQL Queries
SQL> Create table product (p_id char(20) ,productname char(20) ,manufacture char(20),
price integer);
SQL> desc product;
Name Null? Type
----------------------------------------- -------- ----------------------------
P_ID CHAR(20)
PRODUCTNAME CHAR(20)
MANUFACTURE CHAR(20)
PRICE INTEGER(11)
SQL> select * from product;
P_ID PRODUCTNAME MANUFACTURE PRICE
-------------------- -------------------- -------------------- ----------
SH06 SHAMPOO XYZ 120
fw12 FACE WASH XYZ 95
BS01 BATH SOAP ABC 55
FW05 FACE WASH ABC 45
TP01 TECOM POWDER LAK 40
SQL> select *
from product
where price between 50 and 100;
P_ID PRODUCTNAME MANUFACTURE PRICE
-------------------- -------------------- -------------------- ----------
fw12 FACE WASH XYZ 95
BS01 BATH SOAP ABC 55
SQL> update product set price=price+10;
5 rows updated.
SQL> select distinct productname
from product;
PRODUCTNAME
--------------------
BATH SOAP
FACE WASH
SHAMPOO
TECOM POWDER
SQL> select max(price) from product;
MAX(PRICE)
----------
142
SQL> select min(price) from product;
MIN(PRICE)
----------
54
SQL> select p_id, productname, manufacture, price
from product
order by price;
P_ID PRODUCTNAME MANUFACTURE PRICE
-------------------- -------------------- -------------------- ----------
TP01 TECOM POWDER LAK 54
FW05 FACE WASH ABC 60
BS01 BATH SOAP ABC 71
fw12 FACE WASH XYZ 115
SH06 SHAMPOO XYZ 142
SQL> insert into product
values('TP01' ,'TECOM POWDER','XYZ', 60);
1 row created.
SQL> select p_id , productname, manufacture,price
from product
where manufacture like 'A%';
P_ID PRODUCTNAME MANUFACTURE PRICE
-------------------- -------------------- -------------------- ----------
BS01 BATH SOAP ABC 55
FW05 FACE WASH ABC 45
SQL> select sum(price) from product;
SUM(PRICE)
----------
355
SQL> select manufacture, max(price) ,min(price)
from product
group by manufacture;
MANUFACTURE MAX(PRICE) MIN(PRICE)
-------------------- ---------- ----------
ABC 55 45
LAK 40 40
XYZ 120 95
SQL> delete from product
where p_id='TP01';
1 row deleted.