0% found this document useful (0 votes)
48 views6 pages

Advanced Python Programming Notes

The document provides a comprehensive overview of Advanced Python Programming at the diploma level, covering key topics such as modules, packages, exception handling, file handling, advanced OOP concepts, and GUI programming with Tkinter. Each unit includes definitions, examples, and summaries to facilitate understanding. The final overall summary emphasizes the importance of code reusability, error control, permanent storage, real-world design, and GUI application development.

Uploaded by

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

Advanced Python Programming Notes

The document provides a comprehensive overview of Advanced Python Programming at the diploma level, covering key topics such as modules, packages, exception handling, file handling, advanced OOP concepts, and GUI programming with Tkinter. Each unit includes definitions, examples, and summaries to facilitate understanding. The final overall summary emphasizes the importance of code reusability, error control, permanent storage, real-world design, and GUI application development.

Uploaded by

dhyan03344
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Advanced Python Programming (Diploma Level)

As per GTU Syllabus (DI04032031)


Language style: Simple English + Gujarati mix
Level: Diploma Student – Book Type Notes

UNIT 1: Modules and Packages

1.1 Module

Definition:
A module is a Python file that contains functions, variables, or classes which can be reused in another
program.

👉 Gujarati:
Module એટલે Python ની એક .py ફાઇલ, જે માં functions, variables અથવા classes હોય છે અને બીજી ફાઇલમાં ઉપયોગ કરી
શકાય.

1.1.1 Creating User Defined Module

Example:

[Link]

def add(a, b):


return a + b

def sub(a, b):


return a - b

1.1.2 Importing Module

(a) Normal Import

import mathdemo
print([Link](10, 5))

(b) From Import

1
from mathdemo import add
print(add(5, 3))

**(c) From Import with ***

from mathdemo import *


print(sub(10, 4))

1.1.3 Module Search Path

Python module search order: 1. Current folder 2. PYTHONPATH 3. Installation directory

1.2 Package

Definition:
A package is a folder that contains multiple modules.

👉 Gujarati:
ઘણા modules ને સાથે રાખવા માટે folder બનાવીએ, તે ને package કહે વાય.

1.2.1 Creating Package

Structure:

mypack/
__init__.py
[Link]
[Link]

[Link]

def hello():
print("Hello from file1")

1.2.2 Import Package

from mypack.file1 import hello


hello()

2
1.3 Installing Packages using PIP

pip install numpy


pip uninstall numpy

Summary – Unit 1: - Module = single Python file - Package = collection of modules - PIP is used to install
external libraries

UNIT 2: Exception Handling

2.1 Exception

Definition:
An exception is an error that occurs during program execution.

👉 Gujarati:
Program ચાલતી વખતે error આવે તે ને exception કહે છે.

2.2 Types of Exceptions

Built-in Exception Example:

print(10/0) # ZeroDivisionError

2.3 Handling Exception (try-except-finally)

try:
a = int(input("Enter number:"))
print(10/a)
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Program End")

2.4 User Defined Exception

class AgeError(Exception):
pass

try:
age = int(input("Enter age:"))

3
if age < 18:
raise AgeError
except AgeError:
print("Age must be 18 or above")

Summary – Unit 2: - try block checks error - except handles error - finally always executes

UNIT 3: File Handling

3.1 File Types

1. Text File ( .txt )


2. Binary File ( .dat )

3.2 Writing and Reading File

f = open("[Link]", "w")
[Link]("Hello Python")
[Link]()

f = open("[Link]", "r")
print([Link]())
[Link]()

3.3 File Offset

f = open("[Link]", "r")
[Link](6)
print([Link]())

3.4 Pickle (Object Serialization)

import pickle

list1 = [10, 20, 30]


f = open("[Link]", "wb")
[Link](list1, f)
[Link]()

Summary – Unit 3: - Files store data permanently - Pickle stores Python objects

4
UNIT 4: Advanced OOP Concepts

4.1 Inheritance

class A:
def show(self):
print("Class A")

class B(A):
pass

obj = B()
[Link]()

4.2 Polymorphism

class Dog:
def sound(self):
print("Bark")

class Cat:
def sound(self):
print("Meow")

4.3 Encapsulation

class Student:
def __init__(self):
self.__marks = 90

def show(self):
print(self.__marks)

4.4 Operator Overloading

class Test:
def __add__(self, a):
return self.x + a.x

Summary – Unit 4: - Inheritance = reuse - Polymorphism = many forms - Encapsulation = data security

5
UNIT 5: GUI Programming with Tkinter

5.1 First Tkinter Program

from tkinter import *

root = Tk()
[Link]("My App")
Label(root, text="Hello Tkinter").pack()
[Link]()

5.2 Widgets Example

Button(root, text="Click Me").pack()


Entry(root).pack()

5.3 Geometry Managers

• pack()
• grid()
• place()

5.4 Event Handling

def click():
print("Button Clicked")

Button(root, text="Click", command=click).pack()

Summary – Unit 5: - Tkinter is used for GUI - Widgets create interface - Events handle user action

Final Overall Summary


✔ Modules & Packages → Code Reusability
✔ Exception Handling → Error Control
✔ File Handling → Permanent Storage
✔ OOP → Real World Design
✔ Tkinter → GUI Application

End of Diploma Level Advanced Python Notes

Common questions

Powered by AI

Geometry Managers in Tkinter, such as pack(), grid(), and place(), are crucial for arranging widgets within a window efficiently. The pack() manager positions widgets automatically in blocks before or after each other. The grid() manager places widgets in a table-like structure with rows and columns, allowing sophisticated layout designs. Meanwhile, the place() manager offers precise control over widget positions by specifying x and y coordinates, giving designers the flexibility to create complex GUI layouts .

Operator overloading in Python is implemented by defining special methods in classes that redefine how operators work with class instances. The document demonstrates this with the __add__ method, which allows the '+' operator to be used with instances of the Test class. This is useful because it allows developers to extend or customize the behavior of operators to suit specific needs of data types or objects, enabling intuitive operations on custom objects .

Modules and packages facilitate code reusability and organization. Modules in Python are files containing functions, variables, or classes that can be reused in other programs, promoting code reuse and simplification. Packages, being collections of modules, help in grouping related functionalities together, which further enhances organization and reduces redundancy across multiple projects .

The document explains that Python supports both temporary and permanent data storage through file handling. Text and binary files can be used to store data persistently on disk, which is permanent. Temporary data storage is also possible by manipulating file offsets and overwriting data within files. Furthermore, the use of the pickle module allows Python objects to be serialized and stored in binary files, enabling complex data structures to be preserved and retrieved .

The document indicates that PIP, Python's package manager, is used for the installation and management of external libraries. It supports easy installation, updating, and removal of packages. For example, installing a package like 'numpy' can be done with the command 'pip install numpy', and it can be uninstalled using 'pip uninstall numpy', streamlining the handling of dependencies in Python projects .

The document illustrates that inheritance promotes code reusability by allowing a new class to inherit properties and behaviors from an existing class. For instance, class B inherits from class A, directly gaining its methods and attributes, such as the show method without needing to redefine them. This simplifies code maintenance and development by ensuring the reuse of existing, well-tested functionalities .

The document exemplifies user-defined exceptions by demonstrating a custom exception class, AgeError, which is raised when a particular condition (age being less than 18) is not met. This is useful in programming as it allows developers to create specific exceptions that are meaningful to their application domain, facilitating clearer code and tailored error messages, which enhances debugging and maintenance .

The try-except-finally construct in Python allows for comprehensive error handling. The try block is used to test code for errors, while the except block handles exceptions that occur within the try block, preventing the program from crashing. The finally block contains cleanup code that is to be executed regardless of whether an exception was raised, which ensures resources are properly released and any necessary finalization is carried out .

Polymorphism is demonstrated in Python by allowing different classes to implement methods of the same name but with distinct behaviors. In the document's example, classes Dog and Cat both have a method sound(), which performs different actions (printing "Bark" and "Meow," respectively). This polymorphic behavior is significant as it enables flexibility and the ability for different objects to be treated interchangeably, thus supporting easier code maintainability and scalability in object-oriented design .

Encapsulation in object-oriented programming is portrayed as a mechanism for data security within the document. By using access specifiers like private variables (e.g., __marks in the Student class), encapsulation restricts direct access to certain attributes from outside the class. This protects the integrity of data and prevents unauthorized modifications, ensuring that internal object states are safely guarded from unintended interference .

You might also like