Introduction To Python Part 6

Jreyes
2 min readDec 5, 2021

--

Continuing where we left off.

OOPs Concepts

Python can be used as an object-oriented programming language. Hence, we can use the following concepts in python:

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

Abstraction

Data abstraction refers to displaying only the necessary details and hiding the background tasks. Abstraction in python is similar to any other programming language.

Like when we print a statement, we don’t know what is happening in the background.

Encapsulation

Encapsulation is the process of wrapping up data. In python, classes can be an example of encapsulation where the member functions and variables, etc are wrapped into a class.

Inheritance

Inheritance is an object-oriented concept where a child class inherits all the properties from a parent class. Following are the types of inheritance we have in python:

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance

Single Inheritance

In single inheritance, there is only one child class that inherits the properties from a parent class.

class parent:def printname(name):print(name)class child(parent):passob1 = child('eduardo')ob1.printname

Multiple Inheritance

In multiple inheritances, we have two parent classes and one child class that inherits the properties from both the parent classes.

Multilevel Inheritance

In multilevel inheritance, we have one child class that inherits properties from a parent class. The same child class acts as a parent class for another child class.

Polymorphism

Polymorphism is the process in which an object can be used in many forms. The most common example would be when a parent class reference is used to refer to a child class object.

Exceptional Handling

When we are writing a program if an error occurs the program will stop. But we can handle these errors/exceptions using the try, except, finally blocks in python.

When the error occurs, the program will not stop and execute the except block.

try:print(x)except:print('exception')

Finally

When we specify a final block. It will be executed even if there is an error or not raised by the try-except block.

try :print(x)except:print('exception')finally:print('I will be executed anyway')

Now that we have understood exception handling concepts. Let's take a look at file handling concepts in python.

File Handling

File handling is an important concept of the python programming language. Python has various functions to create, read, write, delete or update a file.

Creating a File

import osf = open('file location')

Reading a File

f = open('file location', 'r')print(f.read())f.close()

Append a File

f = open('filelocation','a')f.write('the content')f.close()f = open('filelocation','w')f.write('this will overwrite the file')f.close()

Delete a file

import osos.remove('file location')

These are all the functions we can perform with file handling in python. Hope this series will help someone and this is it for the introduction to Python. Thank you so much and happy coding.

--

--

Jreyes
Jreyes

Written by Jreyes

I'm a software engineer making his way into the tech industry.

No responses yet