CBSE class 12 Computer science Python practicals program|Stacks class 12 computer science programs|Data structure python programs


Introduction

In this blog you will find three major CS class 12 practical programs. Other programs will also be uploaded soon.

1) Read a text file and display the number of uppercase/lowercase characters in the file.

2) Python program to read a text file and display the number of vowels/consonants in the file.

3) Python program to implement a stack data structure.

Source code(for 1st program)-

f=open( "path of file",'r')
C_upper=0

C_lower=0

ch=f.read()

for i in ch:

    if i.isupper():

        C_upper+=1

    if i.islower():

        C_lower+=1

print("Number of Uppercase alphabets is/are : ",C_upper,'\n',"and number of lowercase alphabets are:",C_lower)



#-------------code ended-----------

About the Code-

  • You need to paste the path of your file in line 1 as instructed. Otherwise it may give an error.
  • Read():- The read() function in Python is a pre-defined function that returns the read data as a string.
  • isupper():- This is a built-in method used for string handling. This method returns true if all characters in the string are uppercase, otherwise, "false".
  • islower() :- This is a built-in method which is used for string handling. The islower() method returns true if all characters in the string are lowercase, otherwise returns "false".
  • The operators used in the program are:
  • f.read()
  • for loop
  • i.isupper()
  • islower()
  • print()

Source code(for 2nd program)-


#you have to paste the path of file in the given space before executing it 


file=open('path of file',"r")
vowels = set("AEIOUaeiou")

constant=set("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ")

text=file.read()

countV=0

countC=0

for V in text:

    if V in vowels:

        countV +=1

    if V in constant:

        countC+=1

print("The number of Vowels is:",countV,"\nThe number of consonants is: ",countC)


#-------------code ended-----------
  • You need to paste the path of your file in line 1 as instructed. Otherwise it may give an error.


Source code(for 3rd program)-

stack=[]
choice='yes'
while choice=='yes':
    print('1.PUSH')
    print('2.POP')
    print('3.Display elements of stack')
    choice1=int(input("what do you want to perform?:"))
    if choice1==1:
        a=int(input("Enter the element which you want to push : "))
        stack.append(a)
    elif choice1==2:
        if stack==[]:
            print("Stack is empty..... Underflow")
        else:
            print("deleted element is :",stack.pop())
    elif choice1==3:
        for i in range(len(stack)-1,-1,-1):
            print(stack[i])
    else :
        print("invalid input")
    choice=input("want perform more functions?Yes or no")
    if choice=='no':
        break
#code ended

What are Stacks?

Stack is a linear data structure that follows a particular sequence of LIFO (Last In First Out) or FILO (First In Last Out). We can perform many functions in this data structure.
Some real life examples are :- Stack of plates, stack of books one by one.


To learn more about Stacks and Another program based on list data structure (stack and queue):--https://learntechpython.blogspot.com/2021/07/write-python-program-to-implement-stack.html


"Hope it helped"
Have anything in mind? Let me 
know in the comment section👇

Next Post Previous Post
No Comment
Add Comment
comment url