CLASS 12 : PRACTICAL FILE PROGRAMS

Question: 11

Write a python program to maintain book details like book code, book title and price using stacks data structures? (implement push(), pop() and traverse() functions)

Sol:

"""
push
pop
traverse
"""
book=[]
def push():
        bcode=input("Enter bcode  ")
        btitle=input("Enter btitle ")
        price=input("Enter price ")
        bk=(bcode,btitle,price)
        book.append(bk)
def pop():
    if(book==[]):
        print("Underflow / Book Stack in empty")
    else:
        bcode,btitle,price=book.pop()
        print("poped element is ")
        print("bcode ",bcode," btitle ",btitle," price ",price)
def traverse():
        if not (book==[]):
                n=len(book)
                for i in range(n-1,-1,-1):
                        print(book[i])
        else:
                print("Empty , No book to display")
while True:
        print("1. Push")
        print("2. Pop")
        print("3. Traversal")
        print("4. Exit")
        ch=int(input("Enter your choice "))
        if(ch==1):
                push()
        elif(ch==2):
                pop()
        elif(ch==3):
                traverse()
        elif(ch==4):
                print("End")
                break
        else:
                print("Invalid choice") 

Output:

1. Push
2. Pop
3. Traversal
4. Exit
Enter your choice 1
Enter bcode 101
Enter btitle python
Enter price 254
1. Push
2. Pop
3. Traversal
4. Exit
Enter your choice 3
(‘101’, ‘python’, ‘254’)
1. Push
2. Pop
3. Traversal
4. Exit
Enter your choice

Question:12

Write a python program to maintain employee details like empno,name and salary using Queues data structure? (implement insert(), delete() and traverse() functions)

Sol:

#queue implementation (using functions)
#program to create a queue of employee(empno,name,sal).
"""
add employee
delete employee
traverse / display all employees
"""
employee=[]
def add_element():
        empno=input("Enter empno  ")
        name=input("Enter name ")
        sal=input("Enter sal ")
        emp=(empno,name,sal)
        employee.append(emp)
def del_element():
        if(employee==[]):
                print("Underflow / Employee Stack in empty")
        else:
                empno,name,sal=employee.pop(0)
                print("poped element is ")
                print("empno ",empno," name ",name," salary ",sal)
def traverse():
        if not (employee==[]):
                n=len(employee)
                for i in range(0,n):
                        print(employee[i])
        else:
                print("Empty , No employee to display")
while True:
        print("1. Add employee");
        print("2. Delete employee");
        print("3. Traversal")
        print("4. Exit")
        ch=int(input("Enter your choice "))
        if(ch==1):
                add_element()
        elif(ch==2):
                del_element();
        elif(ch==3):
                traverse()
        elif(ch==4):
                print("End")
                break
        else:
                print("Invalid choice") 

Output:

1. Add employee
2. Delete employee
3. Traversal
4. Exit
Enter your choice 1
Enter empno 101
Enter name Amit
Enter sal 45000
1. Add employee
2. Delete employee
3. Traversal
4. Exit
Enter your choice 3
(‘101’, ‘Amit’, ‘45000’)
1. Add employee
2. Delete employee
3. Traversal
4. Exit
Enter your choice

Question:13

Write a python program to read a file named “article.txt”, count and print total alphabets in the file?

Sol:

def count_alpha():
    lo=0
    with open("story.txt") as f:
        while True:
            c=f.read(1)
            if not c:
                break
            print(c,end='')
            if((c>='A' and c<='Z') or (c>='a' and c<='z')):
                lo=lo+1
        print("total lower case alphabets ",lo)
#function calling
count_alpha() 

Output:

Hello how are you
12123
bye
total lower case alphabets 17
>>>

Question:14

Write a python program to read a file named “article.txt”, count and print the following:
(i). length of the file(total characters in file)
(ii).total alphabets
(iii). total upper case alphabets
(iv). total lower case alphabets
(v). total digits
(vi). total spaces
(vii). total special characters

Sol:

def count():
    a=0
    ua=0
    la=0
    d=0
    sp=0
    spl=0
    with open("story.txt") as f:
        while True:
            c=f.read(1)
            if not c:
                break
            print(c,end='')
            if((c>='A' and c<='Z') or (c>='a' and c<='z')):
                 a=a+1
                 if((c>='A' and c<='Z') or (c>='a' and c<='z')):
                        a=a+1
                        if(c>='A' and c<='Z'):
                            ua=ua+1
                        else:
                            la=la+1  
            elif(c>='0' and c<='9'):
                 d=d+1
            elif(c==' '):
                 sp=sp+1
            else:
                 spl=spl+1
         print("total alphabets ",a)
         print("total upper case alphabets ",ua)
         print("total lower case alphabets ",la)
         print("total digits ",d)
         print("total spaces ",sp)
         print("total special characters ",spl)
# function calling
count() 

Output:

Question: 15

 

Write a python program to read a file named “story.txt”, count and print total words starting with “a” or “A” in the file?

Sol:

def count_words():
    w=0
    with open("story.txt") as f:
        for line in f:
            for word  in line.split():
                if(word[0]=="a" or word[0]=="A"):
                    print(word)
                    w=w+1
        print("total words starting with 'a' are ",w)
# function calling
count_words() 

Output:

are
Ankur
Amit
Aman
total words starting with ‘a’ are 4
>>>

CBSE Class 12 @ Python

  • Home Page class 12 @ Python
  • Class 12 @ Python Theory Syllabus
  • Class 12 @ Python Practical Syllabus
  • Revision Tour
  • Functions (Funcations, Inbuilt Functions, Python Modules, Python Packages)
  • Inbuilt Functions
  • Python Modules
  • Python Packages
  • Using Python Libraries
  • Python Data File Handling
  • Program Efficiency
  • Data Structures In Python
  • Data Visualization Using Pyplot
  • Computer Networks
  • MySQL
  • Interface Python with SQL
  • Society, Law and Ethics
  • Web Development with Django
  • Class 12 @ Python Sample Practical File
  • Class 12 @ Python Sample Papers
  • Class 12 @ Python Projects
  • Tutorials

    Technical Questions

    Interview Questions

    C Programming
    C++ Programming
    Class 11 (Python)
    Class 12 (Python)
    C Language
    C++ Programming
    Python

    C Interview Questions
    C++ Interview Questions
    C Programs
    C++ Programs