You are on page 1of 53

Python

Object Oriented Programming


Introduction to OOPs in Python
Python is a multi-paradigm programming language. Meaning, it supports different
programming approach.

One of the popular approach to solve a programming problem is by creating objects. This is
known as Object-Oriented Programming (OOP).

An object has two characteristics:


attributes
behavior

Let's take an example:


Parrot is an object,

name, age, color are attributes

singing, dancing are behavior


Python
Object Oriented Programming
Class
A class is a blueprint for the object.
We can think of class as an sketch of a parrot with labels.
It contains all the details about the name, colors, size etc. Based on these descriptions, we can
study about the parrot. Here, parrot is an object.

The example for class of parrot can be :


class Parrot:
’’’ __Docstring__’’’
pass

Here, we use class keyword to define an empty class Parrot.


Note:If you want to print documentation string on output screen the use command
print(class name.__doc__)
Doc string printing
Class
Example Class:
Python
Object Oriented Programming

What is pass statement in Python?


In Python programming, pass is a null statement. The difference between
a comment and pass statement in Python is that, while the interpreter ignores a comment
entirely, pass is not ignored.
However, nothing happens when pass is executed. It results into no operation (NOP).
Syntax of pass
pass
Example: pass Statement
# pass is just a placeholder for
# functionality to be added
S = ['p', 'a', 's', 's‘]
for val in S:
pass
Object
An object (instance) is an instantiation of a class. When
class is defined, only the description for the object is
defined. Therefore, no memory or storage is allocated.

The example for object of parrot class can be:


obj = Parrot()

Here, obj is object of class Parrot.


Class creation
Constructor method
Example
Inheritance
Overloading
FILE HANDLING

What is a file?
File is a named location on disk to store related information. It is used to permanently
store data in a non-volatile memory (e.g. hard disk).

Since, random access memory (RAM) is volatile which loses its data when computer
is turned off, we use files for future use of the data.

When we want to read from or write to a file we need to open it first. When we are
done, it needs to be closed, so that resources that are tied with the file are freed.

Hence, in Python, a file operation takes place in the following order.


•Open a file
•Read or write (perform operation)
•Close the file
How to open a file?

Python has a built-in function open() to open a file. This function returns a file object,
also called a handle, as it is used to read or modify the file accordingly.

>>> f = open("test.txt") # open file in current directory


>>> f = open("C:/Python33/README.txt") # specifying full path

We can specify the mode while opening a file. In mode, we specify whether we want to

read 'r', write 'w' or append 'a' to the file. We also specify if we want to open the file in

text mode or binary mode.


Text Mode
The default is reading in text mode. In this mode, we get strings when reading
from the file.

Binary Mode
On the other hand, binary mode returns bytes and this is the mode to be used
when dealing with non-text files like image or exe files.
Mode Description

'r' Open a file for reading. (default)

Open a file for writing. Creates a new


file if it does not exist or truncates the
'w' file if it exists, if a file exists with the
same filename it will be erased.
Open a file for exclusive creation. If
'x' the file already exists, the operation
fails.
Open for appending at the end of the
'a' file without truncating it. Creates a
new file if it does not exist.
't' Open in text mode. (default)
'b' Open in binary mode.
Open a file for updating (reading and
'+' writing)
file.read(n bytes)

This methods reads n bytes from file. example:1 byte ,2 bye,etc…

file.read()
This method reads entire file to memory

file.readline()
This method reads one line from the file.

file.readlines()
This methods reads all the lines from the file into list.

file.write(string)
This method writes the string in to file.

file.close()

This methods closes connection to the file, and frees all connected recourses

You might also like