Practical File_Aryan
Practical File_Aryan
def eoz(x):
n=x
e,z,o=0,0,0
while n !=0:
if n%10 == 0:
z +=1
elif (n%10)%2 == 0 and n%10!=0:
e += 1
else:
o += 1
n //= 10
print(e,o,z,"are the number of even, odd and zero digits respectively.")
def minmax(x):
n=x
max = 0
min = 0
while n != 0:
if (n % 10) >= max:
max = n % 10
n //=10
m=x
while m !=0:
if (m%10) <= min:
min = m % 10
m//=10
print(min,'is minimum digit.',max,"is maximum digit.")
def sumav(x):
n=x
sum = 0
av = 0
cnt = 0
while n!=0:
sum += n%10
cnt += 1
n //=10
av = sum/cnt
print(sum,'is sum.',av,'is average.')
while True:
x = int(input("Enter a number: "))
ch = int(input("1. Count even, odd and zero digits. \n2. Display maximum and minimum. \n3. Find sum
and average of all digits. \n4.Exit"))
if ch == 1:
eoz(x)
elif ch ==2:
minmax(x)
elif ch == 3:
sumav(x)
elif ch==4:
break
else:
print("Incorrect input.")
Output:
def al(x):
n = int(input("Enter a number:"))
cnt = 0
for i in x:
if n == i:
print(x.index(i)+1)
break
else:
cnt+=1
if len(x) == cnt:
print(-1)
def prime(x):
cnt = 0
for i in x:
for j in range((i%2)+1):
if i%j==0:
break
cnt += 1
print(cnt)
def funcx(x):
cnt = 0
for i in x:
sum = 0
for j in range(i):
if i%j==0:
sum += j
if sum == i:
cnt += 1
print(cnt)
def rev(x):
for i in range(len(x)):
if x[i] > 1000:
n = str(x[i])
rev = n[::-1]
x[i] = int(rev)
print(x)
x = None
while True:
ch = int(input("1. Create a list. \n2.Search a number from the list. \n3. Count all prime numbers. \n4.
Count all the perfect numbers. \n5. Reverse all the elements in the list with a value greater than 1000. \
n6. Exit\n>>>"))
if ch == 1:
x = eval(input("Enter a list: "))
elif ch == 2:
if type(x) != list:
print("Create a list first!")
else:
al(x)
elif ch == 3:
if type(x) != list:
print("Create a list first!")
else:
prime(x)
elif ch == 4:
if type(x) != list:
print("Create a list first!")
else:
funcx(x)
elif ch == 5:
if type(x) != list:
print("Create a list first!")
else:
rev(x)
elif ch == 6:
quit()
else:
print("Incorrect option.")
def fxn(x):
for i in x:
if type(i) == int:
z=i
l=0
while z!=0:
r = z%10
l = max(r,l)
z//=10
print(i,l)
elif type(i) == str:
z=i
cnt = 0
for y in range(len(z)):
if z[y] in "aeiouAEIOU":
cnt += 1
print(i,cnt)
elif type(i) == bool:
if i == True:
print(1)
else:
print(0)
elif type(i) == float:
print(int(i)**2)
fxn((20.45,"thisfire",1243290,True))
def fxn(x):
str = ""
for i in range(len(x)):
if x[i].isalpha()==True:
strr = x[i]
if strr not in "zZ":
strr = chr(ord(strr)+1)
elif strr == 'z':
strr = 'a'
elif strr == 'Z':
strr = 'A'
str += strr
elif x[i].isnumeric()==True:
strr = x[i]
if strr != '9':
strr = chr(ord(strr)+1)
else:
strr = '0'
str+=strr
else:
str += '*'
print(str)
fxn("90best!!")
book = {}
def udfa():
global book
book = {}
cnt = 1
while True:
ch = eval(input("Enter name,price,qty,category as a list:"))
ch.append(ch[1]*ch[2])
book[cnt]=ch
ch2 = input("Do you wish to quit?(y/n)\n>>>")
if ch2 in "yY":
break
def udfb(bno):
global book
for i in book:
if i == bno:
print(book[bno])
def udfc():
global book
cnt = 0
for i in book:
if book[i][4] > 5000:
cnt += 1
print(cnt)
def udfd(cat):
global book
cnt = 0
for i in book:
if book[i][3] == cat:
cnt += 1
print(cnt)
while True:
ch=int(input("1. Create the dictionary. \n2. Search details according to book number. \n3. Count all
records with ant > 5000. \n4. Count all books with user given category.\n5.Quit\n>>> "))
if ch == 1:
udfa()
elif ch == 2:
bno = int(input("Enter the book number\n>>> "))
udfb(bno)
elif ch == 3:
udfc()
elif ch == 4:
cat = input("Enter category.")
udfd(cat)
elif ch == 5:
quit()
else:
print("Wrong input!")
MySQL
def create():
f1 = open("sample.txt","w")
while True:
str = input('enter content')
f1.write(str+'\n')
ans = input('want to continue')
if ans in "nN":
f1.close()
break
def funa():
x=int(input("Enter line size: "))
cnt=0
f1=open('sample.txt','r')
for line in f1.readlines():
if len(line) <= x:
cnt+=1
print(cnt, 'number of lines returned')
def count():
cnt=0
f2=open("sample.txt","r")
str=f2.read()
s=str.split()
for wd in s:
if wd[0] in 'tT':
cnt+=1
f2.close()
print(cnt)
def pali():
cnt=0
f2=open('sample.txt','r')
str=f2.read()
s=str.split()
for wd in s:
if wd==wd[::-1]:
cnt+=1
f2.close()
print(cnt)
def avgword():
f1 = open("sample.txt","r")
str = f1.read()
cnt = 0
wdcnt = 0
for wd in str.split():
cnt += 1
wdcnt += len(wd)
avgwd = wdcnt/cnt
f1.close()
print("avg word size is: ",avgwd)
def altword():
f1=open('sample.txt','r')
f2=open('two.txt','w')
str=f1.read()
cnt=0
for wd in str.split():
if cnt%2==0:
f2.write(wd+" ")
cnt+=1
f1.close()
f2.close()
f1=open("two.txt","r")
print(f1.read())
f1.close()
def consonant():
f1=open("sample.txt","r")
f2=open("two.txt","w")
for line in f1.readlines():
if line[0] not in "aeiouAEIOU":
f2.write(line)
f1.close()
f2.close()
f1.open("two.txt","r")
print(f1.read())
f1.close()
def to():
f1 = open("sample.txt")
str = f1.read()
cnt = 0
l = str.split()
for i in l:
if i == "to" or i == "To":
cnt +=1
print("number of \"to\"\'s are:", cnt)
f1.close()
def the():
f1 = open("sample.txt",'r')
lst = f1.readlines()
cnt = 0
for i in f1.readlines():
x = i.split()
if x[0] == "The" or x[0]=="the":
cnt += 1
print("Number of lines starting with the:", cnt)
while True:
ch=int(input("1. create\n2. count the number of lines starting with t \n3. count the number of lines <=
size \n4.count no of to \n5.count lines starting with the \n6. count palindrome words \n7.copy alternate
words \n8. copy lines not starting with vowel \n9. average word size\nenter choice\n>>>"))
if ch==1:
create()
elif ch==2:
count()
elif ch==3:
funa()
elif ch==4:
to()
elif ch==5:
the()
elif ch==6:
pali()
elif ch==7:
altword()
elif ch==8:
consonant()
elif ch==9:
avgword()
else:
print("Invalid input!")
ans=input("wish to go bk?")
if ans in"yY":
break
def convert():
f1 = open("first.txt","r")
f2 = open("second.txt","w")
str = f1
for i in range(len(str)):
if str[i].isalpha() == True:
if str[i] not in "zZ":
ch = str[i]
ch = chr(ord(str)+1)
f2.write(ch)
elif str[i] == "Z":
f2.write("A")
elif str[i] == "z":
f2.write("a")
elif str[i].isnumeric() == True:
if str[i] != "9":
ch = str[i]
f2.write(str(int(ch)+1))
else:
f2.write("0")
elif str[i].isalnum() == False:
f2.write("*")
f1.close()
f2.close()
convert()