Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

11/16/21, 6:42 PM Untitled0.

ipynb - Colaboratory

Exception handling

try:

    a=int(input("Enter any number:"))

    b=int(input("Enter any number:"))

    c=a/b

    print(c)

except ZeroDivisionError:

    print("Cannot divide by 0")

Enter any number:1

Enter any number:0

Cannot divide by 0

Q1

def func1():

    fname=input("Enter File Name")

    file=open(fname,"r")

    while True:

        s=file.readline()

        if s=="":

            break

        print(s,end="#")

    file.close()    

        

def func2():

    fname=input("Enter File Name")

    file=open(fname,"r")

    c1=c2=c3=c4=0

    d=file.read()

    for s in d:   

        if s=="":

            break

        if s in 'AEIOUaeiou':

            c1+=1

        elif s not in 'AEIOUaeiou':

            c2+=1        

        if s.isupper():

            c3+=1

        if s.islower():

            c4+=1

    

    file.close()

    

    print("Number of Vowels are:",c1)

https://colab.research.google.com/drive/1WGp1LjOIIY1lfP5E4dLAaZntujl-3-e6#scrollTo=_h4doI5p07MI&printMode=true 1/10
11/16/21, 6:42 PM Untitled0.ipynb - Colaboratory

    print("Number of Consonants are:",c2)

    print("Number of Uppercase charecters are:",c3)

    print("Number of Lowercase charecters are:",c4)

def func3():

    fname=input("Enter File Name")

    file=open(fname,"r")

    c=ct=0

    while True :

        s=file.readline()

        if s=="":

            break

        if s[0] in 'Tt'and s[1] in 'Hh'and s[2] in 'Ee'and s[3]==' ':

            c+=1

            print(s)

    

    ct+=1    

        

    print("No. of lines begining with 'The' are",c)

    print("Total No. of lines are:",ct)

    

    file.close()

    

while True:

    print('''Menu

1.Display each word by #

2.Displayy No. of Vowels,consonants,Uppercase and Lowercase Charecters

3.Dusplay No. of lines Begining with 'The' and Total Lines

4. Exit''')

    c=int(input("Enter the number to get the desired option:"))

    if c==1:

        func1()

    elif c==2:

        func2()

    elif c==3:

        

        func3()

    elif c==4:

        break

    else:

        print("Wrong Input")

        

Menu

1.Display each word by #

2.Displayy No. of Vowels,consonants,Uppercase and Lowercase Charecters

3.Dusplay No. of lines Begining with 'The' and Total Lines

4. Exit

Enter the number to get the desired option:1

Enter File Name


https://colab.research.google.com/drive/1WGp1LjOIIY1lfP5E4dLAaZntujl-3-e6#scrollTo=_h4doI5p07MI&printMode=true 2/10
11/16/21, 6:42 PM Untitled0.ipynb - Colaboratory

Q2

def func1():

    fname1=input("Enter Source File Name")

    fname2=input("Enter Target File Name")

    file1=open(fname1,"r")

    file2=open(fname2,"w")

    while True:

        s=file1.readline()

        if s=="":

            break

        if s[0] in'Aa':

            file2.write(s)

            file2.write("\n")

    file1.close()

    file2.close()

    file3=open(fname2,"r")

    d=file3.read()

    print(d)

    file3.close()

def func2():

     file1=open("Address.txt","r")

     file2=open("NAdd.txt","w")

     while True:

        s=file1.readline()

        if s=="":

            break

        if 'Delhi'in s:
                s=s.replace('Delhi','New delhi')

                file2.write(s)

                file2.write('\n')

                print(s) 

     file1.close()

     file2.close()           

        

while True:             

    print('''Menu

1.Copying all lines containing 'a' in another file and dispplay another file

2.To read the comntents of file and replace all words 'delhi' with 'New Delhi'

3. Exit

''')

    c=int(input("Enter the number to get the desired option:"))

    if c==1:

        func1()

    elif c==2:

        func2()

    elif c==3:

https://colab.research.google.com/drive/1WGp1LjOIIY1lfP5E4dLAaZntujl-3-e6#scrollTo=_h4doI5p07MI&printMode=true 3/10
11/16/21, 6:42 PM Untitled0.ipynb - Colaboratory

        break

    else:

        print("Wrong Input")            

               

             

Q3

def f():

    rec=""

    file=open("Note.txt","w")

    file.write("States\t\tCapital\n")

    n=int(input("Enter the number of states:"))

    for i in range(n):

        z=input("Enter the name of state:")

        k=input("Enter the capital:")

        rec =z+"\t\t"+k+"\n"

        file.write(rec)

    file.close()

def f1():

    file=open("Note.txt","r")

    data=file.read()

    print(data)

    file.close()

    

def f2():

    with open("Note.txt","r") as file:

        with open("UNote.txt","w") as file1:

            for line in file.readline():

                line=line.upper()
                file1.write(line)
    file.close()

    file1.close()

def f3():

    file=open("UNote.txt","r")

    data=file.read()

    file.close()

    print("Contents of UNote:",data)

    file=open("Note.txt","r")

    data=file.read()

    file.close()

    print("Contents of Note:",data)

    

while True:

https://colab.research.google.com/drive/1WGp1LjOIIY1lfP5E4dLAaZntujl-3-e6#scrollTo=_h4doI5p07MI&printMode=true 4/10
11/16/21, 6:42 PM Untitled0.ipynb - Colaboratory

    print("1.Create a text file Note.txt with some states and capitals")

    print("2.Display the contents")

    print("3.Create another text file UNote.txt and store all states and capitals in uppercas
    print("4.Display contents of UNote.txt and Note.txt")

    print("5.Exit")

    print()

    c=int(input("Enter the desired menu option:"))

    if c==1:

        f()

    elif c==2:

        f1()

    elif c==3:

        f2()

        

    elif c==4:

        f3()

    elif c==5:

        break

    else:

        print("Wrong input")

Q4

def f():

    file=open("HISTORY.txt","w")

    lines=["Mahatma Gandhi is known as Father of India\n","He was born in Gujarat.\n","He was 
    file.writelines(lines)

    file.close()

def f1():

    file=open("HISTORY.txt","r")

    data=file.read()

    print(data)

    file.close()

def f2():

    with open("HISTORY.txt","r") as file:

        with open("HISTORY2.txt","w") as file1:

            for line in file:

                line=line.lower()
                file1.write(line)
    file.close()

    file1.close()

def f3():

    file=open("HISTORY2.txt","r")

    data=file.read()

https://colab.research.google.com/drive/1WGp1LjOIIY1lfP5E4dLAaZntujl-3-e6#scrollTo=_h4doI5p07MI&printMode=true 5/10
11/16/21, 6:42 PM Untitled0.ipynb - Colaboratory

    print(data)

    file.close()

while True:

    print("1.Create a text file with some info about Mahatma Gandhi")

    print("2.Display the contents")

    print("3.Create another text file containing the information of each line in lower case")

    print("4.Display contents of new file")

    print("5.Exit")

    print()

    c=int(input("Enter the desired menu option:"))

    if c==1:

        f()

    elif c==2:

        f1()

    elif c==3:

        f2()

    elif c==4:

        f3()

    elif c==5:

        break

    else:

        print("Wrong Input")

Q5

file=open("Memo.txt","w")

lines=["do DO this is a\n","the are is do do Do\n"]

file.writelines(lines)

file.close()

def countDo():

    k=0

    file=open("Memo.txt","r")

    data=file.read()

    for i in data:

        if i in "do" and i in "Do":

            k=k+1

    file.close()

    print("Number of 'do' and 'Do' present in file is equal to:",k)

def count_vowel():

    v=0

    file=open("Memo.txt","r")

    data=file.read()

    for i in data:

https://colab.research.google.com/drive/1WGp1LjOIIY1lfP5E4dLAaZntujl-3-e6#scrollTo=_h4doI5p07MI&printMode=true 6/10
11/16/21, 6:42 PM Untitled0.ipynb - Colaboratory

        if i in "AEIOUaeiou":

            v=v+1

    file.close()

    print("Number of vowels present in file is equal to:",v)

    

while True:

    print("1. countDo")
    print("2. count_vowel")

    print("3. Exit")

    c=int(input("Enter the desired menu option:"))

    if c==1:

        countDo()

    elif c==2:

        count_vowel()

    elif c==3:

        break

    else:

        print("Wrong Input")

Q6

file=open("Magic.txt","w")
lines=["The file contains various information entered by user\n","This is a text file\n"]
file.writelines(lines)
file.close()

def f():
    a=0
    d=0
    s=0
    file=open("Magic.txt","r")
    data=file.read()
    for i in data:
        if i.isalpha():
            a=a+1
        elif i.isdigit():
            d=d+1
        elif i.isspace():
            s=s+1
    file.close()
    print("Number of alphabets",a)
    print("Number of digits",d)
    print("Number of spaces",s)        

def f2():
    l=0
    file=open("Magic.txt","r")
data=file readlines()
https://colab.research.google.com/drive/1WGp1LjOIIY1lfP5E4dLAaZntujl-3-e6#scrollTo=_h4doI5p07MI&printMode=true 7/10
11/16/21, 6:42 PM Untitled0.ipynb - Colaboratory
    data=file.readlines()
    print("Number of lines:",len(data))
    file.close()

def f3():
    file=open("Magic.txt","r")
    c=0
    while True:
        s=file.readline()
        if s=="":
            break
        if s[0] in "Tt" and s[1] in "Hh" and s[2] in "Ee" and s[3] in " ":
            c=c+1
    file.close()
    print("Number of lines beginning with 'The':",c)

def f4():
    file=open("Magic.txt","r")
    

while True:
    print("1.Create a text file and count and display number of alphabets, digits and spaces"
    print("2.Print Number of lines")
    print("3.Count and display line beginning with 'The'")
    print("4.Count words ending with vowel")
    print("5.Exit")
    print()

    c=int(input("Enter the desired menu option:"))

    if c==1:
        f()
    elif c==2:
        f2()
    elif c==3:
        f3()
    elif c==4:
        
        
    elif c==5:
        break

Q7

import pickle
def create():
    file=open("Bank.dat","wb")
  
    n=int(input("Enter the number;"))
for i in range(n):
https://colab.research.google.com/drive/1WGp1LjOIIY1lfP5E4dLAaZntujl-3-e6#scrollTo=_h4doI5p07MI&printMode=true 8/10
11/16/21, 6:42 PM Untitled0.ipynb - Colaboratory
    for i in range(n):
        l=[]
        accno=int(input("Enter the account number:"))
        acc_name=input("Enter the account holder's name:")
        accbal=int(input("Enter account balance:"))
        l.append(accno)
        l.append(acc_name)
        l.append(accbal)
        pickle.dump(l,file)
        

def reading():
    file=open("Bank.dat","rb")
    try:
        while True:
            l=pickle.load(file)
            print(l)
    except EOFError:
        print()
        file.close()

create()
reading()
        

Enter the number;1

Enter the account number:12345678


Enter the account holder's name:Garv Kukkal

Enter account balance:1000000

[12345678, 'Garv Kukkal', 1000000]

https://colab.research.google.com/drive/1WGp1LjOIIY1lfP5E4dLAaZntujl-3-e6#scrollTo=_h4doI5p07MI&printMode=true 9/10
11/16/21, 6:42 PM Untitled0.ipynb - Colaboratory

arrow_right Executing (9m 33s)  Cellnavigate_nextfunc1()navigate_nextraw_input()navigate_next_input_request()navigate_nextrecv()navigate_nextrecv_multipart()

https://colab.research.google.com/drive/1WGp1LjOIIY1lfP5E4dLAaZntujl-3-e6#scrollTo=_h4doI5p07MI&printMode=true 10/10

You might also like