satya

Sunday, January 3, 2021

Program 30

                                                                        Program 30

Write a Python class to implement pow(x, n).

class Pow1:

    def poe(self,x,n):

        k=x**n

        print("pow({},{})={}".format(x,n,k))


p=Pow1()

x=int(input("Enter the X value:"))

n=int(input("Enter the Y value:"))

p.poe(x,n)



OUTPUT:

===== RESTART: C:/Users/crrcse/Desktop/29.py =======
Enter the X value:3
Enter the Y value:4
pow(3,4)=81
>>> 

Program 31

                                                                            Program 31

Write a Python class to reverse a string word by word.

class Sa:

    def display(self,s):

        words = s.split(' ')  

        reverse_sentence = ' '.join(reversed(words))  

        print("The reverse words of string:",reverse_sentence) 

  

k=Sa()

s = input("enter the string:")

k.display(s)



OUTPUT:

=== RESTART: C:/Users/crrcse/Desktop/30.py =======

enter the string:welcome to python 

The reverse words of string:  python to welcome

>>> 



                                                                       Program 32

Write a program that opens a file dialog that allows you to select a text file. The program then displays the contents of the file in a textbox.















Friday, January 1, 2021

Program 33

                                                                           Program 33
Write a program to demonstrate Try/except/else.



try: 
   a=int(input("enter the A value:"))
   b=int(input("enter the B value:"))
   s=a/b
except ZeroDivisionError:    
    print("Can't divide by zero(b=0)")
else:
    print("The division is=",s) 

OUTPUT:
1.==== RESTART: C:/Users/crrcse/Desktop/36.py =======
enter the A value:27
enter the B value:3
The division is= 9.0
>>> 
2.==== RESTART: C:/Users/crrcse/Desktop/36.py =======
enter the A value:70
enter the B value:0
Can't divide by zero(b=0)
>>> 

Program 34

                                                                    Program 34
 Write a program to demonstrate try/finally and with/as.

try: 
   a=int(input("enter the A value:"))
   b=int(input("enter the B value:"))
   s=a/b
except ZeroDivisionError:    
    print("Can't divide by zero(b=0)")
else:
    print("The divsion is=",s) 
finally: 
    print('This is always executed') 


OUTPUT:
1.=== RESTART: C:/Users/crrcse/Desktop/36.py =====
enter the A value:20
enter the B value:0
Can't divide by zero(b=0)
This is always executed
>>> 

2.===== RESTART: C:/Users/crrcse/Desktop/36.py =======
enter the A value:20
enter the B value:2
The divsion is= 10.0
This is always executed
>>> 

with/as.


file=open('satya.txt','w')
try:
    file.write('welcome to python')
finally:
    file.close()
with open('satya2.txt','w')as file:
    file.write('welcome to python')


OUTPUT:














Program 30

                                                                         Program 30 Write a Python class to implement pow(x, n). class Pow1:...