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:














Tuesday, December 22, 2020

Program 29

                                                                            Program 28 

 Write a class called Time whose only field is a time in seconds. It should have a method called convert_to_minutes that returns a string of minutes and seconds formatted as in the following example: if seconds is 230, the method should return '5:50'. It should also have a method called convert_to_hours that returns a string of hours, minutes, and seconds formatted analogously to the previous method.

class Time:
               def __init__(self,sec):
                              self.sec=sec
               def convertsectomin(self):
                              n=self.sec
                              minutes=n//60
                              seconds=n%60
                              return(str(minutes)+":"+str(seconds))
               def convertsectohrs(self):
                              n=self.sec
                              hours=n//3600
                              minutes=(n//60)%60
                              seconds=n%60
                               return(str(hours)+":"+str(minutes)+":"+str(seconds))
  
sec = int(input ('Enter the number of seconds:'))
c=Time(sec)
print("Time in minutes :seconds formatted is=",c.convertsectomin())
print("Time in houres :minutes:seconds formatted is=",c.convertsectohrs())



OUTPUT:
========== RESTART: C:/Users/satyanarayana/Desktop/p28.py ==========
Enter the number of seconds:560
Time in minutes :seconds formatted is= 9:20
Time in houres :minutes:seconds formatted is= 0:9:20


========= RESTART: C:/Users/satyanarayana/Desktop/p28.py ===========
Time in minutes :seconds formatted is= 76:7
Time in houres :minutes:seconds formatted is= 1:16:7
>>> 


Friday, October 23, 2020

Program 28

                                                                         Program 28

Write a class called Time whose only field is a time in seconds. It should have a method called convert_to_minutes that returns a string of minutes and seconds formatted as in the following example: if seconds is 230, the method should return '5:50'. It should also have a method called convert_to_hours that returns a string of hours, minutes, and seconds formatted analogously to the previous method.

class Time:

    def __init__(self,sec):

        self.sec=sec

    def seconds_to_Minutes(self):

            s=self.sec

            minutes = s // 60

            seconds=s%60

            print('Times in Minutes:seconds format',str(minutes)+":",str(seconds))

    def seconds_to_Hours(self):

            s=self.sec

            hours = s // 3600

            minutes=(s//60)%60

            seconds=s%60

            print('Times in Hours:Minutes:seconds format',str(hours)+":",str(minutes)+":",str(seconds))

 

sec=int(input('Enter the number of seconds:'))

obj=Time(sec) 

obj.seconds_to_Minutes()

obj.seconds_to_Hours()


OUTPUT:

======RESTART: D:/old/r19 lab-python/28.py =============

Enter the number of seconds:150

Times in Minutes:seconds format 2: 30

Times in Hours:Minutes:seconds format 0: 2: 30

>>> 

======RESTART: D:/old/r19 lab-python/28.py =============

Enter the number of seconds:4500

Times in Minutes:seconds format 75: 0

Times in Hours:Minutes:seconds format 1: 15: 0

>>> 

Program 30

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