1. Using file handling we can do file operations # How to open a file in python ? 1. We can open a file two ways in python 1. open() 2. With open() # OPEN 1. Using open we can open only one file at a time and we have to close a file explicity # WITH OPEN 1. Using with open we can open multiple files at a time and no need to close files explicity # File Open Modes In Python r ==> It opens a file for read only. w ==> It allows write-level access to a file. If the file already exists, then it’ll get overwritten. It’ll create a new file if the same doesn’t exist. a ==> It opens the file in append mode. a+ ==> It opens a file FOR append AND read rb ==> It opens a binary file for read 1) Read file fh = open('names.txt', 'r') ## To open a file from another path #fw = open(...
''' 1. Encapsulation is the process of wrapping up variables and methods into a single entity. In programming, a class is an example that wraps all the variables and methods defined inside it. class One(): name = 'umamahesh' def add(self): print(' ADD is ') def sub(self): print(' SUB is:') ins = One() print('\n ', ins.name) --Using encapsulation we can give protection to the class members. It can achieve Using access modifiers. Encapsulation can be achieved using Private and Protected Access Members. ''' Example---CSE department faculty cannot access the ECE department student record and so on. Here, the department acts as the class and student records act like variables and methods. =======Why Do We Need Encapsulation?=========== ===Encapsulation acts as a protective layer by ensuring that, access to wrapped data is not possible by any code define...
1. Any .py file is a module in python 2. Using module we can re use the code # How to import a module ? We can import in two ways 1. import 2. from import # Difference between import and from import 1. Using import we can import entire module and we have to use module name as prefix for each module attribute. 2. Using from import we can import required attribute from module and no need to give prefix as module name to use module attribute. 1) import statement ''' 1. we can import a module using import keyword ''' import base_module # Print a variable from base_module #print(base_module.names) ## call a add function from base_module #base_module.add() ## call a sum function from base_module #r = base_module.sum(20,50) #print(' Sum value is :', r) 2) from import statement ''' 1. Import module attributes using "from" keyword ''' ## to import only names variable #from base_module import names ## to import only nam...
Comments
Post a Comment