''' 1. Using sys module we can do system related operation. ''' import sys print(sys.version) ## to append path #sys.path.append(r'C:\\Users\\sriram\\Desktop\\akr') ## To exit from execution print(' \n Before exit') sys.exit() print(' \n After exit') -------------Input and Output using sys The sys modules provide variables for better control over input or output. We can even redirect the input and output to other devices. This can be done using three variables – 1.stdin 2.stdout 3.stderr ----stdin---: It can be used to get input from the command line directly. It used is for standard input. It internally calls the input() method. It, also, automatically adds ‘\n’ after each sentence. import sys for line in sys.stdin: if 'q' == line.rstrip(): break print(f'Input : {line}') print("Exit") --------stdout-------: A built-in file object that is to the interpreter’s standard out...
## Using __init__ we can create constructor ## Using constructor we can create instance varables. ## Constructor method will call when calling class class One: def __init__(self, name, eid): self.name = name self.eid = eid print('__init__') def add(self): print(' Add ', self.name) def sub(self): print(' SUB ', self.name) print(' SUB EID ', self.eid) inst = One('UMAMAHESH', 999) #inst.add() #inst.sub()
1. An exception is an event, which occurs during the execution of a program. 2. Using try,except,else and finally block we can handle exception. 3. Using exceptions we can handle run time errors. # else 1. else block will execute when the try block executed successfully with out any errors. # Finally *** 1. finally block will execute both times when try block executed successfully or fail. 2. It is used to do the cleanup process. 1) try and except import sys d = {'A':1,'B':2} try: d['C'] 'we' + '123' sys.ok() import re open('2_else.py') except ModuleNotFoundError: print(' Module is not defined ') except NameError: print(' name is not defined please define ') except KeyError: print(' KeyError ') except FileNotFoundError as e: print(' Please define file ', e) except: print(' Try block got failed ') 2)else block #''' #1. else block will ex...
Comments
Post a Comment