0% found this document useful (0 votes)
59 views

Python Ke Farre PDF

Object-oriented programming (OOP) uses classes and objects to structure code. Key OOP concepts include classes that define attributes and methods, objects that are instances of classes, constructors that initialize objects, and polymorphism that allows objects to be treated as their parent class. Python supports OOP through classes, inheritance that allows classes to inherit attributes and methods from parent classes, and data abstraction that hides complex details.

Uploaded by

moviesvala2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Python Ke Farre PDF

Object-oriented programming (OOP) uses classes and objects to structure code. Key OOP concepts include classes that define attributes and methods, objects that are instances of classes, constructors that initialize objects, and polymorphism that allows objects to be treated as their parent class. Python supports OOP through classes, inheritance that allows classes to inherit attributes and methods from parent classes, and data abstraction that hides complex details.

Uploaded by

moviesvala2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Lists: Object-Oriented Programming (OOP):

Lists are ordered and mutable sequences in Python. OOP is a programming paradigm that uses objects and classes for structuring
You can store a collection of items in a list, and each item can be of any data code. Python is an object-oriented programming language, and it supports the
type. Example: my_list = [1, 2, 3, 'apple', 'banana'] following concepts:
Tuples:
Tuples are ordered and immutable sequences in Python. Class and Object:
Similar to lists, but once you create a tuple, you cannot modify its contents
(immutable). Example: my_tuple = (1, 2, 3, 'apple', 'banana') Class: A blueprint or a template for creating objects. It defines attributes and
List vs. Tuple: methods that the objects will have.
Main difference: Lists are mutable (can be modified), while tuples are
immutable (cannot be modified). Use lists when you need a collection that class Dog:
might change, and tuples when you want to ensure the data remains constant. def __init__(self, name, age):
Functions: self.name = name
Functions are blocks of reusable code that perform a specific task. self.age = age
They help in modularizing code, making it more readable and maintainable. Object: An instance of a class. It is a concrete realization of the class, with
Functions can take parameters (inputs), perform a set of actions, and return a specific values.
result. Example: python
my_dog = Dog("Buddy", 3)
def add_numbers(a, b):
return a + b 2. Constructor:
Basic Python Function Concepts: A constructor is a special method in a class that is automatically called when
Defining a function: Use the def keyword, followed by the function name and an object is created.
parameters. Calling a function: Use the function name followed by parentheses
and any required arguments. Non-Parameterized Constructor:
Parameters and arguments: Parameters are variables in a function definition;
arguments are values passed to the function when it's called. class MyClass:
Return statement: Specifies the value a function should return. def __init__(self):
Scope: Variables defined inside a function are typically local to that function. # constructor code
Parameterized Constructor:
File Handling in Python:
File handling is an essential part of any programming language to read from
class MyClass:
and write to files. Python provides built-in functions and methods for handling
def __init__(self, parameter1, parameter2):
various file operations.
self.parameter1 = parameter1
Types of Files in Python:
self.parameter2 = parameter2
Text Files: Contains plain text characters. Commonly used for storing
information like configuration settings or simple data.
3. Destructor:
Binary Files: Contain data in a format that isn't human-readable. Used for non-
A destructor is a special method that is automatically called when an object is
text files like images, videos, or executable programs.
about to be destroyed.
Basic File Operations:
Opening a File:
class MyClass:
Use the open() function to open a file. It takes two parameters: the file name
def __del__(self):
and the mode.
# destructor code
Modes include:
4. Polymorphism:
'r': Read (default mode).
Polymorphism allows objects to be treated as instances of their parent class,
'w': Write (creates a new file or truncates the existing file).
making it easier to write more flexible and reusable code.
'a': Append (opens the file for writing, but appends to the end).
'b': Binary mode.
5. Inheritance:
'x': Exclusive creation, if the file exists, the operation will fail.
Multilevel Inheritance:
file = open("example.txt", "r")
Reading from a File:
class Animal:
Use methods like read(), readline(), or readlines() to read content from a file.
# Animal class code
Example:
content = file.read()
class Dog(Animal):
Writing to a File:
# Dog class code
Use the write() method to write content to a file.
Example:
class Bulldog(Dog):
file = open("example.txt", "w")
# Bulldog class code
file.write("Hello, this is a sample text.")
Multiple Inheritance:
Closing a File:
Always close a file using the close() method when you're done with it.
class Class1:
Example:
# Class1 code
file.close()
Deleting a File:
class Class2:
Use the os.remove() function from the os module to delete a file.
# Class2 code
Example:
import os
class MultiClass(Class1, Class2):
os.remove("example.txt")
# MultiClass code

6. Data Abstraction:
Data abstraction is the concept of hiding the complex reality while exposing
only the essential parts.

You might also like