MSC Python-unit1 & 2 Notes
MSC Python-unit1 & 2 Notes
Management
A Constituent Unit of Yenepoya (Deemed to be University)
COURSE NAME-PYTHON PROGRAMMING
Python Class
• Class: The class is a user-defined data structure that binds the data members and methods into a
single unit. Class is a blueprint or code template for object creation. Using a class, you can
create as many objects as you want.
• Object: An object is an instance of a class. It is a collection of attributes (variables) and methods.
We use the object of a class to perform actions.
Objects have two characteristics: They have states and behaviors (object has attributes and
methods attached to it) Attributes represent its state, and methods represent its behavior. Using
its methods, we can modify its state.
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
Polymorphism:
The word polymorphism means having many forms. In programming, polymorphism means the
same function name (but different signatures) being used for different types. The key
difference is the data types and number of arguments used in function.
Ex:
Encapsulation:
Encapsulation is one of the fundamental concepts in object-oriented programming (OOP).
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
It describes the idea of wrapping data and the methods that work on data within one
unit. This puts restrictions on accessing variables and methods directly and can
prevent the accidental modification of data. To prevent accidental change, an
object’s variable can only be changed by an object’s method. Those types of
variables are known as private variables.
Ex:
Data Abstraction:
Data abstraction is one of the most essential concepts of Python OOPs which is used to hide
irrelevant details from the user and show the details that are relevant to the users.
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
Inheritance:
It is a mechanism that allows you to create a hierarchy of classes that share a set of properties
and methods by deriving a class from another class. Inheritance is the capability of one
class to derive or inherit the properties from another class.
The OS module offers a broad range of fundamental operations, each designed to ease interaction
with the underlying system. Here, we’ll familiarize ourselves with some of these foundational
tasks, laying the groundwork for more advanced interactions later on.
Every program that runs has a "current working directory," or CWD. It's the folder where the program
looks for files to open, and where it will place files that it creates.
import os
# Get the current working directory
current_directory = os.getcwd()
print("Current Directory:", current_directory)
- `chdir()`: This method changes the current working directory to the specified path.
Directories, often called folders, are a crucial part of file organization on any computer. The os
module gives you the tools to navigate, create, and delete directories.
- `mkdir()`: Creates a directory. Ensure the path doesn’t already exist, or you'll get an error.
- `rmdir()`: Deletes a directory. The directory should be empty, and its absence won’t trigger an
error.
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
# List all entries in the current directory
entries = os.listdir()
print("Entries:", entries)
While directories hold files and subdirectories, the actual data resides in the files. The os module
provides methods to manipulate these files, ensuring data is stored or changed as required.
- `rename()`: As the name suggests, this method is used to rename a file or directory.
# Rename a file
os.rename('old_filename.txt', 'new_filename.txt')
- `remove()`: Deletes a file. Note that it doesn't send the file to the Trash or Recycle Bin; it
permanently deletes the file.
# Delete a file
os.remove('unwanted_file.txt')
These basic operations provided by the os module are essential for any Python developer. They act as
the building blocks for more complex system-level scripts and programs. By mastering these
fundamental methods, you set a solid foundation for more advanced OS interactions.
While creating, deleting, and renaming files or directories is fundamental, sometimes, we need to
retrieve or manipulate information about them. Python's os module, combined with its os.path
submodule, offers functions to achieve precisely that.
Every file or directory on a computer comes with metadata - information about its size, permissions,
timestamps, and more. The stat() function fetches these details for us.
import os
file_stats = os.stat('example.txt')
print("File Size:", file_stats.st_size, "bytes")
print("Last Modified:", file_stats.st_mtime)
print("File Mode:", file_stats.st_mode)
print("Inode number:", file_stats.st_ino)
Here:
- st_mtime provides the last modified time (in seconds since epoch).
Handling file paths across different operating systems can be tricky due to variations in file path
conventions. Thankfully, the os.path submodule offers platform-independent operations.
- `join()`: Combines multiple path names into a single path. It's safer than concatenating strings as it
considers the underlying OS.
- `split()`: Splits the path into two parts: everything before the final slash and everything after.
if os.path.exists('example.txt'):
print('File exists!')
if os.path.isfile('example.txt'):
print('It is a file!')
if os.path.isdir('folder_name'):
print('It is a directory!')
DATA MANAGEMENT:
Database Management Systems (DBMS) are software systems used to store, retrieve, and run
queries on data. A DBMS serves as an interface between an end-user and a database, allowing
users to create, read, update, and delete data in the database.
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
Handling Image and Video Data:
Python offers numerous libraries such as OpenCV, Pillow, and scikit-image for processing and
manipulating image data. These libraries allow for tasks like image resizing, filtering, and
transformation, essential for optimizing images for social media platforms. Similarly, for video
content, Python provides tools like MoviePy and OpenCV, enabling video editing, processing, and
conversion to formats suitable for social media
Python is at the forefront of AI and machine learning, which are crucial in analyzing and
categorizing multimedia content. Libraries like TensorFlow and PyTorch enable the development of
algorithms for image and video recognition, content tagging, and automated moderation. This AI-
driven approach ensures that content uploaded to social media platforms is relevant, appropriate,
and engaging.
Python's libraries like Matplotlib, Seaborn, and Plotly are used to create data visualizations that are
increasingly shared on social media platforms. These visualizations, including interactive graphs
and charts, enhance the engagement of posts and provide an attractive way to present data and
information.
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.