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

MSC Python-unit1 & 2 Notes

The document outlines the curriculum for a Python Programming course at the Yenepoya Institute, focusing on Object-Oriented Programming (OOP) concepts such as classes, objects, polymorphism, encapsulation, inheritance, and data abstraction. It also covers the OS module in Python for file and directory management, including basic operations like creating, deleting, and renaming files and directories. Additionally, the document discusses Python's role in multimedia processing, AI and machine learning for content analysis, and data visualization techniques.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

MSC Python-unit1 & 2 Notes

The document outlines the curriculum for a Python Programming course at the Yenepoya Institute, focusing on Object-Oriented Programming (OOP) concepts such as classes, objects, polymorphism, encapsulation, inheritance, and data abstraction. It also covers the OS module in Python for file and directory management, including basic operations like creating, deleting, and renaming files and directories. Additionally, the document discusses Python's role in multimedia processing, AI and machine learning for content analysis, and data visualization techniques.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

The Yenepoya Institute of Arts, Science, Commerce and

Management
A Constituent Unit of Yenepoya (Deemed to be University)
COURSE NAME-PYTHON PROGRAMMING

PYTHON PROGRAMMING: OOPS


In Python, object-oriented Programming (OOPs) is a programming paradigm that uses objects and
classes in programming. It aims to implement real-world entities like inheritance,
polymorphisms, encapsulation, etc. in the programming. The main concept of OOPs is to bind
the data and the functions that work on that together as a single unit so that no other part of the
code can access this data.
OOPs Concepts in Python
• Class
• Objects
• Polymorphism
• Encapsulation
• Inheritance
• Data Abstraction

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.

Every object has the following property.

• Identity: Every object must be uniquely identified.


• State: An object has an attribute that represents a state of an object, and it also reflects the property
of an object.
• Behavior: An object has methods that represent its behavior.

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.

Unit 2: Exceptions in python


The OS module in Python provides functions for interacting with the operating system. OS
comes under Python’s standard utility modules. This module provides a portable way of
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
using operating system-dependent functionality. The *os* and *os.path* modules include
many functions to interact with the file system.
Python OS Module Functions
Here we will discuss some important functions of the Python os module :
• Handling the Current Working Directory
• Creating a Directory
• Listing out Files and Directories with Python
• Deleting Directory or Files using Python

Basic OS Module Operations

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.

Current Working Directory: getcwd(), chdir()

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.

- `getcwd()`: This method returns the current working directory.

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.

# Change the current working directory


os.chdir('/path/to/directory')
print("Directory changed to:", os.getcwd())

Handling Directories: mkdir(), rmdir(), listdir()

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.

# Create a new directory


os.mkdir('new_directory')

- `rmdir()`: Deletes a directory. The directory should be empty, and its absence won’t trigger an
error.

# Remove the directory


os.rmdir('new_directory')

- `listdir()`: Lists all files and subdirectories in the specified directory.

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)

Working with Files: rename(), remove()

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.

File and Directory Information

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.

Fetching File Attributes: stat()

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_size gives the file size in bytes.

- st_mtime provides the last modified time (in seconds since epoch).

- st_mode indicates permissions and file type.

- st_ino is the inode number in filesystems that support it.


Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
Path Manipulations with os.path

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.

path = os.path.join('folder', 'subfolder', 'file.txt')


print(path) # Output: 'folder/subfolder/file.txt' on Linux/macOS, 'folder\\subfolder\\file.txt' on
Windows

- `split()`: Splits the path into two parts: everything before the final slash and everything after.

head, tail = os.path.split('/path/to/file.txt')


print("Head:", head) # Output: '/path/to'
print("Tail:", tail) # Output: 'file.txt'

- `exists()`: Checks if the given path exists, be it a file or a directory.

if os.path.exists('example.txt'):
print('File exists!')

- `isfile()`: Determines if the given path points to a file.

if os.path.isfile('example.txt'):
print('It is a file!')

- `isdir()`: Determines if the given path points to a directory.

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.

USING PYTHON FOR MULTIMEDIA:

Python, a versatile and powerful programming language, plays a pivotal role in


integrating multimedia content such as images and videos into social media platforms.
Its extensive libraries and frameworks support the processing, analysis, and
dissemination of multimedia, making it an indispensable tool in the realm of social
media technology.

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

AI and Machine Learning for Content Analysis:

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.

Data Visualization and Interactive Content:

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.

You might also like