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

Python

Uploaded by

ajoantony2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python

Uploaded by

ajoantony2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Python

• Packages
• File Handles
• Date and Time operations
• Classes
.

Packages
In Python, a package is a collection of related modules and subpackages that provide a
specific functionality or service.
Packages are used to organize and structure code, making it easier to reuse and distribute.
Each package contains an __init__.py file, which distinguishes it from a
regular directory and allows Python to recognize it as a package.

mypackage/

├── __init__.py
├── module1.py
└── module2.py
main.py
Example pgm

from mypackage import module1, module2 #main.py


module1.greet("Alice") #Using functions from module1
result = module2.add(3, 5) #Using functions from module2
print("The result of addition is:", result)
Output
Hello, Alice!
The result of addition is: 8
File Handling
File handling in Python refers to the operations that allow
you to create, read, update, and delete files. Python
provides built-in functions to handle files without needing
any external libraries.
Basic File Operations
1. Opening a File
2. Writing to a File
3. Reading from a File
4. Closing a File
1. Opening a File
• To work with a file, you first need to open it using the open() function.
• This function requires at least two arguments:
*file name and
*mode in which to open the file.
Common Modes:
"r": Read (default mode). Opens a file for reading; an error occurs if the file does not exist.
"w": Write. Opens a file for writing; creates the file if it does not exist or truncates it if it does.
"a": Append. Opens a file for appending; creates the file if it does not exist.
"x": Exclusive creation. Fails if the file already exists.

file = open('example.txt', 'w') # Opens or creates example.txt for


writing
2. Writing to a File

• To write data to a file, you can use the write() method or writelines()
method for multiple lines.
3. Reading from a File
• Reading from a file in Python is a straightforward
process that involves opening the file, reading its
contents, and then closing it.
4. Closing a File
• It is important to close a file after completing operations to free up
system resources.
• Syntax :
Date and Time Operation
Date and time operations in Python are primarily handled using the
datetime module, which provides classes for manipulating dates and
times.
Date Manipulation
Time Manipulation

• The time module in Python provides various functions for handling


time-related tasks, including getting the current time, formatting time,
and performing operations based on time.
Class in Python
• Python is an object oriented programming language.
• A class in Python is a blueprint for creating objects (instances). It
encapsulates data (attributes) and behavior (methods) that define the
properties and actions of the objects created from it.
• To create a class, use the keyword “class”
class MyClass:
x=5
p1 = MyClass()
print(p1.x)
o/p
Object
• Object is an instance of the data structure defined by a class.
Inheritance
Inheritance is the process of forming a new class from an existing class
or base class.
Function overloading
Function overloading is a form of polymorphism that allows a function
to have different meanings, depending on its context
Operator Overloading
Operator overloading is a form of polymorphism that allows
assignment of more than one function to a particular operator.
Function overriding
• Function overriding allows a child class to provide a specific
implementation of a function that is already provided by the base
class.
• Child class implementation of the overridden function has the same
name parameters and return type as the function in the base class.

You might also like