cs

  1

Write a Python program to make user choice to print area of different figure

def Square():

    number=int(input("Enter the side:"))

    area=number*number

    print("Area of Square:",area)

def Rectangle():

    l=int(input("Enter the Length: "))

    b=int(input("Enter the Breadth: "))

    area=l*b

    print("Area of Rectangle:" ,area)

def Triangle():

    b=int(input("enter the base of triangle:"))

    h=int(input("enter the height of the triangle:"))

    area=b*h*.5

    print("Area of triangle :",area)

print("Enter the choice 1 for Rectangle 2 for square and 3 for Triangle")

n=int(input("enter the choice:"))

if n==1:

    Rectangle()

elif n==2:

    Square()

else:

    Triangle()


2

Write a Python program to read a text file and display the number of vowels/consonants/uppercase/lowercase characters in the file

file=open("AI.TXT","r")

content=file.read()

vowels=0

consonants=0

lower_case_letters=0

upper_case_letters=0

for ch in content:

    if(ch.islower()):

        lower_case_letters+=1

    elif(ch.isupper()):

        upper_case_letters+=1

        ch=ch.lower()

    if (ch in ['a','e','i','o','u']):

        vowels+=1

    elif(ch in ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']):

        consonants+=1

file.close()

print("Vowels are :",vowels)

print("Consonants :",consonants)

print("Lower_case_letters :",lower_case_letters)

print("Upper_case_letters :",upper_case_letters)



3


def push():

    a=int(input("Enter the element to be added:"))

    stack.append(a)

    return a

def pop():

    if stack==[]:

        print("Stack Underflow!")

    else:

        print("Deleted element is",stack.pop())

def display():

    if stack==[]:

        print("Stack is Empty")

    else:

        for i in range(len(stack)-1,-1,-1):

            print(stack[i])

stack=[]

print("STACK OPERATIONS")

choice='y'

while choice=='y':

    print("Enter 1 for PUSH 2 for POP 3 for DISPLAY other key for exit")

    c=int(input("Enter your choice:"))

    if c==1:

        push()

    elif c==2:

        pop()

    elif c==3:

        display()

    else:

        break


4


import pickle

stud_data={}

list_of_students=[]

no_of_students=int(input("Enter no of Students:"))

for i in range(no_of_students):

    stud_data["roll_no"]=int(input("Enter roll no:"))

    stud_data["name"]=input("Enter name: ")

    list_of_students.append(stud_data)

    stud_data={}

file=open("StudDtl.dat","wb")

pickle.dump(list_of_students,file)

print("Data added successfully")

file.close()


Comments