0% found this document useful (0 votes)
10 views

project file 12th c (1)

Uploaded by

srishti8851
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

project file 12th c (1)

Uploaded by

srishti8851
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Program 1:

Write a program to create calculator using functions add subtract divide


and multiply.

Code:

#This function adds two numbers

def add(x,y):

return x+y

#This function subtracts two numbers

def subtract(x,y):

return x-y

#This function multiplies two numbers

def multipy(x,y):

return x*y

#This function divides two numders

def divide(x,y):

return x/y

print("select operation")

print("1.add")

print("2.subtract")

print("3.multipy")

print("4.divide")
while True:

#tale input from the user

choice=input("enter choice(1/2/3/4):")

#check if choice is one of these five options

if choice in('1','2','3','4','5'):

try:

num1=float(input("enter first no."))

num2=float(input("enter second no."))

except ValueError:

print("Invalid input. Please enter a number.")

continue

if choice=='1':

print(num1,"+", num2, "=", add(num1,num2))

elif choice=='2':

print(num1,"-", num2, "=", subtract(num1,num2))

elif choice=='3':

print(num1,"*", num2, "=", multipy(num1,num2))

elif choice=='4':

print(num1,"/", num2, "=", divide(num1,num2))

#check if user wants another calculation

#break the while loop if answer is no

next_calculation=input("Let's do next calculation?(yes/no):")

if next_calculation=="no":
break

else:

print("invalid input")

print(“srishti”)
Output:
Program 2:

Write a program to implement different types of functions argument.

Code:

def subtract(x,y):#positional arguement

z=x-y

print(x,y,z)

def total(a,b):#keyword arguement

c=a+b

print(c)

def product(p,q=84):#default parameter

r=p+q

print(p,q,r)

subtract(100,20)

total(b=23,a=56)

product(56)

print("srishti")
Output:
Program 7:

Write a program to pass a list to function and add new data in it.

Code:

def data(l1):

l1.append(60)

l=[10,20,30,40,50]

print("list before adding new data is",l)

data(l)

print("list after adding new data is",l)

print("srishti")
Output:
Program 17 :

Write a program to implement LEGB rule.

Code:

def calcsum(x,y):

z=x+y

return z

num1=int(input("Enter first no."))

num2=int(input("Enter second no."))

sum=calcsum(num1,num2)

print("Sum of given number is",sum)

print(“srishti”)
Output:
Program 3 :
Write a program to calculate size of a file.

Code :

f=open("C:\\Users\\HP\\Desktop\\srishti.txt","r")

str=f.read()

size=len(str)

print("the size of file in bytes")

print(size,"bytes")

f.close()
Output:
Program 4:

Write a program to count number of words and numder of lines in a file.

To count number of words

Code:

f=open("C:\\Users\\HP\\Desktop\\srishti.txt","r")

s=f.read()

p=s.split()

print("total number of words in the file is", len(p))

f.close()

print(”srishti”)
Output:
To count number of lines

Code:

F=open("C:\\Users\\HP\\Desktop\\srishti.txt","r")

s=f.readlines()

print("total number of lines in the file is",len(s))

f.close()

print(“srishti”)
Output:
Program 5 :

Write a program to display those lines which are starting with the vowel.

Code:

f=open("C:\\Users\\HP\\Desktop\\srishti.txt","r")

s=f.readlines()

print(s)

for i in s:

if i[0]in'aeiouAEIOU':

print(i)

f.close()

print(“srishti’’)
Output:
Program 6 :

Write a program to perform write operation on text file using both


operation.

By write operation

Code:

f=open('C:\\Users\\HP\\Desktop\\srishti1.txt','w')

s='where are you'

f.write(s)

print('srishti')

f.close()
Output:
By writelines operation

Code:

f=open('C:\\Users\\HP\\Desktop\\srishti1.txt','w')

L=["HELLO","this","is","python"]

f.writelines(L)

print("srishi")

f.close()

def data(l1):

l1.append(60)

l=[10,20,30,40,50]

print('list before adding new data is',l)

data(l)

print('list after adding new data is',l)

print('srishti')
Output:
Program 9 :

Write a program to perform write operation on binary file.

Code:

import pickle

f=open('C:\\Users\\HP\\Desktop\\srishti1.dat','wb')

s={'name':'nisha','number':123,'salary':1000}

s1={'name':'ankita','number':321,'salary':2000}

pickle.dump(s,f)

pickle.dump(s1,f)

f.close()

print("srishti")
Binary file:
Program 10 :

Write a program to perform read operation on binary file.

Code:

import pickle

f=open('C:\\Users\\HP\\Desktop\\srishti1.dat','rb')

try:

while true:

s=pickle.load(f)

print(s)

except:

f.close()

print('srishti')
Program 12 :

Write a program to update binary file.

Code:

import pickle

f=open('C:\\Users\\HP\\Desktop\\srishti1.dat','rb')

try:

while True:

p=f.tell()

s=pickle.load(f)

if s['name']=='astha':

s['salary']=s['salary']+1000

f.seek(pos)

pickle.dump(s,f)

except:

f.close()

print('srishti')
Output:
Program 11 :
Write a program to search operation in a binary file.

Code:

import pickle

f=open('C:\\Users\\HP\\Desktop\\srishti1.dat','rb')

try:

while True:

s=pickle.load(f)

if s["salary"]>2000:

print(s["name"])

except:

f.close()

print("srishti")
Program 18 :

Write a program to operate on csv file.


Code:

import csv

f=open("record.csv",'w')

a='y'

p=csv.writer(f)

while a=='y':

name=input("enter name")

rn=int(input("enter rollno."))

marks=int(input("enter your marks"))

l=[name,rn,marks]

p.writerow(l)

a=input("if you want to enter a new record enter yes otherwise no")

f.close()

print("srishti")
Output:
Program 14 :

Write a program to read a binary file.

Code:

import csv

f=open("record.csv",'r')

r=csv.reader(f)

for i in r:

print(r)

print("srishti")
Output:
Program 15 :

Write a program to implement stack operation.

1) push()

Code:

stk=()

top=None

def push():

if len(stk)==0:

stk.append(items)

top=top+1

print(top)

print("srishti")
Output:

2) pop()
stk=[19,39]

a=stk.pop()

print(a)

print(stk)

print("srishti")

Output:
Program 16 :
Write a program to display global and local variable.

Code:

#Global

def add(x,y=15):

global a

z=x+y

a=a+100

print(z)

print(a)

a=80

b=20

add(a,b)

Output:
#Local
def sub(a,b):

c=a-b

print(y,z)

print(c)

y=10

z=20

sub(y,z)
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy