program file vanshika
program file vanshika
CONTENTS
SR No Title Signat Signatu
ure re
(Teach (Examin
er) er)
PYTHON PROGRAMS
line=input("Enter a line:")
lowercount=uppercount=0
digitcount=alphacount=0
for a in line:
if a.islower():
lowercount+=1
elif a.isupper():
uppercount+=1
elif a.isdigit():
digitcount+=1
else:
alphacount+=1
print("Number of uppercase letters:",uppercount)
print("Number of lowercase letters:",lowercount)
print("Number of alphabets:",alphacount)
print("Number of digits:",digitcount)
# OUTPUT:-
# OUTPUT: -
Enter a string and press enter:- hi!! how are you? @user
Number of words =5
Number of characters= 23
Percentage of alphanumeric characters= 8.695652173913043 %
# PROGRAM 3:-TO SORT A LIST USING BUBBLE SORT.
alist=[23,5,72,45,12,3,9]
print("Original list is:",alist)
n=len(alist)
for i in range(n):
for j in range(0,n-i-1):
if alist[j]>alist[j+1]:
alist[j],alist[j+1]=alist[j+1],alist[j]
print("List after sorting:",alist)
# OUTPUT:-
alist= [15,6,13,22,3,52,2]
print("Original list is:",alist)
for i in range(1,len(alist)):
key=alist[i]
j=i-1
while j>=0 and key<alist[j]:
alist[j+1]=alist[j]
j=j-1
else:
alist[j+1]=key
print("List after sorting:",alist)
# OUTPUT:-
string=input("Enter a string:")
n=len(string)
rev=''
for i in range(n-1,-1,-1):
rev=rev+string[i]
if rev==string:
print("IT IS A PALINDROME")
else:
print("IT IS NOT A PALINDROME")
# OUTPUT:-
l=[]
lr = [ ]
n=int(input("Enter the number of elements in list:-"))
for i in range(0,n):
s=int(input("Enter the number:-"))
l.append(s)
print("Original list is",l)
for j in range(-1,-(n+1),-1):
p=l[j]
lr.append(p)
print("Reversed list is",lr)
# OUTPUT:-
a=[]
n= int(input("Enter the number of elements in list:"))
for x in range(0,n):
element=input("Enter element" + str(x+1) + ":")
a.append(element)
max1=len(a[0])
temp=a[0]
for i in a:
if (len(i)>max1):
max1=len(i)
temp=i
print("The word with the longest length is:")
print(temp)
# OUTPUT:-
import random
def rand(n,m):
s=random.randint(n,m)
return s
# OUTPUT:-
# OUTPUT:-
1.0045248555348174
# PROGRAM 10:- MINIMUM ONE’S DIGIT IN TWO NUMBERS.
def comp(a,b):
a1=a%10
b1=b%10
if a1<b1:
return a
elif b1<a1:
return b
else:
return 0
n1=int(input("Enter the first number:-"))
n2=int(input("Enter the second number:-"))
ans=comp(n1,n2)
if ans==0:
print("Both have same one's digit")
else:
print(ans,"has minimum value of one's digit")
# OUTPUT:-
FILE TO BE READ:-
# CODE :-
f=open("poem.txt","r")
l=f.readlines()
for i in l:
print(i)
# OUTPUT:-
Where the mind is without fear and the head is held high
Where knowledge is free
Where the clear stream of reason has not lost its way
FILE TO BE READ:-
#CODE:-
text = open("poem.txt",'r')
word1="to"
word2="the"
c1=0
c2=0
for line in text:
words=line.split()
l=len(words)
for i in range(0,l):
s=words[i]
if s==word1:
c1=c1+1
if s==word2:
c2=c2+1
print("The word 'to' occurs",c1,"times")
print("The word 'the' occurs",c2,"times")
# OUTPUT:-
The word 'to' occurs 0 times
The word 'the' occurs 7 times
# PROGRAM 13:- TO CONVERT BINARY NUMBER TO DECIMAL NUMBER.
for i in range(len(b_num)):
digit = b_num.pop()
if digit == '1':
value = value + pow(2, i)
print("The decimal value of the number is", value)
# OUTPUT:-
def octalToDecimal(n):
num = n;
dec_value = 0;
base = 1;
temp = num;
while (temp)
last_digit = temp % 10;
temp = int(temp / 10);
dec_value += last_digit * base;
base = base * 8;
return dec_value;
num = 45621;
print(octalToDecimal(num));
# OUTPUT:-
19345
# PROGRAM 15: TO WRITE DATA TO CSV FILE.
import csv
fields = ['Name', 'Branch', 'Year', 'CGPA']
rows = [ ['Nikhil', 'COE', '2', '9.0'],
['Sanchit', 'COE', '2', '9.1'],
['Aditya', 'IT', '2', '9.3'],
['Sagar', 'SE', '1', '9.5'],
['Prateek', 'MCE', '3', '7.8'],
['Sahil', 'EP', '2', '9.1']]
filename = "university_records.csv"
with open(filename, 'w') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(fields)
csvwriter.writerows(rows)
# OUTPUT:-
# PROGRAM 16:-BINARY SEARCH IN AN ARRAY.
import pickle
emp1={'Empno':1201,'Name':'anushree','Age':25,'Salary':47000}
emp2={'Empno':1211,'Name':'zoya','Age':30,'Salary':48000}
emp3={'Empno':1251,'Name':'alex','Age':31,'Salary':50000}
empfile=open('emp.txt','wb')
pickle.dump(emp1,empfile)
pickle.dump(emp2,empfile)
pickle.dump(emp3,empfile)
empfile.close()
# OUTPUT:-
# PROGRAM 18:- IMPLEMENTATION OF STACK OPERATIONS.
def isempty(stk):
if stk==[]:
return True
else:
return False
def push(stk,item):
stk.append(item)
top=len(stk)-1
def pop(stk):
if isempty(stk):
print("Underflow!")
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item
def peek(stk):
if isempty(stk):
print ("Underflow!")
else:
return stk[len(stk)-1]
def display(stk):
if isempty(stk):
print("Stack Empty")
else:
top=len(stk)-1
print(stk[top],"<-top")
for a in range(top-1,-1,-1):
print(stk[a])
stack=[]
top=None
while True:
print("Stack Operations")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Dislay Stack")
print("5. Exit")
ch=int(input("Enter your choice"))
if ch==1:
item=int(input("Enter item"))
push(stack,item)
elif ch==2:
item=pop(stack)
print("popped item is",item)
elif ch==3:
item=peek(stack)
print("Topmost item is",item)
elif ch==4:
display(stack)
elif ch==5:
break
else:
print("Invalid choice!")
# OUTPUT:-
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-1
Enter item:-6
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-1
Enter item:-8
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-1
Enter item:-2
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-1
Enter item:-4
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-4
4 <-top
2
8
6
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-3
Topmost item is:-4
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-2
popped item is :-4
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-4
2 <-top
8
6
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-5
# PROGRAM 19:- TO MODIFY THE CONTENTS OF BINARY FILE.
string = b""
Flag = 0
pos = 0
data = string = file.read(1)
while data:
data = file.read(1)
if string == word:
file.seek(pos)
Flag = 1
break
else:
pos = file.tell()
data = string = file.read(1)
else:
string += data
continue
if Flag:
print("Record successfully updated")
else:
print("Record not found")
update_binary(word, new)
# OUTPUT:-
import math
a = int(input("Please Enter First Number of an G.P Series: : "))
n = int(input("Please Enter the Total Numbers in this G.P Series: : "))
r = int(input("Please Enter the Common Ratio : "))
# OUTPUT:-
# OUTPUT:-
# SQL OUTPUT :
# PROGRAM 22:- TO INTEGRATE MYSQL WITH PYTHON TO SEARCH AN
EMPLOYEE USING EMPID AND DISPLAY THE RECORD IF
PRESENT IN ALREADY EXISTING TABLE EMP, IF NOT
DISPLAY THE APPROPRIATE MESSAGE.
#OUTPUT:-
Python Executed Program Output:
# SQL OUTPUT :
# PROGRAM 23: TO INTEGRATE MYSQL WITH PYTHON TO SEARCH AN
EMPLOYEE USING EMPID AND UPDATE THE SALARY OF AN
EMPLOYEE IF PRESENT IN ALREADY EXISTING TABLE EMP,
IF NOT DISPLAY THE APPROPRIATE MESSAGE.
# OUTPUT :
Python Executed Program Output:
#SQL OUTPUT :
# PROGRAM 24: INTEGRATE SQL WITH PYTHON BY IMPORTING THE MYSQL
MODULE.
import mysql.connector
con=mysql.connector.connect(host='localhost', user='root',
password='A123456z',db='jinal')
a=con.cursor()
# OUTPUT:-