Semester I BCA Syllabus BCA101 T – Programming Principles Using Python
Semester I Paper BCA101 T Unit V – Object Oriented Programming:
- Introduction to Object
- Class and Method
- Standard Libraries
- File Handling through Libraries
- Reading from a File
- Writing to a File
Introduction to Object:
Object-oriented programming is a programming paradigm that uses objects, which are instances of classes, to structure and organize code. In Python, everything is an object, and OOP plays a significant role in designing and implementing programs. Let’s start by understanding the fundamental concepts of OOP in Python.
Class and Method:
- Class –
- Definition: In Python, a class is a blueprint or a template for creating objects. It details the elements and properties of objects that fall within that class.
- Purpose: Classes are used to represent real-world entities, abstract data types, or concepts in your code. They encapsulate data (attributes) and behavior (methods) related to these entities.
- Syntax: To create a class in Python, you use the
class
keyword followed by the class name, and you can define attributes and methods within the class.
Example:
class Person:
# Attributes
def init(my, name, age):
self.name = name
self.age = age
# Methods
def greet(my):
print(f"Hello, my name is {my.name} and I am {my.age} years old.")
In this example, Person is a class that has attributes (name and age) and a method (greet()).
2. Method –
- Definition: A method is a function defined inside a class. Methods represent the behavior or actions that objects of the class can perform.
- Purpose: Methods are used to manipulate and interact with the attributes of objects. They encapsulate the logic associated with a class.
- Syntax: Methods are defined like regular functions but are indented within the class. The first parameter of a method is typically
self
, which represents the instance of the class (the object itself).
Example:
class Rectangle:
# Attributes
def init(self, length, width):
self.length = length
self.width = width
# Method to calculate area
def calculate_area(self):
return self.length * self.width
In this example, calculate_area() is a method of the Rectangle class that calculates the area based on the length and width attributes.
To use a class and its methods, you have to create objects (instances) of that class. It can be done like this:
# Creating objects of the 'Person' class
person1 = Person("Jenny", 28)
person2 = Person("David", 35)
# Calling methods on objects
person1.greet() # Output: Hello, my name is Jenny and I am 28 years old.
person2.greet() # Output: Hello, my name is David and I am 35 years old.
# Creating objects of the 'Rectangle' class
rect1 = Rectangle(5, 3)
rect2 = Rectangle(4, 4)
# Calling methods on objects
area1 = rect1.calculate_area() # area1 = 15
area2 = rect2.calculate_area() # area2 = 16
Classes define the structure of objects, and methods define their behavior. By using classes and methods, you can model and work with complex data and logic in a modular and organized way, which is a key aspect of Object-Oriented Programming in Python.
Standard Libraries:
Python comes with a rich set of standard libraries that provide ready-to-use modules and functions for various tasks. These libraries simplify common programming tasks, such as working with data, networking, and more.
Example
Using the ‘math’ module from the standard library
import math
print(math.sqrt(25)) # Output: 5.0 (square root of 25)
print(math.pi) # Output: 3.141592653589793 (value of pi)
File Handling through Libraries:
Python provides built-in libraries for file handling, making it easy to read from and write to files.
- Reading from a File: You can use the
open()
function to open a file and read its contents.
Example
Reading from a file
with open(“example.txt”, “r”) as file:
content = file.read()
print(content)
2. Writing to a File:
You can use the open()
function with the "w"
mode to write data to a file.
Example:
Writing to a file
with open(“output.txt”, “w”) as file:
file.write(“Hello, World!”)
The above explanation of Unit V provides a foundational understanding of Object-Oriented Programming and file handling in Python. To excel in your BCA degree program at Avadh University, practice these concepts and explore more advanced topics as you progress in your studies.