Wa0022.
Wa0022.
TERM-1
PROGRAM -1
Q. Write a program to print the Fibonacci Series using Function.
AIM:
Program to print the Fibonacci Series using Function.
SOURCE CODE
def fibonacci(n):
first =0
second=1
print(first,end=" ")
print(second,end=" ")
for a in range(1,n-1):
third=first+second
first,second=second,third
print("Fibonacci Series")
print("*****************")
fibonacci(n)
OUTPUT
Enter the Limit:20
Fibonacci Series
*****************
PROGRAM 2:
Q. Write a program to calculate and print roots of a quadratic equation using
function
AIM:
program to calculate and print roots of a quadratic equation using function
SOURCE CODE
import math
def quadratic(a,b,c):
if a==0:
print("\n Aborting")
else:
delta = b*b-4 *a * c
if delta >0:
root1=(-b + math.sqrt(delta))/(2*a)
root2=(-b - math.sqrt(delta))/(2*a)
print("Root1=",root1,"Root2=",root2)
elif delta==0:
root1=-b/(2*a)
print("Root1=",root1,"Root2=",root1)
else:
a=int(input("Enter a :"))
b=int(input("Enter b :"))
c=int(input("Enter c :"))
quadratic(a,b,c)
OUTPUT 1
Enter the coefficients below
Enter a :3
Enter b :5
Enter c :2
Roots are Real and UnEqual
Root1= -0.6666666666666666 Root2= -1.0
OUTPUT 2
Enter the coefficients below
Enter a :2
Enter b :3
Enter c :4
Roots are Complex and Imaginary
PROGRAM 3
Q. Write a program to check whether a number is palindrome or Armstrong or not
using function
AIM:
Program to check whether a number is palindrome or Armstrong or not using
function
SOURCE CODE
def calc(n):
s=0
rev=0
while n>0:
dig=n%10
rev=(rev*10)+dig
s=s+(dig*dig*dig)
n=n//10
return s,rev
#main
n=int(input(" Enter a number "))
num=n
x,y=calc(n)
if(num==1):
print( " ARMSTRONG & PALINDROME ")
elif(num==x):
print( " ARMSTRONG Number ")
elif(y==num):
print(" Number is PALINDROME ")
else:
print(" SORRY , Not a Palindrome or Not an Armstrong number")
OUTPUT
Number is PALINDROME
ARMSTRONG Number
PROGRAM 4:
Write a program to read a text file and display the number of alphabets ,vowels
consonants, digits and special characters in the file.
AIM:
Program to read a text file and display the number of alphabets ,vowels
consonants, digits and special characters in the file.
SOURCE CODE
x=open("sample.txt","r+")
a=v=c=d=sp=0
s=x.read()
for i in s:
if(i.isalpha()):
a=a+1
if(i in "AEIOUaeiou"):
v=v+1
else:
c=c+1
elif(i.isdigit()):
d=d+1
sp=sp+1
print("No of alphabets=",a)
print("No of vowels=",v)
print("No of consonants=",c)
Mar Ivanios Bethany School,Kalayapuram Page 10
Python Programming
print("No of digit=",d)
x.close()
OUTPUT
No of alphabets= 31
No of vowels= 12
No of consonants= 19
No of digit= 2
No of special characters= 3
PROGRAM : 5
Write a program to calculate simple interest using a function interest() that can
receive principal amount,time and rate and returns calculated simple interest. Do
specify default values for rate and time as 10% and 2 years respectively.
AIM:
SOURCE CODE:
#program to calculate simple interest with default values
def interest(principal,time=2,rate=0.10):
return principal*rate*time
#_main_
si1=interest(prin)
print("Simple interest with your provided ROI and time values is :")
si2= interest(prin,time,roi/100)
print("Rs ",si2)
OUTPUT:
Enter principal amount:6700
Rs. 1340.0
Rs 1608.0
PROGRAM :6
Q. Write a program to find a lottery winner(by generating random number
within a limit.
AIM:
Program to find a lottery winner
SOURCE CODE
import random
n=random.randint(1,6)
guess= int(input("Enter a number between 1 to 6:"))
if n== guess:
print("Congratulations ,You won the lottery")
else:
print("Sorry , Try again. The lucky number was :",n)
OUTPUT
AIM: program to read and display a text file content line by line with each word
separated by „#‟
SOURCE CODE:
f=open("file1.txt")
for line in f:
words=line.split()
for w in words:
print(w+'#',end='')
print()
f.close()
OUTPUT:
SOURCE CODE:
def count(x):
s=x.readlines()
c=0
for i in s:
w=i.split()
l=len(w)
c=c+1
#_main_
x=open("document.txt","r")
count(x)
x.close()
AIM:
Program to count the number of lines in a text file „Story.txt‟ which is starting with
an alphabet „A‟ or „a‟.
SOURCE CODE:
file= open("Story.txt","r")
lines=file.readlines()
count=0
for w in lines:
print(w)
if w[0]=="A" or w[0]=="a":
count=count+1
file.close()
OUTPUT:
import pickle
stu= {}
stufile=open('stu','wb')
ans='y'
while ans=='y':
stu['Rollno']=rno
stu['Name']=name
stu['Marks']=marks
pickle.dump(stu,stufile)
print("Records added")
stufile.close()
AIM:
SOURCE CODE:
# Program to store the sports details into a csv file
import csv
file=open("Sports.csv","w",newline='')
spwriter=csv.writer(file,delimiter='\t')
spwriter.writerow(['Sports','Competitions','Prizes won'])
ans='y'
while ans=='y':
rec=[sport,comp,prizes]
spwriter.writerow(rec)
file.close()
SOURCE CODE:
def searchList(L, item):
if item in L:
else:
ans = 'y'
print(list(L))
searchList(L, item)
SOURCE CODE:
import pickle
stu ={}
found=False
ans = 'y'
stu["Roll_No"]=roll_no
stu["Name"]=name
stu["Marks"]=marks
pickle.dump(stu, f)
f=open("Student.dat", "rb")
try:
while True:
stu = pickle.load(f)
print(stu)
found=True
except EOFError:
if found==False:
else:
print("Search successful")
f.close()
AIM: Program to read the lines from a file „poem.txt‟ and rewrite the lines
starting with letter „B‟ to another file „temp.txt‟.
SOURCE CODE:
f = open("D:\\pgm\\poem.txt", "r")
x = open("D:\\pgm\\temp.txt", "w")
line = f.readlines()
for w in line:
if w[0]=="B":
x.write(w)
f.close()
x.close()
AIM:
Program to count the number of “Me” or “My” words present in the text file
“Story.txt”
SOURCE CODE:
myfile=open("Story1.txt","r")
str=myfile.read()
word=str.split()
count=0
for i in word:
if i=="Me" or i=="My":
count+=1
OUTPUT
TERM-2
SOURCE CODE:
stack=[]
def PUSH():
stack.append(item)
def POP():
if stack==[]:
print("Stack is empty")
else:
stack.pop()
print('1.PUSH\n 2.POP')
more='y'
if choice==1:
PUSH()
elif choice==2:
POP()
print("invalid choice!!")
OUTPUT:
SOURCE CODE:
EVEN=[]
def PUSH(Num):
for n in Num:
if n%2==0:
EVEN.append(n)
if EVEN!=[]:
print("Stack content",EVEN)
else:
print("Stack is empty")
PUSH(Num)
OUTPUT
Age - Integer
SOURCE CODE:
EMP=[]
def PUSH():
E=[eno,ename,age]
EMP.append(E)
more='Y'
while more=='Y':
PUSH()
print("empolyee details:")
for i in range(len(EMP)-1,-1,-1):
print(EMP[i])
SOURCE CODE:
import mysql.connector as sqlcon
mycon=sqlcon.connect(host='localhost',user='root',password='hello',database='school')
mycur=mycon.cursor()
print(row)
mycon.close()
OUTPUT
SOURCE CODE
import mysql.connector as sqlcon
mycon=sqlcon.connect(host='localhost',user='root',password='hello',database='sch
ool')
mycur=mycon.cursor()
print(row)
mycon.close()
OUT PUT
SOURCE CODE:
import mysql.connector as sqlcon
mycon=sqlcon.connect(host='localhost',user='root',password='hello',database='sch
ool'')
mycur=mycon.cursor()
print(row)
mycon.close()
OUTPUT:
MySQL
SQL QUERIES
Set -1
1. Create a database School
2. Display all the databases
3. Use the database School
4. Create a table Teacher with the following structure:
Tno int Primary Key
Name varchar(15) Not Null
DOB date
Salary float
Deptno int
5. Display the structure of the table Teacher
6. Insert 5 records into Teacher table.
7. Display all the records in table Teacher.
2. Display the details of teachers whose salary is between 30000 and 40000(both
values included)
4. Display the name and salary of teachers whose salary is greater than 40000
5. Display the details of all the teachers whose department is „science „ from tables Teacher
and Dept.
6. Display the details of all the teachers whose department is „maths‟ from tables Teacher and
Dept(using Equi join)
4. Display the records of all the teachers whose designation column is null.
6. Update the designation of all the teachers as „PGT‟ whose salary is >=40000
1. Create a table student with the following structure and insert data
Rollno int Primary Key
Name varchar(15)
Gender char(1)
Marks float
2. Display the details of all the students
3. Display the total marks of all the students
4. Display total number of students
5. Display minimum and maximum marks of students
6. Display the number of male and female students
7. Display the details of students in ascending order of their names.
8. Display the details of students in descending order if their marks,
1.Create a table student with the following structure and insert data
ts