Xii Practical File
Xii Practical File
Code:
fixed_charge=power*110
unit_charge=0
if units<=150:
unit_charge=units*5.5
elif units<=300:
unit_charge=units*6
elif units<=500:
unit_charge=units*6.5
else:
unit_charge=units*7
total=fixed_charge+unit_charge
print('the electricity bill charges Rs.',total)
Output:
Code:
Output:
if sum==n:
print('the given number is armstrong')
else:
print('the given number is not armstrong')
Output:
Output:
Program to input a string having some digits and calculate the sum of digits present in the string
# Program to input a string having some digits and calculate the sum of digits present in the string
Output:
Program to input a string and count the number of vowels and consonants in the string
# Program to input a string and count the number of vowels and consonants in the string
for i in s:
if i in 'aeiouAEIOU':
n_vowels +=1
elif i.isalpha():
n_consonants +=1
Output:
Program to create a dictionary of names and their marks and display those names who
have scored more than 75
# Program to create a dictionary of names and their marks and display those names who have scored more than 75
l=[]
for key in dict:
if dict[key]>75:
l.append(key)
Output:
Output:
Read.txt
Hello. This is a Shemford School.
We are in class XII.
Output:
Hello.#This#is#a#Shemford#School.#
We#are#in#class#XII.#
Program – 9
Program to read a text file and display the number of vowels and number of characters in
the file
def countVowels(str):
count=0
for i in str:
if i in 'aeiouAEIOU':
count=count+1
return(count)
def countTotal(str):
count=0
for i in str:
count=count+1
return(count)
fh=open('read.txt','r')
str=fh.read()
print('the number of vowels are ',countVowels(str))
print('the number of characters are ',countTotal(str))
fh.close()
Output:
Read.txt
Hello. This is a Shemford School.
We are in class XII.
Output:
the number of vowels are 15
the number of characters are 53
Program – 10
Program to read a text file and put all the lines having character ‘a’ in one file and
remaining lines in another file
# Program to remove all the lines that contain a and write it to another file
f1=open("read.txt","r")
f2=open( “first.txt","w")
f3=open(“second.txt","w")
str=f1.readline()
while str:
if " a " in str:
f2.write(str)
else:
f3.write(str)
str=f1.readline()
f1.close()
f2.close()
f3.close()
Output:
Read.txt:
Hello. This is a School.
We are in class XII.
Its a co-ed School.
We are here to study computer.
Output:
First.txt:
Hello. This is a School.
Its a co-ed School.
Second.txt:
We are in class XII.
We are here to study computer.
Program – 11
Program to create a binary file with id and password. Search for a given id and display
the password.
import pickle
def add(fh):
id=input('enter the id:')
pswd=input('enter the password:')
row={'id':id,'password':pswd}
pickle.dump(row,fh)
def search(fh):
ide=input('enter the id to be searched for password:')
flag=False
fh.seek(0)
try:
while True:
t=pickle.load(fh)
if t.get('id')==ide:
print('the password is:',t.get('password'))
flag=True
break
except:
if flag==False:
print('not found')
fh=open("data.dat",'ab+')
print('enter 1 to add data, 2 to search and 0 to exit')
ch=int(input('enter your choice'))
while(ch!=0):
if ch==1:
add(fh)
elif ch==2:
search(fh)
elif ch==0:
break
else:
print('wrong choice')
ch=int(input('enter your choice'))
fh.close()
Output:
Program to create a csv file by entering user id and password, read and search the
password for given user id
import csv
def add(fh):
id=input('enter the id')
pswd=input('enter the password')
row=(id,pswd)
wr=csv.writer(fh,delimiter=',')
wr.writerow(row)
def search(fh):
id=input('enter the id to be searched for password')
fh.seek(0)
rdr=csv.reader(fh)
for i in rdr:
if i[0]==id:
print('the password is',i[1])
break
else:
print('id not found')
fh=open("loginData.csv",'a+',newline='\r\n')
print('enter 1 to add data, 2 to search and 0 to exit')
ch=int(input('enter your choice'))
while(ch!=0):
if ch==1:
add(fh)
elif ch==2:
search(fh)
elif ch==0:
break
else:
print('wrong choice')
ch=int(input('enter your choice'))
fh.close()
Output:
File
school shemford
pc admin
Program – 13
dic={0:'zero',1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine'}
res=''
while n>0:
i=n%10
n=n//10
w=dic[i]
res=w+' '+res
print(res)
Output
Aim: program to accept a list and store the index of all non zero elements in a tuple
t=[]
for i in range(len(n)):
if n[i]!=0:
t.append(i)
tup=tuple(t)
print(tup)
Output
[0,45,0,78,12,0]
(1, 3, 4)
Program – 15
def push(s,a):
s.append(a)
print('done')
def pop(s):
if len(s)==0:
print('underflow')
else:
e=s.pop()
print(e)
def peek(s):
if len(s)==0:
print('stack empty')
else:
print(s[-1])
def display(s):
if len(s)==0:
print("stack empty")
else:
for i in s[::-1]:
print(i)
elif ch==2:
pop(stack)
elif ch==3:
peek(stack)
elif ch==4:
display(stack)
elif ch==9:
stack.clear()
print("stack emptied")
break
else:
print('wrong choice')
Output:
Program to demonstrate ddl sql queries for creating database and tables
Database changed
Program to demonstrate ddl sql queries for creating database and tables-2
5. To find the customers of saharanpur who have balance greater than or equal to 10000
import mysql.connector as c
db=c.connect(host='localhost',passwd='12345',user='root',db='bank')
cursor=db.cursor()
cursor.execute(statement)
records=cursor.fetchall()
for i in records:
print(i)
output:
(1001, 'Anuj', 'M', 'Saharanpur', datetime.date(1993, 2, 4), 9876543210, 1000)
Program to demonstrate update query using sql interfacting in python( money deposited
or withdrawn from an account of customer)
import mysql.connector as c
db=c.connect(host='localhost',passwd='12345',user='root',db='bank')
cursor=db.cursor()
cursor.execute(statement)
db.commit()
db.close()
Output:
Program to demonstrate insert query using sql interfacting in python( new customer
details)
import mysql.connector as c
db=c.connect(host='localhost',passwd='12345',user='root',db='bank')
cursor=db.cursor()
# sql query
cursor.execute(statement)
db.commit()
db.close()
Output:
import mysql.connector as c
db=c.connect(host='localhost',passwd='12345',user='root',db='bank')
cursor=db.cursor()
cursor.execute(statement)
db.commit()
db.close()
Output: